【发布时间】:2012-10-26 03:12:46
【问题描述】:
Jsfiddle:http://jsfiddle.net/yJJs7/
Javascript:
function main(){
var centerx=250;
var centery=250;
var degrees=0;
var div=document.getElementById('test');
var move=function(){
if(degrees>360)degrees=degrees%360;
var radians = degrees*Math.PI/180;
var newx = Math.cos(radians)*100;
var newy = Math.sin(radians)*100;
div.style.top=(newy+centery)+'px';
div.style.left=(newx+centerx)+'px';
degrees+=10;
};
setInterval(move,50);
console.log(div);
}
main();
HTML:
<div id="test"></div>
<div id="test2"></div>
CSS:
#test{
height:100px;
width:100px;
background:black;
border-radius:100px;
position:fixed;
}
#test2{
position:fixed;
height:30px;
width:30px;
background:black;
border-radius:30px;
position:fixed;
top:250px;
left:250px;
}
第二个 div 以 250x250 像素为中心,第一个 div 应围绕它旋转。为什么不是?
【问题讨论】:
-
+1 回答一个有趣的问题:-) 纯 JS 2D 动画
标签: javascript animation geometry trigonometry