Javascript时钟颜色样式
Javascript clock color styling
我是JS的新手,想知道是否有办法在代码中的时钟小时和分钟之间修改"|"的颜色。我希望它是红色的。感谢您的帮助、提示和最重要的解释:)
<script type="text/javascript">
"use strict";
var textElem = document.getElementById("clock");
var textNode = document.createTextNode("");
textElem.appendChild(textNode);
var curFontSize = 24; // Do not changes
function updateClock() {
var d = new Date();
var s = "";
s += (10 > d.getHours () ? "0" : "") + d.getHours() + "|";
s += (10 > d.getMinutes() ? "0" : "") + d.getMinutes();
textNode.data = s;
setTimeout(updateClock, 60000 - d.getTime() % 60000 + 100);
}
function updateTextSize() {
var targetWidth = 0.9; // Proportion of full screen width
for (var i = 0; 3 > i; i++) { // Iterate for better better convergence
var newFontSize = textElem.parentNode.offsetWidth * targetWidth / textElem.offsetWidth * curFontSize;
textElem.style.fontSize = newFontSize.toFixed(3) * "pt";
curFontSize = newFontSize;
}
}
updateClock();
updateTextSize();
window.addEventListener("resize", updateTextSize);
</script>
@font-face {
font-family: Steelfish;
src: url(Images/Steelfish.ttf);
}
body {
margin: 0;
padding: 0;
text-shadow: 0px 1px 1px black;
font-family: Steelfish;
font-weight:100;
color:red;
letter-spacing: 1px;
}
#clock {
position:absolute; top:-17px; left:205px; text-align:left; z-index:50;
font-size:10pt;
color:gray;
}
#minute{color:gray;}
#icon{position:absolute; top:24px; left:0px; width:50px; z-index:50;}
#location{
position:absolute; top:50px; left:80px; width:250px; height:50px; text-align:left; z-index:50;
color:#f79b00;
font-size:17px;
}
#temperature{
position:absolute; top:66px; left:160px; width:250px; height:50px; text-align:left; z-index:50;
color:#248ffc;
font-size:19px;
}
#condition{
position:absolute; top:14px; left:80px; width:250px; height:50px; text-align:left z-index:50;
color:f16000;
font-size:19px;
}
#barre{
position:fixed; top:62px; left:60px; width:140px; height:64px; text-align:left; z-index:12;
border-top: 1px solid grey;
}
<html>
<body onload="didLoadPage()">
<div id="barre"></div>
<div id="wrap">
<section id="topSection">
<div id="left" style="width:40%;vertical-align:middle;">
<p id="location"">London</p>
<p id="clock" style="font-size:36pt;"></p>
<p id="condition">Mostly Clear</p>
</div>
<div id="middle" style="width:20%;">
<center>
<img id="icon" src="IconSets/Sink/0.png" style="width:75px;" alt="" />
</center>
</div>
<div id="right" style="width:40%;">
<p><span id="temperature">10</span>
</div>
</section>
</div>
<script type="text/javascript">
"use strict";
var textElem = document.getElementById("clock");
var textNode = document.createTextNode("");
textElem.appendChild(textNode);
var curFontSize = 24; // Do not changes
function updateClock() {
var d = new Date();
var s = "";
s += (10 > d.getHours () ? "0" : "") + d.getHours() + "|";
s += (10 > d.getMinutes() ? "0" : "") + d.getMinutes();
textNode.data = s;
setTimeout(updateClock, 60000 - d.getTime() % 60000 + 100);
}
function updateTextSize() {
var targetWidth = 0.9; // Proportion of full screen width
for (var i = 0; 3 > i; i++) { // Iterate for better better convergence
var newFontSize = textElem.parentNode.offsetWidth * targetWidth / textElem.offsetWidth * curFontSize;
textElem.style.fontSize = newFontSize.toFixed(3) * "pt";
curFontSize = newFontSize;
}
}
updateClock();
updateTextSize();
window.addEventListener("resize", updateTextSize);
</script>
</body>
</html>
这是一把正在工作的小提琴-https://jsfiddle.net/fhv6r0sL/
您可能想要添加一个内联元素来包装|
由于您创建了一个textNode,您将无法立即添加任何HTML标记。
这是更新后的JS代码。
"use strict";
var textElem = document.getElementById("clock");
// var textNode = document.createTextNode("");
// textElem.appendChild(textNode);
var curFontSize = 24; // Do not changes
function updateClock() {
var d = new Date();
var s = "";
s += (10 > d.getHours () ? "0" : "") + d.getHours() + "<span class='timePipe'>|</span>";
s += (10 > d.getMinutes() ? "0" : "") + d.getMinutes();
textElem.innerHTML = s;
setTimeout(updateClock, 60000 - d.getTime() % 60000 + 100);
}
function updateTextSize() {
var targetWidth = 0.9; // Proportion of full screen width
for (var i = 0; 3 > i; i++) { // Iterate for better better convergence
var newFontSize = textElem.parentNode.offsetWidth * targetWidth / textElem.offsetWidth * curFontSize;
textElem.style.fontSize = newFontSize.toFixed(3) * "pt";
curFontSize = newFontSize;
}
}
updateClock();
updateTextSize();
window.addEventListener("resize", updateTextSize);
在这里,我删除了您创建textNode并将其附加到textElem
分区的部分
之后,它不再是|
,而是现在的"<span class='timePipe'>|</span>"
,这样我们就可以对timePipe
类进行样式化。
CSS
.timePipe {
color: red;
}
现在,我们可以使用innerHTML方法将var s
作为HTML注入到textElem
中。
textElem.innerHTML = s
请注意,即使您创建了一个textNode也可以,但我们需要HTML来设计|
的样式。因此,没有必要在那里设置一个,而是将内容添加为HTML。
希望对有所帮助
相关文章:
- javascript样式的背景颜色在设置时闪烁.ASP.NET
- Javascript时钟颜色样式
- jQuery颜色样式交替
- 如何从jquery中获取内联样式的边框颜色
- 如何设置'样式:color'淘汰赛中的颜色选择器.JS
- 如何更改chardin.js中的文本颜色和样式
- OL3:如何获取矢量图层的现有样式属性(例如填充颜色、描边颜色等)
- 如何将样式/颜色皮肤设为默认
- 使用Javascript为输入字段添加边框样式颜色(提交时没有值),该字段已经在css中带有!important标签的边
- 引导 NAV 方式在移动设备中折叠或展开时设置颜色样式
- 如何使样式元素的一部分成为函数?(IE:颜色:$random而不是颜色:红色)
- CSS3 颜色样式在使用 javascript 删除类后仍然存在
- 设置内容样式包含字体系列、字体大小和颜色的可编辑列表 (
- )
- JavaScript 函数来设置背景颜色的样式
- 如何使用内联背景颜色样式设置窗体输入字段的样式
- 如何在Xpage中动态更改样式???事实上,我想眨眼一个不同颜色的标签
- 我需要在点击页面中的链接时切换我的样式表,以便页面以不同的颜色主题显示
- 从字符串中删除所有ANSI颜色/样式
- 改变悬停样式颜色效果的函数
- 可以设置样式颜色大小吗