开发者

Scrollbar location within a UL

开发者 https://www.devze.com 2023-03-21 10:03 出处:网络
I\'ve looked at other questions but I can\'t seem to find an answer... if($(\"#my-list\").pageYOffset > 541){

I've looked at other questions but I can't seem to find an answer...

if($("#my-list").pageYOffset > 541){
$("#my-list").scrollTo("-=540px");
}

This is my code. As you can see, I'm trying to use jQuery or just some JavaScript to identify where I've scrolled to i开发者_开发知识库n my unordered list (which has a vertical scroll due to overflow:scroll). If this target offset meets a certain threshold, I want it to bump back up to the beginning.

Any help would be much appreciated. Thank you.


Well, pageYOffset gives how far the entire page has been scrolled, but as far as I understand you want to observe the scrolltop offset of #my-list's children (i.e. li) relative to their parent (i.e. the ul). So the function would have to look something like

$("#my-list").scroll(function() {
    if($(this).scrollTop()>541) {
         $(this).scrollTop("0px");   
    }
});

For a working example look at http://jsfiddle.net/LFE2v/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号