Last active
September 18, 2017 09:10
-
-
Save MiniXC/93d03abbb0b28547fe0f78e59b85c6dd to your computer and use it in GitHub Desktop.
remembers and sets the scrolling position of the current page after reload
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
var href = 'remscroll.js:' + window.location.href; | |
window.addEventListener('load', () => { | |
let scrollPos = localStorage.getItem(href); | |
if(scrollPos) { | |
document.scrollingElement.scrollTop = Number(scrollPos); | |
} | |
}); | |
document.addEventListener('scroll', () => { | |
if(!href.includes('#')) { | |
localStorage.setItem(href, document.scrollingElement.scrollTop); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment