jQuery检测滚动条到达顶部或底部

jQuery获取位置和尺寸相关函数:

函数名 作用
$(document).height() 获取整个页面的高度。
$(window).height() 获取当前也就是浏览器所能看到的页面的那部分的高度。这个大小在你缩放浏览器窗口大小时会改变,与document是不一样的。
scrollTop() 获取匹配元素相对滚动条顶部的偏移。
scrollLeft() 获取匹配元素相对滚动条左侧的偏移。
scroll([[data],fn]) 当滚动条发生变化时触犯scroll事件
$(document).ready(function() {
    $(window).scroll(function() {
        if ($(document).scrollTop()<=0){
            alert("滚动条已经到达顶部为0");
        }

        if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
            alert("滚动条已经到达底部为" + $(document).scrollTop());
        }
    });
});

本文转自:http://www.phpddt.com/dhtml/jquery-scroll.html

posted @ 2016-04-19 09:21  chenjiacheng  阅读(266)  评论(0编辑  收藏  举报