javascript - Using jQuery to detect scroll distance from the top of a div, not from top of page - Stack Overflow

So here's the deal. I have a div (call it div.container for troubleshooting purposes) on my page t

So here's the deal. I have a div (call it div.container for troubleshooting purposes) on my page that will hold dynamically loaded content from a database (loaded using PHP). I want to detect the user's scroll position from the top of div.container, not from the top of the page. Note, div.container is not positioned at the top of the page, but is a child div with a position roughly 50px from the top of the page. I am looking to get the scroll distance in pixels.

I tried this with zero success:

$("div.container").scrollTop();

Other than that, I really have no idea where to start with this. Any help is so greatly appreciated. Thanks much.

So here's the deal. I have a div (call it div.container for troubleshooting purposes) on my page that will hold dynamically loaded content from a database (loaded using PHP). I want to detect the user's scroll position from the top of div.container, not from the top of the page. Note, div.container is not positioned at the top of the page, but is a child div with a position roughly 50px from the top of the page. I am looking to get the scroll distance in pixels.

I tried this with zero success:

$("div.container").scrollTop();

Other than that, I really have no idea where to start with this. Any help is so greatly appreciated. Thanks much.

Share Improve this question asked Dec 16, 2013 at 5:26 Alix PërryAlix Përry 4892 gold badges7 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You need to get the window's scrollTop() and subtract the '.container' offset() top value:

$(window).scroll(function(){
    var posTop = $(window).scrollTop() - $('.container').offset().top
});

See this jsfiddle demo.

use the offset to caculate the child div 's distance from the parent div,

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744835236a4596223.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信