Heim > Web-Frontend > js-Tutorial > Hauptteil

js实现滚动条滚动到页面底部继续加载_javascript技巧

WBOY
Freigeben: 2016-05-16 15:24:38
Original
1638 Leute haben es durchsucht

这个实例应该说可以很简单,直接使用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" alt="js实现滚动条滚动到页面底部继续加载_javascript技巧" >
  </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');
   }
  });*/
 }

Nach dem Login kopieren

获取页面顶部被卷起来的高度函数

 //获取页面顶部被卷起来的高度
 function scrollTop(){
  return Math.max(
   //chrome
   document.body.scrollTop,
   //firefox/IE
   document.documentElement.scrollTop);
 }
Nach dem Login kopieren

  chrome浏览器和Firefox/IE对滚动条是属于body还是html理解不同导致处理不同。

获取页面文档的总高度

 //获取页面文档的总高度
 function documentHeight(){
  //现代浏览器(IE9+和其他浏览器)和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
  return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
 }
Nach dem Login kopieren

  这个算法和jQuery计算文档高度的方法一致。

获取页面浏览器视口的高度

 function windowHeight(){
  return (document.compatMode == "CSS1Compat")&#63;
  document.documentElement.clientHeight:
  document.body.clientHeight;
 }
Nach dem Login kopieren

  这里需要说明的是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" alt="js实现滚动条滚动到页面底部继续加载_javascript技巧" >
  </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")&#63;
  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>
Nach dem Login kopieren

里面的加载条图片为

以上就是滚动条滚动到页面底部继续加载的处理实例,希望对大家的学习有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!