如何发现用户是否使用jQuery滚动到HTML容器的末尾

How to find out if the user scrolled to the end of a HTML container with jQuery?

本文关键字:HTML 滚动 jQuery 何发现 发现 是否 用户      更新时间:2023-09-26

如何确定用户在可滚动容器中是向上滚动还是向下滚动?

jQuery提供任何机制吗?

css:

#container {
    display: block;
    width: 250px;
    height: 25px;
    background: green;
}
#scrolling {
    width: 250px;
    height: 300px;
    backround: red;
    overflow: auto;
}

http://jsfiddle.net/Hmaf2/2/

非常感谢!

Pat

$('#somediv').scrollTop()

会告诉你顶部的位置

-编辑-

$('#somediv').scroll(function(){
    if($('#somediv').scrollTop()==0){
        console.log('top')
    }
})

是的,您可以访问可滚动容器的当前滚动顶部值。

jsFiddle在这里工作。

$('#scrolling').scroll(function() {
   var scrollTop = $(this).scrollTop();
   console.log(scrollTop);
});​

您可以通过比较div:的高度、可滚动高度和滚动偏移量来测试滚动位置

$(document).ready(function(){
    $('#scrolling').scroll(function(){
        var scrollTop = $(this).scrollTop();
        var iheight = $(this).innerHeight();
        var sheight = this.scrollHeight;
        var text = '';
        if (scrollTop <= 5) {
            text = 'top';
        }
        else if (scrollTop+5 >= sheight-iheight) {
            text = 'bottom';
        }
        else {
            text = scrollTop+' middle ('+iheight+','+sheight+')';
        }
        $('#result').text(text);
    });
});

小提琴
本例从div的页边空白的顶部和底部保留了5个像素。