2010-08-16 38 views
0

當前代碼:使用jQuery動畫()API並不能讓它停止跳動

$(document).ready(function(){ 
    $('#go').hover(
     function(){ // Change the input image's source when we "roll on" 
      $(this).animate({"top": "-=100px"}, 50, "linear", function(){ 
       $(this).animate({"top": "+=100px"}, 50); 
      }); 
      $(this).attr({ src : '/gfx/go_over.png'}); 
     }   
    ); 
}); 

基本上我希望我的按鈕彈起並改變狀態,那麼當它回來了下來,將留在那個改變的狀態。在滾動期間,我希望發生逆轉。

林不知道我在這裏做什麼,因爲每次它下來它徘徊和再次反彈。

回答

0

如果你傳遞一個函數來.hover()這將在mouseentermouseleave事件被執行,如果你只是希望它動過的元素時發生一次,直接使用.mouseenter(),像這樣:

$(function(){ 
    $('#go').mouseenter(function(){ 
    $(this).animate({"top": "-=100px"}, 50, "linear", function(){ 
     $(this).animate({"top": "+=100px"}, 50); 
    }); 
    $(this).attr({ src : '/gfx/go_over.png'}); 
    }); 
}); 
+0

這給了我相同的結果,因爲一旦它回來了它再次是一個mouseenter – Adam 2010-08-16 18:24:08

+0

@亞當 - 它是否可能包裹在另一個元素,你實際上盤旋,所以你不會得到鼠標問題?另一種方法是像'mouseenter'處理程序那樣首先執行if($(this).is(':animated'))return; – 2010-08-16 18:25:48