javascript - Div 使用 JavaScript 循环移动

标签 javascript html css dhtml

是否可以使用 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/

相关文章:

css - 为什么我的 div 不重叠?

css - 如何仅使用 CSS 自动将绝对元素置于相对父元素的中心?

javascript - 将 Node.js net.socket 与流式消息协议(protocol)一起使用

html - 如何在分页 Bootstrap 3 旁边垂直居中页面限制器?

javascript - 使用 $stateProvider 的 AngularJS/Ionic 路由 - Controller 不会在第二次调用状态时重新加载

jquery - 文本淡入效果

html - 需要帮助调整站点图像

css - 什么是 .在 CSS 中是什么意思?

javascript - 简单的javascript字符串提取

javascript - 从 Javascript 文本框中删除制表符空间