尝试用这种
var deg = 2; // starting
var rotation_diff = 30; // you can change it to 2 if you want to rotate 2 deg in each second
function myFunction()
{
var img = document.getElementById("image");
img.style.webkitTransform = "rotate("+deg+"deg)";
img.style.transform = "rotate("+deg+"deg)";
img.style.MozTransform = "rotate("+deg+"deg)";
img.style.msTransform = "rotate("+deg+"deg)";
img.style.OTransform = "rotate("+deg+"deg)";
setTimeout("myFunction()",1000);
deg = deg + rotation_diff;
}
看到FIDDLE
更新2:
启动和停止
HTML
<img id="image" src="http://static.kuikr.com/images/quikr_logo_f3.png" >
<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
JAVASCRIPT
var deg = 2; // starting
var rotation_diff = 30;
var rotation;
function start()
{
rotation=1;
myFunction();
}
function stop()
{
rotation=0;
}
function myFunction()
{
var img = document.getElementById("image");
img.style.webkitTransform = "rotate("+deg+"deg)";
img.style.transform = "rotate("+deg+"deg)";
img.style.MozTransform = "rotate("+deg+"deg)";
img.style.msTransform = "rotate("+deg+"deg)";
img.style.OTransform = "rotate("+deg+"deg)";
if(rotation==1) {
setTimeout("myFunction()",1000); }
deg = deg + rotation_diff;
}
看到UPDATE FIDDLE
你检查我的答案吗?尝试一次 –