2011-06-18 46 views
2

嗨如果能請看看我的代碼並告訴我下一步我想要獲得這個結果。當div達到某個PX時發生火災事件

<script type="text/javascript"> 
    function doMove1() { 
     foo1.style.left = parseInt(foo1.style.left)+1+'px'; 
     setTimeout(doMove1,20); // call doMove in 20msec 
    } 
    function start1() { 
     foo1 = document.getElementById('fooObject'); // get the "foo" object 
     foo1.style.left = '0px'; // set its initial position to 0px 
     doMove1(); // start animating 
    } 
window.onload = start1; 
</script> 
<body> 
     <div id="fooObject"> 
    <img src="images/pac.gif" width="38" height="38"></div> 
</body> 

基本上什麼情況是,整個screen.What我試圖完成一個圖像移動是當圖像達到某個像素(比方說900px),我想要一個事件激發。關於完成這項任務的任何提示我是一名初學者,我一直在努力尋找幫助。

回答

1

只需添加這種檢查在doMove1

function doMove1() { 
    var left = parseInt(foo1.style.left, 10) + 1; 
    if (left >= 900) { 
    trigger_my_event(); 
    } else { 
    foo1.style.left = left + 'px'; 
    setTimeout(doMove1,20); // call doMove in 20msec 
    } 
} 

這是否幫助?

+0

謝謝,這正是我想要的。我做了一些類似的東西,但我做了變量,如--- var ypos = foo1.style.left。我非常感謝你的幫助。 –