zoukankan      html  css  js  c++  java
  • IE,火狐,谷歌浏览器下js判断滚动条是否已拉到页面最底部

    E/FF/Chrome下document.documentElement和document.body的 scrollHeight/scrollTop/clientHeight 以及判断滚动条是否已拉到页面最底部

    DTD已声明 
    IE
    document.documentElement.scrollHeight  浏览器所有内容高度 ,document.body.scrollHeight  浏览器所有内容高度
    document.documentElement.scrollTop  浏览器滚动部分高度,document.body.scrollTop 始终为0
    document.documentElement.clientHeight  浏览器可视部分高度,document.body.clientHeight  浏览器所有内容高度
    FF
    document.documentElement.scrollHeight  浏览器所有内容高度 ,document.body.scrollHeight  浏览器所有内容高度
    document.documentElement.scrollTop  浏览器滚动部分高度,document.body.scrollTop 始终为0
    document.documentElement.clientHeight 浏览器可视部分高度,document.body.clientHeight  浏览器所有内容高度
    Chrome
    document.documentElement.scrollHeight  浏览器所有内容高度, document.body.scrollHeight  浏览器所有内容高度
    document.documentElement.scrollTop 始终为0,document.body.scrollTop  浏览器滚动部分高度
    document.documentElement.clientHeight  浏览器可视部分高度,document.body.clientHeight  浏览器所有内容高度
    DTD未声明
    IE
    document.documentElement.scrollHeight  浏览器可视部分高度,document.body.scrollHeight  浏览器所有内容高度
    document.documentElement.scrollTop 始终为0,document.body.scrollTop  浏览器滚动部分高度
    document.documentElement.clientHeight 始终为0,document.body.clientHeight  浏览器可视部分高度
    FF
    document.documentElement.scrollHeight  浏览器可视部分高度, document.body.scrollHeight 浏览器所有内容高度
    document.documentElement.scrollTop 始终为0,document.body.scrollTop 浏览器滚动部分高度
    document.documentElement.clientHeight 浏览器所有内容高度,document.body.clientHeight 浏览器可视部分高度
    Chrome
    document.documentElement.scrollHeight 浏览器可视部分高度,document.body.scrollHeight 浏览器所有内容高度
    document.documentElement.scrollTop 始终为0,document.body.scrollTop 浏览器滚动部分高度
    document.documentElement.clientHeight 浏览器所有内容高度,document.body.clientHeight 浏览器可视部分高度

    浏览器所有内容高度即浏览器整个框架的高度,包括滚动条卷去部分+可视部分+底部隐藏部分的高度总和

    浏览器滚动部分高度即滚动条卷去部分高度即可视顶端距离整个对象顶端的高度。
    综上

    1、document.documentElement.scrollTop和document.body.scrollTop始终有一个为0,所以可以用这两个的和来求scrollTop

    2、scrollHeight、clientHeight 在DTD已声明的情况下用documentElement,未声明的情况下用body

    3、document.documentElement.scrollTop在未声明的情况下始终为0,所以可以用来判断是否声明了DTD;

    所以,判断滚动条是否已拉到页面最底部,可以用如下代码

    <script>
        window.onscroll = function (){
            var marginBot = 0;
            if (document.documentElement.scrollTop){
                marginBot = document.documentElement.scrollHeight - (document.documentElement.scrollTop+document.body.scrollTop)-document.documentElement.clientHeight;
            } else {
                marginBot = document.body.scrollHeight - document.body.scrollTop - document.body.clientHeight;
            }
            if(marginBot<=0) {
                //do something
                alert('正在请求数据');
            }
        }
    </script>
  • 相关阅读:
    指定一个M3U8文件,判断它包含的TS文件是不是都存在。指定一个Office生成的Swf文件,判断它包含的Swf文件是不是完整都存在。
    东师理想云平台异步任务处理系统V2.0重构思路
    Mysql查询出所有列名
    IntelliJ IDEA添加jar包
    IDEA下利用Jrebel插件实现JFinal项目main方法【热加载】
    整理OpenResty+Mysql+Tomcat+JFinal+Cannal+HUI
    有没有 linux 命令可以获取我的公网 ip, 类似 ip138.com 上获取的 ip?
    MongoDB学习笔记(5)--document
    MongoDB学习笔记(4)--collection
    MongoDB学习笔记(3)--删除数据库
  • 原文地址:https://www.cnblogs.com/phpfensi/p/3852736.html
Copyright © 2011-2022 走看看