1.js实现


  1. <style>
  2. * {margin: 0;padding: 0}
  3. body {
  4. height: 2000px;
  5. background: red;
  6. }
  7. div {
  8. height: 1000px;
  9. background: green;
  10. }
  11. </style>
  12. <body>
  13. <div></div>
  14. <script src="lib/base.js"></script>
  15. <!-- 判断滚动条是否到达底部 -->
  16. <script>
  17. window.onscroll = function(){
  18. console.log(onReachBottom())
  19. }
  20. </script>
  21. base.js
  22. function onReachBottom(){
  23. /* 获取滚动区域的高度 */
  24. var dh = document.documentElement.scrollHeight;
  25. var sh = Math.ceil(document.documentElement.scrollTop);
  26. var ah = document.documentElement.clientHeight;
  27. return sh+ah==dh?true:false;
  28. }

2.jquery实现


  1. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  2. <style>
  3. body{
  4. height: 2000px;
  5. background: red;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. <script>
  11. var scrollHeight = $(document).height();
  12. var clientHeight = $(window).height();
  13. $(window).scroll(function(){
  14. var scrollTop = $(document).scrollTop();
  15. if(clientHeight+scrollTop==scrollHeight){
  16. console.log("已经到达底部")
  17. }
  18. })
  19. </script>
  20. </body>
  21. 封装
  22. function onReachBottom() {
  23. var dh = $(document).height();
  24. var sh = $(window).scrollTop();
  25. var wh = $(window).height();
  26. return (Math.ceil(sh + wh) == dh) ? true : false;
  27. }

3.Math


  1. <script>
  2. /* Math.ceil() 上取整
  3. Math.floor() 下取整
  4. Math.round() 四舍五入取整
  5. */
  6. var a = 12.34;
  7. console.log(Math.ceil(a))
  8. console.log(Math.floor(a))
  9. console.log(Math.round(a))
  10. </script>

image.png