Skip to content

Instantly share code, notes, and snippets.

@MiniXC
Last active September 18, 2017 09:10
Show Gist options
  • Save MiniXC/93d03abbb0b28547fe0f78e59b85c6dd to your computer and use it in GitHub Desktop.
Save MiniXC/93d03abbb0b28547fe0f78e59b85c6dd to your computer and use it in GitHub Desktop.
remembers and sets the scrolling position of the current page after reload
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