2013-06-05 18 views
0

我有一個主頁的小動畫,需要禁用鼠標滾輪直到完成(我設法使用jquery mousewheel插件)。我的問題是,我不知道如何在這個動畫之後再次啓用鼠標滾輪。如何臨時阻止jquery鼠標滾輪

$('html, body').animate({ 
scrollTop: $("#header").position().top }, 3000).bind("mousewheel", function() { 
return false;  }); 

回答

4

我猜想.unbind("mousewheel")將是事情。所以:

$('html, body').animate({ 
    scrollTop: $("#header").position().top 
}, 3000, function() { 
    $(this).unbind("mousewheel"); 
}).bind("mousewheel", function() { 
    return false; 
}); 

.animate() method允許您提供一個回調函數會在動畫完成後調用,這樣函數中,你可以取消綁定鼠標滾輪的處理程序。

(注:如果您使用jQuery 1.7或更高版本,通常建議使用.on().off(),而不是.bind().unbind(),儘管爲此,他們做同樣的事情)

+0

感謝您偉大的建議! – Kyobul