js 页面滚动到底部实现上拉加载数据

313 阅读1分钟
        window.onscroll = function () {
            //scrollTop就是触发滚轮事件时滚轮的高度
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            console.log("滚动距离" + scrollTop);
            //变量windowHeight是可视区的高度
            var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
            console.log("可视高度" + windowHeight);
            //变量scrollHeight是滚动条的总高度
            var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            console.log("总高度" + scrollHeight);
            //判断滚动条是否到底部
            if (scrollTop + windowHeight == scrollHeight) {
                //加载数据
                console.log("距顶部" + scrollTop + "可视区高度" + windowHeight + "滚动条总高度" + scrollHeight);
                addData();//所触发的方法名
            }
        }