1.新建一个lib文件夹

2.建一个base.js的文件(封装)

  1. function onReachBottom(){
  2. var scrollHeight=$(document).height();
  3. var scrollTop=$(document).scrollTop();
  4. var availHeight=$(window).clientHeight;
  5. return (Math.ceil(scrollTop+availHeight)==scrollHeight)?true:false;
  6. }

3.css样式

  1. <script src="lib/base.js"></script>
  2. <style>
  3. *{margin: 0;padding: 0}
  4. body{
  5. height: 1500px;
  6. background: red;
  7. }
  8. div{
  9. height: 1000px;
  10. background: green;
  11. }
  12. </style>
  1. <div>
  2. </div>
  3. <!-- 判断滚动条是否到达底部 -->
  4. <script>
  5. /*原生*/
  6. /* var scrollHeight=document.documentElement.scrollHeight;
  7. console.log(scrollHeight)
  8. window.onscroll=function(){
  9. var scrollTop=Math.ceil(document.documentElement.scrollTop);//四舍五入
  10. var availHeight=document.documentElement.clientHeight; //屏幕的高度
  11. if(scrollTop+availHeight==scrollHeight){
  12. console.log("到达底部")
  13. }
  14. } */
  15. window.onscroll=function(){
  16. console.log(onReachBottom())
  17. }
  18. </script>

4.原生

  1. <div>
  2. </div>
  3. <script>
  4. var scrollHeight=document.documentElement.scrollHeight;
  5. console.log(scrollHeight)
  6. window.onscroll=function(){
  7. var scrollTop=Math.ceil(document.documentElement.scrollTop);//四舍五入
  8. var availHeight=document.documentElement.clientHeight; //屏幕的高度
  9. if(scrollTop+availHeight==scrollHeight){
  10. console.log("到达底部")
  11. }
  12. }
  13. </script>

5.Math

  1. Math.ceil() 上取整
  2. Math.floor() 下取整
  3. Math.round() 四舍五入取整
  1. var a=12.54;
  2. console.log(Math.ceil(a)) //13
  3. console.log(Math.floor(a)) //12
  4. console.log(Math.round(a)) //13