简体   繁体   English

jQuery检测用户何时滚动到列表顶部

[英]jquery detect when user scrolled to the top of list

I have dynamical list 我有动态清单

<div id="content" class="content" data-role="content" data-scroll="y">
    <div id="myDiv">
        <form>
            <ul id="myList" data-role="listview">
            </ul>
        </form>
    </div>
</div>

And I want to append another list to myList , when you scrolled it to the top; 当您滚动到顶部时,我想将另一个列表附加到myList上; How can I detect when user scrolled it to the top? 我如何检测用户何时将其滚动到顶部?

I tried some variants of scrollTop function, like: 我尝试了scrollTop函数的一些变体,例如:

  $(window).bind('scrollstop', function(){
    alert("1 "+$(window).scrollTop());  
    alert("2 "+$('#content').scrollTop()); 
    alert("3 "+$('#myDiv').scrollTop()); 
    alert("4 "+$('#myList').scrollTop()); 
    alert("5 "+$('#myList li').scrollTop()); 
});

but all of them returned 0 :( 但他们所有人都返回了0 :(

You need to attach the scroll event to the element being scrolled. 您需要将scroll事件附加到要滚动的元素。 Assuming you are scrolling the #content element this will work: 假设您正在滚动#content元素,它将起作用:

$('#content').on('scroll', function(){
    if ($(this).scrollTop() == 0) {
        alert('at the top');
    }
});

Example fiddle 小提琴的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM