Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.pageYOffset vs document.documentElement.scrollTop

Tags:

javascript

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

  • When to use each of them
  • difference between them
like image 383
jahajee.com Avatar asked Mar 29 '14 06:03

jahajee.com


People also ask

What can I use instead of pageYOffset?

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.

What is the pageYOffset?

The read-only Window property pageYOffset is an alias for scrollY ; as such, it returns the number of pixels the document is currently scrolled along the vertical axis (that is, up or down) with a value of 0.0, indicating that the top edge of the Document is currently aligned with the top edge of the window's content ...


1 Answers

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.

like image 192
Krish Avatar answered Sep 28 '22 18:09

Krish