Skip to content

Instantly share code, notes, and snippets.

@electerious
Last active September 17, 2019 13:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electerious/7ad886432f55cfcb4222 to your computer and use it in GitHub Desktop.
Save electerious/7ad886432f55cfcb4222 to your computer and use it in GitHub Desktop.
Cross-browser scroll element fetching
const scrollroot = (() => {
if ('scrollingElement' in document) return document.scrollingElement
const initial = document.documentElement.scrollTop
document.documentElement.scrollTop = initial + 1
const updated = document.documentElement.scrollTop
document.documentElement.scrollTop = initial
return (updated > initial ? document.documentElement : document.body)
})()
@electerious
Copy link
Author

Example:

scrollroot.scrollTop = 100

Description:

[…] Despite the specification being clear on it, browsers disagree on which element should handle the scroll position. Firefox and Edge use html, while Safari and Chrome consider body as the scrolling element. A new DOM API has been introduced to deal with this issue (document.scrollingElement) but we can’t rely on it yet as it’s not widely supported. […]

Source:

https://medium.com/@bdc/stripe-open-source-behind-the-scenes-59790999dea0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment