监听滚动条js的操作

来源:互联网 发布:上瘾网络剧发布会视频 编辑:程序博客网 时间:2024/05/18 01:07

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异步加载新数据
    }
});
0 0