国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實(shí)現(xiàn)判斷滾動(dòng)條到底部

2019-11-20 12:11:37
字體:
供稿:網(wǎng)友

判斷滾動(dòng)條到底部,需要用到DOM的三個(gè)屬性值,即scrollTop、clientHeight、scrollHeight。

scrollTop為滾動(dòng)條在Y軸上的滾動(dòng)距離。

clientHeight為內(nèi)容可視區(qū)域的高度。

scrollHeight為內(nèi)容可視區(qū)域的高度加上溢出(滾動(dòng))的距離。

從這個(gè)三個(gè)屬性的介紹就可以看出來,滾動(dòng)條到底部的條件即為scrollTop + clientHeight == scrollHeight。

廢話不多少說,趕緊上代碼(兼容不同的瀏覽器)。

lazyload.js

//滾動(dòng)條在Y軸上的滾動(dòng)距離  function getScrollTop(){  var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;  if(document.body){    bodyScrollTop = document.body.scrollTop;  }  if(document.documentElement){    documentScrollTop = document.documentElement.scrollTop;  }  scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;  return scrollTop;}  //文檔的總高度  function getScrollHeight(){  var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;  if(document.body){    bodyScrollHeight = document.body.scrollHeight;  }  if(document.documentElement){    documentScrollHeight = document.documentElement.scrollHeight;  }  scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;  return scrollHeight;}  //瀏覽器視口的高度  function getWindowHeight(){  var windowHeight = 0;  if(document.compatMode == "CSS1Compat"){    windowHeight = document.documentElement.clientHeight;  }else{    windowHeight = document.body.clientHeight;  }  return windowHeight;}  window.onscroll = function(){  if(getScrollTop() + getWindowHeight() == getScrollHeight()){    alert("you are in the bottom!");  }};

lazyload-jQuery.js

$(window).scroll(function(){  var scrollTop = $(this).scrollTop();  var scrollHeight = $(document).height();  var windowHeight = $(this).height();  if(scrollTop + windowHeight == scrollHeight){    alert("you are in the bottom");  }});

lazyLoad.html

<!doctype html><html lang="en" style="height:900px;"> <head> <meta charset="UTF-8"> <meta name="Author" content="forever"> <link rel="stylesheet" href="css/lazyload.css" /> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <title>lazyLoad</title> <script type="text/javascript">     $(function(){    var $ul=$("#lazyLoadWrap").find("ul");    $(window).scroll(function(){        var scrollTop = $(this).scrollTop();        var scrollHeight = $(document).height();        var windowHeight = $(this).height();       if(scrollTop + windowHeight == scrollHeight){          for(var i=0;i<6;i++){            $ul.append("<li>Hello</li>");          }       }    });  }); </script> </head> <body>  <div id="lazyLoadWrap">    <ul>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li></li>      <li>12</li>    </ul>  </div> </body></html>

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿城市| 临沂市| 南宫市| 怀化市| 新龙县| 句容市| 聂荣县| 衡东县| 申扎县| 庆阳市| 台南市| 哈密市| 镇巴县| 开阳县| 灌阳县| 龙海市| 琼海市| 上饶市| 含山县| 石楼县| 中江县| 长宁区| 会泽县| 山西省| 长垣县| 丁青县| 霸州市| 全椒县| 钟祥市| 怀集县| 盈江县| 谢通门县| 定边县| 遂昌县| 开化县| 舟山市| 乌拉特后旗| 仪征市| 新干县| 维西| 岳西县|