Google chrome document.body.scrollTop always retur

2019-02-17 04:46发布

问题:

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);

回答1:

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