监听滚动条js的操作


function getScrollTop(){
    var scrollTop;
    var bodyScrollTop,documentScrollTop;
    if (document.body) {bodyScrollTop = document.body.scrollTop};
    if (document.documentElement) {documentScrollTop = document.documentElement.scrollTop};
    scrollTop = (bodyScrollTop - documentScrollTop)?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;
    var bodyScrollHeight,documentScrollHeight;
     if (document.body) {bodyScrollTop = document.body.scrollHeight};
    if (document.documentElement) {documentScrollTop = document.documentElement.scrollHeight};
    scrollHeight = (bodyScrollHeight - documentScrollHeight)?bodyScrollHeight:documentScrollHeight

    return scrollHeight;

}
//监听事件
$(window).on("scroll", function(){
    //函数内判断,距离底部50px的时候则进行数据加载
    if (getScrollTop() + getWindowHeight() + 50 >= getScrollHeight()) {
        //Ajax异步加载新数据
    }
});

版权声明:本文为sixteen_cicle原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。