是否可以使用 JavaScript 循环旋转 Div。我在一个 HTML 页面中有四个 DIV。我需要循环旋转这些 DIV。
最佳答案
其实并不难:
function moveDiv(t,mdiv) {
t = t + 0.05; // "time"
var r = 10, //radius of circle
xcenter = 400, //x location of circles centre on screen
ycenter = 400, //y location of circles centre on screen
x = Math.floor(xcenter + (r * Math.cos(t))), //circles parametric function
y = Math.floor(ycenter + (r * Math.sin(t))); //circles parametric function
mDiv.style.top = x + "px"; //set divs new coordinates
mDiv.style.left = y + "px"; //set divs new coordinates
setTimeout(function() { //make sure the animation keeps going
moveDiv(t,mdiv);
}, 100);
}
myDiv = //get div element
moveDiv(1,myDiv); //start the animation
还没有测试过,但这是关于它应该如何工作的。确保将这些 div 的 css“位置”属性设置为绝对或固定。另请查看 parametric equation为圈子。
关于javascript - Div 使用 JavaScript 循环移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/842789/