Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is scrollTop in JQuery supposed to function?

I'm confused about scrollTop().
jQuery docs say it should be displaying a number that represents the scrollTop offset for a given element, but as it does in my own script, on their demo it returns "0".

Setting the scrollTop manually works great, as does using the $(window).scrollTop() command.

Does scrollTop() ONLY return values associated with elements that themselves contain scrollbars (such as an element with a CSS property of overflow:scroll)?

like image 242
FilmJ Avatar asked Jul 19 '09 23:07

FilmJ


1 Answers

It only follows that elements with scrollbars in positions other than the top would have a scrollTop of > 0. If you have an element without scrollbars then one would expect that their scrollTop position IS 0 as they have nowhere to scroll to.

I'm not exactly sure what the problem is.

EDIT: Just in case that it is not explained properly in the jQuery docs:

scrollTop() => position of scrollbar for element (window/div/anything scrollable)

$("#element").offset().top => position of element relative to page

$("#element").offset().top - $(window).scrollTop() => position of element relative to scrolled area.

like image 139
Darko Z Avatar answered Nov 11 '22 04:11

Darko Z