Skip to content

Instantly share code, notes, and snippets.

@elfacht
Created January 5, 2022 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfacht/017e277d952117c630017fc203ab3520 to your computer and use it in GitHub Desktop.
Save elfacht/017e277d952117c630017fc203ab3520 to your computer and use it in GitHub Desktop.
Memorize Scroll Position
/* @source https://css-tricks.com/memorize-scroll-position-across-page-loads/ */
let sidebar = document.querySelector(".sidebar");
let top = localStorage.getItem("sidebar-scroll");
if (top !== null) {
sidebar.scrollTop = parseInt(top, 10);
}
window.addEventListener("beforeunload", () => {
localStorage.setItem("sidebar-scroll", sidebar.scrollTop);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment