2012-12-28 252 views
0

我做了一個小jquery腳本。當你在頁面上滾動,你的結果超過350.比按鈕顯示。當你回去時,該按鈕被隱藏。這是腳本:jquery停止滾動功能

var button = $('a[title*="terug naar boven"]'); 

$(document).bind('scroll', function(e) { 
    if (window.scrollY > 350) { 
     button.animate ({ 
      opacity: 1 
     }) 
    } 
    if (window.scrollY < 350) { 
     button.animate ({ 
      opacity: 0 
     }) 
    } 
}); 

但問題是。腳本繼承是。當我滾動。腳本每次都會觸發。我怎樣才能解決這個問題?這是腳本一次點燃。

+0

參考http://stackoverflow.com/questions/4244841/how-to-know-the-end-of-scrolling-event-for-a-div-tag – Ankur

+0

你想你的腳本運行只有一次? –

回答

0

爲了防止按鈕在已經處於正確狀態(顯示或隱藏)時被激活,您應該保持按鈕的狀態。

您可以使用一個變量來做到這一點

var buttonState = 0; 
$(document).bind('scroll', function(e) { 
    if (window.scrollY > 350 && buttonState == 0) { 
     buttonState = 1; 
     button.animate ({ 
      opacity: 1 
     }) 
    } 
    if (window.scrollY < 350 && buttonState == 1) { 
     buttonState = 0; 
     button.animate ({ 
      opacity: 0 
     }) 
    } 
}); 

或者,如果你想只運行一次這個,你也可以使用該按鈕的不透明度水平狀態

,你可以取消綁定的處理程序事件

$(document).bind('scroll', function(e) { 
    if (window.scrollY > 350) { 
     button.animate ({ 
      opacity: 1 
     }) 
    } 
    if (window.scrollY < 350) { 
     button.animate ({ 
      opacity: 0 
     }) 
    } 
    $(document).unbind('scroll'); 
});