js当滚动条即将到达底部自动加载数据

来源:互联网 发布:node req.query 编辑:程序博客网 时间:2024/04/30 21:26
<script type="text/javascript">    $(window).scroll(function () {        //下面这句主要是获取网页的总高度,主要是考虑兼容性所以把Ie支持的documentElement也写了,这个方法至少支持IE8        var htmlHeight = $(document).height();          //clientHeight是网页在浏览器中的可视高度,        var clientHeight = $(window).height();         //scrollTop滚动条到顶部的垂直高度        var scrollTop = $(document).scrollTop();         //通过判断滚动条的top位置与可视网页之和与整个网页的高度是否相等来决定是否加载内容;        var he = scrollTop + clientHeight;        if (he >= htmlHeight * 0.9) {            addListMore();        }        //console.log("滚动条位置:" + scrollTop);        //console.log("可视高度:" + clientHeight);        //console.log("网页总高度" + htmlHeight);    });    function addListMore() {        console.log("加载更多");    }</script>