2016-08-24 66 views
0
var today = new Date(); 
var h = today.getHours(); 
var m = today.getMinutes(); 
var s = today.getSeconds(); 
console.log("hour"+h+"min"+m+"sec"+s); 

document.getElementById("sec").style.transform = "rotate((s*6)deg)"; 

我想旋转ID秒的图像(当前秒* 6)度。但似乎css旋转只能像旋转一样(40deg)。 如果我做 var x =40; rotate('x'deg); 或类似的东西,它不会工作。如何旋转CSS样式转换图像的程度变量?

如何让它与变量一起旋转?

+0

尝试用'document.getElementById(“sec”).style.transform =“rotate((”+ s * 6 +“)deg)替换上一条语句”;' –

+0

明白了。谢谢! –

+0

你能接受我的回答吗? –

回答

0

你正在做的是提供一串rotate((s*6)deg)(字面上与S * 6而不是结果)到变换样式。您需要拆分字符串,以便将结果连接到字符串中。

使用此:

ar today = new Date(); 
var h = today.getHours(); 
var m = today.getMinutes(); 
var s = today.getSeconds(); 
console.log("hour"+h+"min"+m+"sec"+s); 

document.getElementById("sec").style.transform = "rotate(" + (s*6) + "deg)"; 

这将通过可变s次6的值到字符串。因此,而不是CSS分析器质疑文字字符串s*6deg的含义,它将代替120deg(如果变量s是值20),这是一个有效的CSS值。这与您在console.log上面调用一行时使用的概念相同。

0

你需要更换你的最后一句话,

document.getElementById("sec").style.transform = "rotate(("+s*6+")deg)"; 

既然你是路过的,而不是为度值的字符串。

希望这会有所帮助。