欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到頁(yè)面底部繼續(xù)加載

 更新時(shí)間:2021年06月09日 12:00:01   作者:chua1989  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)滾動(dòng)條滾動(dòng)到頁(yè)面底部繼續(xù)加載,原理很簡(jiǎn)單,就是為window添加一個(gè)scroll事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

這個(gè)實(shí)例應(yīng)該說(shuō)可以很簡(jiǎn)單,直接使用jQuery的方法來(lái)處理也是可以的。但本文底層使用原生的js來(lái)處理,遇到一些小知識(shí)點(diǎn)可以分析一下也算有所得。

原理很簡(jiǎn)單,就是為window添加一個(gè)scroll事件,瀏覽器每次觸發(fā)scroll事件時(shí)判斷是否滾動(dòng)到了瀏覽器底部,如果到了底部則加載新數(shù)據(jù)。關(guān)鍵是計(jì)算滾動(dòng)條是否滾動(dòng)到了瀏覽器底部,算法如下

滾動(dòng)條卷起來(lái)的高度 + 窗口高度 > 文檔的總高度 + 50/*我這里將滾動(dòng)響應(yīng)區(qū)域高度取50px*/;如果這個(gè)判斷為true則表示滾動(dòng)條滾動(dòng)到了底部。

實(shí)例

 <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">
 //圖片查詢中正對(duì)瀏覽器主頁(yè)面滾動(dòng)事件處理(瀑布流)。只能使用window方式綁定,使用document方式不起作用
 $(window).on('scroll',function(){
 if(scrollTop() + windowHeight() >= (documentHeight() - 50/*滾動(dòng)響應(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');
  }
 });*/
 }

獲取頁(yè)面頂部被卷起來(lái)的高度函數(shù)

 //獲取頁(yè)面頂部被卷起來(lái)的高度
 function scrollTop(){
 return Math.max(
  //chrome
  document.body.scrollTop,
  //firefox/IE
  document.documentElement.scrollTop);
 }

chrome瀏覽器和Firefox/IE對(duì)滾動(dòng)條是屬于body還是html理解不同導(dǎo)致處理不同。

獲取頁(yè)面文檔的總高度

 //獲取頁(yè)面文檔的總高度
 function documentHeight(){
 //現(xiàn)代瀏覽器(IE9+和其他瀏覽器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
 return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
 }

這個(gè)算法和jQuery計(jì)算文檔高度的方法一致。

獲取頁(yè)面瀏覽器視口的高度

 function windowHeight(){
 return (document.compatMode == "CSS1Compat")?
 document.documentElement.clientHeight:
 document.body.clientHeight;
 }

這里需要說(shuō)明的是document.compatMode這個(gè)東東。很陌生,一般情況貌似沒(méi)有遇到過(guò)。

document.compatMode有兩個(gè)取值"BackCompat"和"CSS1Compat"。官方解釋是BackCompat:標(biāo)準(zhǔn)兼容模式關(guān)閉。CSS1Compat:標(biāo)準(zhǔn)兼容模式開(kāi)啟。

IE對(duì)盒模型的渲染在 Standards Mode和Quirks Mode是有很大差別的,在Standards Mode下對(duì)于盒模型的解釋和其他的標(biāo)準(zhǔn)瀏覽器是一樣,但在Quirks Mode模式下則有很大差別,而在不聲明Doctype的情況下,IE默認(rèn)又是Quirks Mode。

舉個(gè)例子說(shuō)明兩種模式之間的差別有多大。

當(dāng)document.compatMode等于"BackCompat"時(shí),瀏覽器客戶區(qū)寬度是document.body.clientWidth;

當(dāng)document.compatMode等于CSS1Compat時(shí),瀏覽器客戶區(qū)寬度是document.documentElement.clientWidth。

還有其他屬性類(lèi)似。這里不說(shuō)了,但是我們可以預(yù)見(jiàn)兩種模式導(dǎo)致IE渲染的基石都更改了,可想而知構(gòu)建出來(lái)的建筑物差別當(dāng)有多大。

所以請(qǐng)為每一個(gè)頁(yè)面聲明Doctype不僅僅是一個(gè)好習(xí)慣,而且是一個(gè)必要的處理。Quirks Mode可以進(jìn)垃圾堆了。

好了下面附上完整的代碼,有一個(gè)小例子(沒(méi)有后臺(tái)刷數(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">

 //獲取頁(yè)面頂部被卷起來(lái)的高度
 function scrollTop(){
 return Math.max(
  //chrome
  document.body.scrollTop,
  //firefox/IE
  document.documentElement.scrollTop);
 }
 //獲取頁(yè)面文檔的總高度
 function documentHeight(){
 //現(xiàn)代瀏覽器(IE9+和其他瀏覽器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
 return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
 }
 //獲取頁(yè)面瀏覽器視口的高度
 function windowHeight(){
 //document.compatMode有兩個(gè)取值。BackCompat:標(biāo)準(zhǔn)兼容模式關(guān)閉。CSS1Compat:標(biāo)準(zhǔn)兼容模式開(kāi)啟。
 return (document.compatMode == "CSS1Compat")?
 document.documentElement.clientHeight:
 document.body.clientHeight;
 }
 </script>
 <script type="text/javascript">
 //圖片查詢中正對(duì)瀏覽器主頁(yè)面滾動(dòng)事件處理(瀑布流)。只能使用window方式綁定,使用document方式不起作用
 $(window).on('scroll',function(){
 if(scrollTop() + windowHeight() >= (documentHeight() - 50/*滾動(dòng)響應(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>

里面的加載條圖片為

以上就是滾動(dòng)條滾動(dòng)到頁(yè)面底部繼續(xù)加載的處理實(shí)例,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論