2012-04-02 32 views
0

我怎樣才能編輯本只運行一次後用戶滾動onscroll頂部一次

(function() { 
    var previousScroll = 0; 

    $(window).scroll(function(){ 
     var currentScroll = $(this).scrollTop(); 
     if (currentScroll < previousScroll){ 
      alert('up'); 
     } 
     previousScroll = currentScroll; 
    }); 
}()); //run this anonymous function immediately 
+0

你是指只運行一次? – 2012-04-02 14:49:03

+0

對不起,對不起 – 2012-04-02 14:52:11

+0

您想要做什麼而不是那裏的警報聲明?你想找到其他地方用戶是否滾動嗎? – 2012-04-02 14:54:15

回答

1

,如果你希望用戶只需進行一次通知,然後你可以只使用一個布爾值來跟蹤是否通知已經完成了。

(function() { 
var previousScroll = 0; 
var isAlerted = false; 
$(window).scroll(function(){ 
    if(isAlerted) return;      
    var currentScroll = $(this).scrollTop(); 
    if (currentScroll < previousScroll){ 
     console.log('up'); 
     isAlerted = true; 
    } 
    previousScroll = currentScroll; 
}); 
})();​ 

另請注意最後一行中的更改。正確使用自行執行的匿名功能,因爲

(function(){})(); 
+0

thx它的工作,但不工作時,我改變console.log('up');我的功能 – 2012-04-02 15:30:50

+0

你可以在jsfiddle.net發表示例代碼 – 2012-04-02 15:35:11

+0

http://jsfiddle.net/bT25A/ – 2012-04-02 15:44:23