繁体   English   中英

Javascript-setInterval更改元素的位置

[英]Javascript - setInterval to change element's position

我正在尝试找出如何在屏幕上移动元素。 我不想使用JQuery的animate()方法,因为我需要更精确的控制。 这是基本思想:

http://jsfiddle.net/9RLkJ/

setInterval(function() {
    var e = document.getElementById("aDiv");
    // Increase the top position by 1 pixel
    e.style.top = '+1px';
    // If the top position is greater than 100px, set it to 100px
    if (parseInt(e.style.top) > 100) { e.style.top = '100px'; }
}, 1000);

谢谢。

var top= document.getElementById("aDiv").style.top; setInterval(function() { top += 1; var e = document.getElementById("aDiv"); // Increase the top position by 1 pixel e.style.top = top + 'px'; // If the top position is greater than 100px, set it to 100px if (parseInt(e.style.top) > 100) { e.style.top = '100px'; } }, 1000);

var e = document.getElementById("aDiv");
var s = 1;
setInterval(function(){
    var eLeftPos = e.offsetLeft;
    e.style.left = (eLeftPos + s) + 'px';

}, 1000);

上面的代码会将框向右移动;

工作演示将框移到底部

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM