1

we usually use $(window).scrollTop() to detect how far the page has been scrolled. Is there a equivalent for scrollable elements?

Say I set html to overflow hidden, and body to overflow scroll, now I want to detect $('body').scrollTop() I will always get 0.

P.S. this is useful for mobile site development.

Here is a plunker:

http://embed.plnkr.co/jZH3zCiMSfk5bIZIoDEp/

7
  • elm.scrollTop is a property of every element Commented Mar 10, 2015 at 15:07
  • 1
    what are you trying to do? Very rare to ever get body scrolling unless set height and overflow hidden on <html> which isn't common Commented Mar 10, 2015 at 15:13
  • @charlietfl To eliminate using fixed position because it causes issue across angular material framework. Commented Mar 10, 2015 at 15:14
  • 1
    Could it be a XY problem? mywiki.wooledge.org/XyProblem Instead i guess you'd have better to ask question regarding your issue across angular material framework Commented Mar 10, 2015 at 15:14
  • 1
    Please provide example code that replicates problem Commented Mar 10, 2015 at 15:16

2 Answers 2

1

Common issue with Angular. Instead of making the body scrollable, you should wrap everything inside a div and make the div an Angular controller. Then use the ng-scroll to detect scrolling position.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you want to get top offset of an element.

Like so with jQuery

  $('body').offset().top 

1 Comment

Not quite. I tried this too, and it also returns 0. This is because the offset to top, which is the html, is always 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.