最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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,

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论