建立一个颜色变化的模拟时钟w/javascript的问题
所以我试图通过构建一个谷歌插件教自己的JavaScript。这个想法是,每个间隔的颜色变化(每5秒秒手,每5分钟分钟等),但我有麻烦。我已经构建了时钟,除了一些文体变化之外,我认为这只是离开这个讨厌的JavaScript。所以我的第一个想法是计算时钟的角度,并为每种颜色创建“区域”(当秒针在12-1或30度之间时为红色,然后在30度后变为绿色,然后变为蓝色等等)。所以这是我的所有html和css + javascript使时钟工作和角度计算。建立一个颜色变化的模拟时钟w/javascript的问题
<!DOCTYPE html>
<html>
<head>
<title>Analog Clock</title>
<link type="text/css" rel="stylesheet" href="clock1.css">
</head>
<body>
<div class="analog-clock">
<svg width="140" height="140">
<circle id="clock-face" cx="70" cy="70" r="65" />
<line id="h-hand" x1="70" y1="70" x2="70" y2="38" />
<line id="m-hand" x1="70" y1="70" x2="70" y2="20" />
<line id="s-hand" x1="70" y1="70" x2="70" y2="12" />
<text x="62" y="18">12</text>
<text x="126" y="76">3</text>
<text x="66" y="130">6</text>
<text x="7" y="76">9</text>
</svg>
<div class="time-text">
<span id="hr">:</span>
<span></span>
<span id="min">:</span>
<span></span>
<span id="sec">:</span>
<span id="suffix"></span>
</div>
</div>
</body>
</html>
*{
margin:0;
padding:0;
font-family:sans-serif;
font-size:14px;
}
.analog-clock{
width:140px;
height:140px;
}
#clock-face{
stroke:black;
stroke-width:2px;
fill:transparent;
}
#h-hand, #m-hand, #s-hand {
stroke:black;
stroke-linecap:round;
}
#h-hand{
stroke-width:3px;
}
#m-hand{
stroke-width:2px;
}
#s-hand{
stroke-width:1px;
}
.time-text{
text-align:center;
}
function clock(){
//calculate angle
var d, h, m, s;
d = new Date;
h = 30 * ((d.getHours() % 12) + d.getMinutes()/60);
m = 6 * d.getMinutes();
s = 6 * d.getSeconds();
//move hands
setAttr('h-hand', h);
setAttr('m-hand', m);
setAttr('s-hand', s);
setAttr('s-tail', s+180);
//display time
h = d.getHours();
m = d.getMinutes();
s = d.getSeconds();
// necessary? if(h >= 12){
// setText('suffix', 'PM');
// }else{
// setText('suffix', 'AM');
// }
if(h != 12){
h %= 12;
}
//setText('sec', s);
// setText('min', m);
// setText('hr', h);
//call every second
setTimeout(clock, 1000);
};
function setAttr(id,val){
var v = 'rotate(' + val + ', 70, 70)';
document.getElementById(id).setAttribute('transform', v);
};
function setText(id,val){
if(val < 10){
val = '0' + val;
}
document.getElementById(id).innerHTML = val;
};
const colors = ['black', 'blue', 'red', 'green', 'pink', 'orange', 'yellow', 'purple', 'black', 'silver', 'gold', 'brown', 'gray', 'blue', 'red', 'pink', 'magenta', 'orange', 'black'];
function getColorTerse() {
const now = new Date();
const minutes = now.getMinutes();
const interval = Math.floor(minutes/5);
return colors[interval];
}
const color = getColorTerse();
const attribute = 'stroke: ' + color;
document.getElementById('m-hand').setAttribute('style', attribute);
//var hand = document.getElementById("h-hand")?
//var handm = document.getElementById("m-hand")?
//var hands = document.getElementById("s-hand")?
然后这里是我到目前为止计算的角度。
function testColor(){
var ang;
var num;
for(num = 1; num < 13; num++){
ang = num * Math.PI/6;
}
所以我一直在这里停留一段时间,我跑出来的想法(我试过一个整体的setInterval的事情,但这是一个烂摊子太),所以我想我只是想知道,如果它可以分配颜色值转换为时钟上的角度,然后在该角度下分钟/小时/秒针将变为该颜色。有没有办法像if/else语句或while循环一样编写?胸围基本上最大的问题是,我甚至可以将颜色值分配给这些角度,还是应该采取不同的策略?
编辑:所以我在建议的代码中添加。我决定在小时,秒和分钟测试它,并得到相同的结果。加载时钟时分配颜色,但除非刷新页面,否则颜色不会改变颜色
与基于当前时间绘制手的方式相同,您应该根据该时间确定颜色为好。在编程中,这是一个重要的概念,您希望将逻辑基于模型,而不是UI元素。
这里有一些JavaScript让你开始。
function getColor() {
const now = new Date();
const minutes = now.getMinutes();
switch (Math.floor(minutes/5)) {
case 0:
return 'black';
break;
case 1:
return 'blue';
break;
//etc
default:
return 'red';
break;
}
}
这是如何使用交换机一个很好的例子,但你会发现它会得到很长的,如果你有20个不同的时间间隔。下面的代码完成相同的事情,但使用数组。
const colors = ['black', 'blue', 'red', 'green', 'etc', 'but', 'make', 'sure', 'you', 'have', '20', 'elements', 'in', 'there', 'or', 'it', 'will', 'return', 'undefined!'];
function getColorTerse() {
const now = new Date();
const minutes = now.getMinutes();
const interval = Math.floor(minutes/5);
return colors[interval];
}
然后,您可以使用此功能,像这样:
const color = getColorTerse();
const attribute = 'stroke: ' + color;
document.getElementById('h-hand').setAttribute('style', attribute);
,所以我插入了它,但我无法让手改变颜色。我尝试使用var hand/var hands/var handm我在原始帖子中注释掉了。然后我尝试在getColorTerse中插入手/ s/m,但没有运气。 – amnmustafa15
@ amnmustafa15所以声明一个函数并执行它是有区别的。你确定你的代码正在执行吗?使用chrome的开发工具和调试器断点来确保。发布你的代码的更新版本,并告诉我们你遇到什么问题。 –
@ amnmustafa15编辑我的答案,显示如何使用该功能。 –
那岂不是更容易计算基于当前时间的颜色,而不是在手的位置? –
@GuillaumeCR可能,我还没有探索过这个选项。是否有可能使用JS来构建它?就像我上面说过的,我使用这个来学习javascript – amnmustafa15