2016-06-21 125 views
0

我試圖檢測用戶何時滾動到頁面的最底部。爲什麼文檔高度等於窗口內部高度+ pageYoffset?

,我結束了在以下

var windowHeight = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight; 
var body = document.body, html = document.documentElement; 
var docHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight); 
var windowBottom = windowHeight + window.pageYOffset; 

if (windowBottom >= docHeight) { 
    // Bottom is reached 
} 

我很困惑,爲什麼這個工程

從文檔,innerheight是認爲門戶的高度解決方案,與pageYoffset是量滾動我們製作。

因此,爲了檢測滾動到底部,我不應該檢查,如果pageYOffset >= documentHeight

爲什麼我必須添加innerheight

回答

1

這與Angular無關。這就是所有瀏覽器的工作原理。

您的pageYOffset永遠不會比文檔高度大,因爲它代表視口的頂部。爲使pageYOffset等於文檔高度,您必須滾動頁面末尾。

最大pageYOffset可以是docHeight - window.innerHeight。所以你的支票可能是:

if (pageYOffset >= docHeight - window.innerHeight) { 
    // Bottom is reached 
} 
相關問題