這個實例應(yīng)該說可以很簡單,直接使用jQuery的方法來處理也是可以的。但本文底層使用原生的js來處理,遇到一些小知識點可以分析一下也算有所得。
原理很簡單,就是為window添加一個scroll事件,瀏覽器每次觸發(fā)scroll事件時判斷是否滾動到了瀏覽器底部,如果到了底部則加載新數(shù)據(jù)。關(guān)鍵是計算滾動條是否滾動到了瀏覽器底部,算法如下
滾動條卷起來的高度 + 窗口高度 > 文檔的總高度 + 50/*我這里將滾動響應(yīng)區(qū)域高度取50px*/;如果這個判斷為true則表示滾動條滾動到了底部。
實例
<style type="text/css"> html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{ margin: 0; padding:0; } *{ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .waterfllow-loading{ z-index: 2000; display:none; } .waterfllow-loading.active{ display:block; } .waterfllow-loading img.loading-progress{ position: fixed; /*設(shè)置等待條水平居于窗口正中*/ margin-left: auto; margin-right: auto; left: 0; right: 0; /*不能設(shè)置margin-top:auto和margin-bottom:auto否則IE下bottom就不頂用了*/ bottom: 30px; } </style> <div class="waterfllow-loading"> <img class="loading-progress" src="busy.gif"> </div> <script type="text/javascript"> //圖片查詢中正對瀏覽器主頁面滾動事件處理(瀑布流)。只能使用window方式綁定,使用document方式不起作用 $(window).on('scroll',function(){ if(scrollTop() + windowHeight() >= (documentHeight() - 50/*滾動響應(yīng)區(qū)域高度取50px*/)){ waterallowData(); } }); function waterallowData(){ $('.waterfllow-loading').addClass('active'); /*$.ajax({ url:url, type:"post", data: params, success:function(data,textStatus,jQXHR){ //添加數(shù)據(jù) ... //隱藏加載條 $('.waterfllow-loading.active').removeClass('active'); } });*/ }
獲取頁面頂部被卷起來的高度函數(shù)
//獲取頁面頂部被卷起來的高度 function scrollTop(){ return Math.max( //chrome document.body.scrollTop, //firefox/IE document.documentElement.scrollTop); }
chrome瀏覽器和Firefox/IE對滾動條是屬于body還是html理解不同導(dǎo)致處理不同。
獲取頁面文檔的總高度
//獲取頁面文檔的總高度 function documentHeight(){ //現(xiàn)代瀏覽器(IE9+和其他瀏覽器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以 return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight); }
這個算法和jQuery計算文檔高度的方法一致。
獲取頁面瀏覽器視口的高度
function windowHeight(){ return (document.compatMode == "CSS1Compat")? document.documentElement.clientHeight: document.body.clientHeight; }
這里需要說明的是document.compatMode這個東東。很陌生,一般情況貌似沒有遇到過。
document.compatMode有兩個取值"BackCompat"和"CSS1Compat"。官方解釋是BackCompat:標準兼容模式關(guān)閉。CSS1Compat:標準兼容模式開啟。
IE對盒模型的渲染在 Standards Mode和Quirks Mode是有很大差別的,在Standards Mode下對于盒模型的解釋和其他的標準瀏覽器是一樣,但在Quirks Mode模式下則有很大差別,而在不聲明Doctype的情況下,IE默認又是Quirks Mode。
舉個例子說明兩種模式之間的差別有多大。
當document.compatMode等于"BackCompat"時,瀏覽器客戶區(qū)寬度是document.body.clientWidth;
當document.compatMode等于CSS1Compat時,瀏覽器客戶區(qū)寬度是document.documentElement.clientWidth。
還有其他屬性類似。這里不說了,但是我們可以預(yù)見兩種模式導(dǎo)致IE渲染的基石都更改了,可想而知構(gòu)建出來的建筑物差別當有多大。
所以請為每一個頁面聲明Doctype不僅僅是一個好習(xí)慣,而且是一個必要的處理。Quirks Mode可以進垃圾堆了。
好了下面附上完整的代碼,有一個小例子(沒有后臺刷數(shù)據(jù),只是顯示等待條)
<!DOCTYPE html><html lang="ch-cn"> <head> <meta charset="utf-8"> <script type="text/javascript" src='jquery-1.9.1.js'></script> <style type="text/css"> html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{ margin: 0; padding:0; } *{ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .waterfllow-loading{ z-index: 2000; display:none; } .waterfllow-loading.active{ display:block; } .waterfllow-loading img.loading-progress{ position: fixed; /*設(shè)置等待條水平居于窗口正中*/ margin-left: auto; margin-right: auto; left: 0; right: 0; /*不能設(shè)置margin-top:auto和margin-bottom:auto否則IE下bottom就不頂用了*/ bottom: 30px; } </style> </head> <body style="background:#ff0;height:1000px;"> <div class="waterfllow-loading"> <img class="loading-progress" src="busy.gif"> </div> </body> <script type="text/javascript"> //獲取頁面頂部被卷起來的高度 function scrollTop(){ return Math.max( //chrome document.body.scrollTop, //firefox/IE document.documentElement.scrollTop); } //獲取頁面文檔的總高度 function documentHeight(){ //現(xiàn)代瀏覽器(IE9+和其他瀏覽器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以 return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight); } //獲取頁面瀏覽器視口的高度 function windowHeight(){ //document.compatMode有兩個取值。BackCompat:標準兼容模式關(guān)閉。CSS1Compat:標準兼容模式開啟。 return (document.compatMode == "CSS1Compat")? document.documentElement.clientHeight: document.body.clientHeight; } </script> <script type="text/javascript"> //圖片查詢中正對瀏覽器主頁面滾動事件處理(瀑布流)。只能使用window方式綁定,使用document方式不起作用 $(window).on('scroll',function(){ if(scrollTop() + windowHeight() >= (documentHeight() - 50/*滾動響應(yīng)區(qū)域高度取50px*/)){ waterallowData(); } }); function waterallowData(){ $('.waterfllow-loading').addClass('active'); /*$.ajax({ url:url, type:"post", data: params, success:function(data,textStatus,jQXHR){ //添加數(shù)據(jù) ... //隱藏加載條 $('.waterfllow-loading.active').removeClass('active'); } });*/ } </script> </html>
里面的加載條圖片為
以上就是滾動條滾動到頁面底部繼續(xù)加載的處理實例,希望對大家的學(xué)習(xí)有所幫助。
新聞熱點
疑難解答