2012-10-03 24 views
-1

我想要一個彈出窗口,當用戶達到某個文檔高度時顯示。例如,當閱讀器接近〜後彈出一個可以觸發問他/她的輸入端(評論是否喜歡交與否)我 試圖讀取/通過傳遞文件的高度:不斷提取滾動條的垂直位置

function doch() { 
    var currentscrollpos; 
    if (currentscrollpos > 1500) { 
     //trigger popup 
     alert(currentscrollpos); 
    } 
    setInterval(function() { 
     currentscrollpos = $(document).scrollTop(); 
     return currentscrollpos; 
    }, 3e3); 
}​ 

在HTML功能,可只需通過doch()觸發;

的問題是,currentscrollpos的價值不能從setInterval函數傳遞給如果循環

回答

0

爲什麼在用戶向下滾動頁面時可以綁定到setInterval?

$(window).scroll(function(){ //when a user scrolls 
    if(this.pageYOffset + $(this).height() > $(document).height() - 200){ //if the scroll is within 200 px of the bottom of the page 
     $('div').css('background', 'red'); //do popup 
    } 
}); 

http://jsfiddle.net/rUbwq/2/

+0

感謝您的簡潔答案。然而,作爲學習者,我可能會問爲什麼在我的代碼中currentcrollpos的值不會傳遞給if循環? – nightcrawler

+0

另外我想知道爲什麼使用$(this).height()而不是this.height() – nightcrawler