Nuxt.js: document.body.scrollTop always returns 0 in Safari

Created on 7 Jan 2018  ·  9Comments  ·  Source: nuxt/nuxt.js

Does anyone have an issue getting the scroll offset in Safari or iOS devices?
This works perfectly in Firefox:

this.scrollPosition = document.body.scrollTop

but in Safari I'm always getting 0, no matter what. That happens only with Nuxt apps.

Thanks for your help!

This question is available on Nuxt.js community (#c2182)

Most helpful comment

This is the correct way to get body scrollTop in modern browsers:

function getBodyScrollTop () { const el = document.scrollingElement || document.documentElement return el.scrollTop }

This is not a nuxt related question though, so should be closed.

All 9 comments

This is the correct way to get body scrollTop in modern browsers:

function getBodyScrollTop () { const el = document.scrollingElement || document.documentElement return el.scrollTop }

This is not a nuxt related question though, so should be closed.

Hey, thank you for your reply. I am aware that this shouldn't be Nuxt related, but as the thing is that your method works in plain HTML, but not as a Nuxt/Vue method. And I have no idea why. Asking, if anyone else had a similar problem.

Ok, so I found out what is the issue. I had a css on body element, which prevents .scrollTop to get correct value. But interestingly, only in Safari.

body {
  overflow-x: hidden;
}

The issue can be closed.

const scrollTop = Math.max(window.pageYOffset, document.documentElement.scrollTop, document.body.scrollTop)

Thank you! This is very helpful!

Have you solved it, please? I have the same problem.

Yes, my problem was overflow-x: hidden on a body element. Check, if you're having the same attribute.

@davision thanks 谢谢你 我再试下 :)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pehbehbeh picture pehbehbeh  ·  3Comments

shyamchandranmec picture shyamchandranmec  ·  3Comments

o-alexandrov picture o-alexandrov  ·  3Comments

vadimsg picture vadimsg  ·  3Comments

VincentLoy picture VincentLoy  ·  3Comments