Created
January 5, 2022 10:58
-
-
Save elfacht/017e277d952117c630017fc203ab3520 to your computer and use it in GitHub Desktop.
Memorize 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
/* @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