2014-05-10 2 views
0

화면에서 요소를 이동하는 방법을 알아 내려고하고 있습니다. 더 정확한 제어가 필요하기 때문에 JQuery의 animate() 메서드를 사용하고 싶지 않습니다.Javascript - 요소의 위치를 ​​변경하는 setInterval

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); 

http://jsfiddle.net/9RLkJ/ 감사합니다 : 여기에 기본적인 생각이다.

+0

무엇이 문제입니까? –

+0

또 다른 '나는 5 분을 시도하고 실패했다. 이제 나에게 해줄 수 있겠 니?' 문제. – GameAlchemist

답변

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

}, 1000); 

위의 코드는 오른쪽에있는 상자를 이동합니다;

실무 데모 move the box to bottom

+0

고마워 이것이 완벽하게 작동합니다. – user1009573

0

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);

관련 문제