Skip to content

Instantly share code, notes, and snippets.

@jimthoburn
Last active June 14, 2018 18:23
Show Gist options
  • Save jimthoburn/826efde653d813c80f116c95f825679a to your computer and use it in GitHub Desktop.
Save jimthoburn/826efde653d813c80f116c95f825679a to your computer and use it in GitHub Desktop.
Save & restore scroll position
(function() {
var position
function saveScrollPosition() {
// If we don’t already have a saved scroll position
if (position == null || position <= 0) {
position = window.scrollY
}
}
function restoreScrollPosition() {
// If we have a saved scroll position, and if we’re at the top of the page
if (position != null && position > 0 && window.scrollY <= 0) {
window.scrollTo(0, position)
// Reset the saved scroll position
position = null
}
}
window.__saveScrollPosition = saveScrollPosition
window.__restoreScrollPosition = restoreScrollPosition
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment