這篇文章主要介紹了jQuery scroll事件實現監控滾動條分頁簡單示例,使用ajax加載,同時介紹了(document).height()與$(window).height()的區別,須要的朋友能夠參考下。ajax
scroll事件適用於window對象,但也可滾動iframe框架與CSS overflow屬性設置爲scroll的元素。瀏覽器
1 $(document).ready(function () { //本人習慣這樣寫了 2 $(window).scroll(function () { 3 //$(window).scrollTop()這個方法是當前滾動條滾動的距離 4 //$(window).height()獲取當前窗體的高度 5 //$(document).height()獲取當前文檔的高度 6 var bot = 50; //bot是底部距離的高度 7 if ((bot + $(window).scrollTop()) >= ($(document).height() - $(window).height())) { 8 //當底部基本距離+滾動的高度〉=文檔的高度-窗體的高度時; 9 //咱們須要去異步加載數據了 10 $.getJSON("url", { page: "2" }, function (str) { alert(str); }); 11 } 12 }); 13 });
注意:(window).height()和(document).height()的區別框架
jQuery(window).height()表明了當前可見區域的大小,而jQuery(document).height()則表明了整個文檔的高度,可視具體狀況使用.異步
注意當瀏覽器窗口大小改變時(如最大化或拉大窗口後) jQuery(window).height() 隨之改變,可是jQuery(document).height()是不變的。url
$(document).scrollTop() //獲取垂直滾動的距離 即當前滾動的地方的窗口頂端到整個頁面頂端的距離 $(document).scrollLeft() //這是獲取水平滾動條的距離
要獲取頂端 只須要獲取到scrollTop()==0的時候 就是頂端了spa
要獲取底端 只要獲取scrollTop()>=$(document).height()-$(window).height() 就能夠知道已經滾動到底端了.net
$(document).height() //是獲取整個頁面的高度 $(window).height() //是獲取當前 也就是你瀏覽器所能看到的頁面的那部分的高度 這個大小在你縮放瀏覽器窗口大小時 會改變 與document是不同的 根據英文應該也能理解吧
本身作個實驗就知道了code
$(document).scroll(function(){ $("#lb").text($(document).scrollTop()); }) <span id="lb" style="top:100px;left:100px;position:fixed;"></span><!--一個固定的span標記 滾動時方便查看-->