Last active
November 18, 2021 16:00
-
-
Save asci/a466db193aef44fac462e76fc16a8d51 to your computer and use it in GitHub Desktop.
Setting scroll progress to CSS variable
This file contains hidden or 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
(() => { | |
const rootStyle = document.querySelector(":root").style; | |
const scrollMaxY = window.scrollMaxY || (document.documentElement.scrollHeight - document.documentElement.clientHeight); | |
rootStyle.setProperty("--scroll-y-max-position", scrollMaxY) | |
rootStyle.setProperty("--scroll-y-position", window.scrollY) | |
rootStyle.setProperty("--scroll-y-progress", scrollMaxY ? window.scrollY / scrollMaxY : 1) | |
document.addEventListener("scroll", () => { | |
rootStyle.setProperty("--scroll-y-position", window.scrollY) | |
rootStyle.setProperty("--scroll-y-progress", window.scrollY / scrollMaxY) | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment