Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google chrome document.body.scrollTop always returns 0

In Google chrome document.body.scrollTop always returns 0.

I try

  if (window.pageYOffset > 0) {
    st = window.pageYOffset;
  } else if (document.documentElement.scrollTop > 0) {
    st = document.documentElement.scrollTop;
  } else {
    st = document.body.scrollTop;
  }

But not working. document.body.scrollTop is working in firefox.

Even in chrome console when i this code in console it is not working.

enter code here
  $('html, body').stop().animate({
    scrollTop: 50
  }, 500);
like image 786
Ricky ponting Avatar asked May 01 '17 09:05

Ricky ponting


People also ask

What is the document documentElement scrollTop?

The Element. scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content.

Can I use document documentElement scrollTop?

Scrolling: scrollTo, scrollBy, scrollIntoView Regular elements can be scrolled by changing scrollTop/scrollLeft . We can do the same for the page using document. documentElement.


1 Answers

I hit this as well. According to this issue this behavior changed between Chrome 60 and Chrome 61:

https://bugs.chromium.org/p/chromium/issues/detail?id=766938

From what I have read the new behavior is actually more standards compliant. The suggested fix from comment #5 for that issue is:

If you are looking for an interoperable way of querying scrollTop you can use the window.scrollY attribute or doing something like document.documentElement.scrollTop || document.body.scrollTop

like image 108
Ian Lovejoy Avatar answered Oct 14 '22 07:10

Ian Lovejoy