js捕捉下拉到底部

2018-07-07  本文已影响0人  Nick_4438

前言

通过js代码捕捉网页已经下拉到底部,触发一个js请求。

代码

<!DOCTYPE html>
<html>
<head>
  <title>拉到底部测试</title>
  <meta charset="gbk">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    .item{
      width: 300px;
      height: 100px;
      background: #333333;
      margin-top: 10px;
    }
  </style>
<body>
<div data-role="page" data-theme="b">
  <div data-role="header"></div>
  <div data-role="content">
    <script>
      //文档高度
      function getDocumentTop() {
        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 getWindowHeight() {
        var windowHeight = 0;
        if (document.compatMode == "CSS1Compat") {
          windowHeight = document.documentElement.clientHeight;
        } else {
          windowHeight = document.body.clientHeight;
        }
        return windowHeight;
      }

      //滚动条滚动高度
      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;
      }

      window.onscroll = function () {
        console.log(getScrollHeight() +"=?" + getWindowHeight() +"+" + getDocumentTop())
        if(getScrollHeight() == getWindowHeight() + getDocumentTop()){
          console.log("底裤出来了")
        }
      }
    </script>
    <div class="list">
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    </div>
  </div>
</body>
</html>

执行效果

image.png
上一篇 下一篇

猜你喜欢

热点阅读