javascript - 移动浏览器中内容的高度超过视口时,没有设置overflow-y:scroll ,当窗口移动时时如何监听视口到了内容底部
黄舟
黄舟 2017-04-10 17:10:43
[JavaScript讨论组]

1 主要是为了无限加载
2 设置overflow-y:scroll的滚动条奇丑无比

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
ringa_lee

首先给出3个函数:

        function getScrollTop() {
            //滚动条在Y轴上的滚动距离
            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).on("scroll", function(){
    //函数内判断,距离底部50px的时候则进行数据加载
    if (getScrollTop() + getWindowHeight() + 50 >= getScrollHeight()) {
        //Ajax异步加载新数据
    }
});

---------更新
关于滚动条丑的问题,如果是ios可以不用管了,如果是安卓,可以考虑使用第三方滚动条框架iscroll,github或者百度搜索即可找到

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号