zoukankan      html  css  js  c++  java
  • 判断滚动条是否到了底部

    判断滚动条是否滚动到底部,需要用到DOM的三个属性值,
    scrollTop为滚动条在Y轴上的滚动距离。
    clientHeight为内容可视区域的高度。
    scrollHeight为内容的总高度

    滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。对于整个HTML文档来说,滚动条是否到达了最底部也是如此判断

     
    //滚动条在Y轴上的滚动距离
    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;
    }
    function touchedBottom(){
       if(getScrollTop() + getWindowHeight() == getScrollHeight()){
        console.log("you are in the bottom!");
        return true;
      }else{
          return false;
       }
    };
  • 相关阅读:
    Docker 系列(四):Docker 容器数据卷简单使用
    【QML 动态对象】使用JS中的语句动态创建和销毁组件
    【QML 动态对象】Loader动态加载组件
    vue-cli2.0全局使用sass变量
    两边+居中 布局
    跳转子路由后左侧菜单跳转为空白页,路由地址出错
    el-tree可搜索单选
    el-tree固定高度加滚动条
    前端 权限控制 方式
    综合分析类
  • 原文地址:https://www.cnblogs.com/winyh/p/6722584.html
Copyright © 2011-2022 走看看