Last active
June 14, 2018 18:23
-
-
Save jimthoburn/826efde653d813c80f116c95f825679a to your computer and use it in GitHub Desktop.
Save & restore scroll position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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