2012-01-20 66 views
0

效果搖我有jQuery代碼:jQuery的.stop()不工作的懸停

setInterval(function() { 

    var grayarrow = jQuery("ul#dropdowngray").parent(); 
    var greenarrow = jQuery("ul#dropdown").parent(); 

    grayarrow.effect('shake', { times:2 }, 100); 
    greenarrow.effect('shake', { times:3 }, 200); 

    jQuery("ul#dropdowngray").parent().hover( 

     function(){ grayarrow.stop().effect('shake', { times:0 }, 100); }, 

     function(){ grayarrow.stop().effect('shake', { times:2 }, 100); } 

    ); 

    jQuery("ul#dropdown").parent().hover( 

     function(){ greenarrow.stop().effect('shake', { times:0 }, 100); }, 

     function(){ greenarrow.stop().effect('shake', { times:2 }, 100); } 

    ); 

}, 5000); 

看看效果:http://mainstreetfinancing.com.s136249.gridserver.com/frequently-asked-questions-direct-lender-net-lender.html

然而,當我將鼠標懸停在該元素,晃動繼續執行。我本來代碼:

jQuery("ul#dropdowngray").parent().hover( 

      function(){ grayarrow.stop(); }, 

      function(){ grayarrow.stop(); } 

     ); 

,但因爲這是不工作要麼,我實現瞭解決這裏討論:jquery .stop() not working

我怎樣纔能有效地使晃動停止,當鼠標在顫抖元素?

謝謝

回答

2

要立即停止,你需要2個argements(clearQueue:真,jumpToEnd:真)進入停止()調用,見http://api.jquery.com/stop/。你也應該設置一個變量來控制setInterval調用,這樣當鼠標懸停在元素上時,它應該立即返回。這是代碼。要查看效果,請轉至http://jsfiddle.net/BT4bt/

var stopShaking = false; 


var shakeIt = function() { 
    $('#shaker').effect('shake', { times: 6 }, 100); 
} 

setInterval(function() { 
    if (stopShaking) { 
     return; 
    } 
    shakeIt(); 
}, 5000); 

$('#shaker').hover(function(){ 
    $(this).stop(true, true); 
    stopShaking = true; 
}, function(){ 
    stopShaking = false; 
}) 
+0

給我留下了深刻的印象。它工作得很好。謝謝 – IberoMedia