jahajee.com
jahajee.com

Reputation: 3743

window.pageYOffset vs document.documentElement.scrollTop

In Javascript window.pageYOffset and document.documentElement.scrollTop both measures the distance of an window top to its topmost visible content in pixel. Are they both same or am I missing something?

Trust window.pageYOffset is not supported for IE < 9 but if assuming IE >8 then

Upvotes: 35

Views: 31994

Answers (3)

thedanotto
thedanotto

Reputation: 7317

document.documentElement.scrollTop does NOT work with Safari nor Apple Products.

window.pageYOffset works on all browsers. On browsers that support both functions, they appeared to produce the same values.

Upvotes: 2

Hashan Seneviratne
Hashan Seneviratne

Reputation: 1177

The difference between these two can be clearly observed in cross platform mobile application development using Jquery mobile. There, several pages can be defined in single html pages.

document.documentElement.scrollTop will be useful when you are in a particular page and want the value relative to that page where as window.pageYOffset only applied to whole html page.

Otherwise like krish has mentioned, results from these two are basically the same.

Upvotes: 12

Krish
Krish

Reputation: 357

Both window.pageYOffset and document.documentElement.scrollTop returns the same result in all the cases.

Yes, window.pageYOffset is not supported below IE 9.

scrollTop() method also can be used to get the vertical scrollbar position of the specific element.

Upvotes: 22

Related Questions