滾動條滾動到頁面底部繼續加載的處理實例


  這個實例應該說可以很簡單,直接使用jQuery的方法來處理也是可以的。但本文底層使用原生的js來處理,遇到一些小知識點可以分析一下也算有所得。

  原理很簡單,就是為window添加一個scroll事件,瀏覽器每次觸發scroll事件時判斷是否滾動到了瀏覽器底部,如果到了底部則加載新數據。關鍵是計算滾動條是否滾動到了瀏覽器底部,算法如下

  滾動條卷起來的高度 + 窗口高度 > 文檔的總高度 + 50/*我這里將滾動響應區域高度取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;
      /*設置等待條水平居於窗口正中*/
      margin-left: auto;
      margin-right: auto;
      left: 0;
      right: 0;

      /*不能設置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/*滾動響應區域高度取50px*/)){
      waterallowData();
    }
  });

  function waterallowData(){
    $('.waterfllow-loading').addClass('active');
    
    /*$.ajax({
      url:url,
      type:"post",
      data: params,
      success:function(data,textStatus,jQXHR){
        //添加數據
        ...

        //隱藏加載條
        $('.waterfllow-loading.active').removeClass('active');
      }
    });*/
  }

獲取頁面頂部被卷起來的高度函數

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

  chrome瀏覽器和Firefox/IE對滾動條是屬於body還是html理解不同導致處理不同。

獲取頁面文檔的總高度

  //獲取頁面文檔的總高度
  function documentHeight(){
    //現代瀏覽器(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:標准兼容模式關閉。CSS1Compat:標准兼容模式開啟。
  IE對盒模型的渲染在 Standards Mode和Quirks Mode是有很大差別的,在Standards Mode下對於盒模型的解釋和其他的標准瀏覽器是一樣,但在Quirks Mode模式下則有很大差別,而在不聲明Doctype的情況下,IE默認又是Quirks Mode。
  舉個例子說明兩種模式之間的差別有多大。

  當document.compatMode等於"BackCompat"時,瀏覽器客戶區寬度是document.body.clientWidth;

  當document.compatMode等於CSS1Compat時,瀏覽器客戶區寬度是document.documentElement.clientWidth。

  還有其他屬性類似。這里不說了,但是我們可以預見兩種模式導致IE渲染的基石都更改了,可想而知構建出來的建築物差別當有多大。

  所以請為每一個頁面聲明Doctype不僅僅是一個好習慣,而且是一個必要的處理。Quirks Mode可以進垃圾堆了。

  好了下面附上完整的代碼,有一個小例子(沒有后台刷數據,只是顯示等待條)

<!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;
      /*設置等待條水平居於窗口正中*/
      margin-left: auto;
      margin-right: auto;
      left: 0;
      right: 0;

      /*不能設置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(){
    //現代瀏覽器(IE9+和其他瀏覽器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
    return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
  }
  //獲取頁面瀏覽器視口的高度
  function windowHeight(){
    //document.compatMode有兩個取值。BackCompat:標准兼容模式關閉。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/*滾動響應區域高度取50px*/)){
      waterallowData();
    }
  });

  function waterallowData(){
    $('.waterfllow-loading').addClass('active');
    
    /*$.ajax({
      url:url,
      type:"post",
      data: params,
      success:function(data,textStatus,jQXHR){
        //添加數據
        ...

        //隱藏加載條
        $('.waterfllow-loading.active').removeClass('active');
      }
    });*/
  }
  </script>  
</html>
View Code

  里面的加載條圖片為

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM