Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Created January 17, 2018 20:24
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 neenjaw/d02fa66253be6d0c96336eb5f51b29bd to your computer and use it in GitHub Desktop.
Save neenjaw/d02fa66253be6d0c96336eb5f51b29bd to your computer and use it in GitHub Desktop.
JS for defining CSS3 variable --scroll, to make effects based on the scroll of a window
//put in function to be executed once elements with class="scrolling" have loaded
for (let el of document.querySelectorAll(".scrolling")) {
el.addEventListener("scroll", evt => {
let maxScroll = el.scrollHeight - el.offsetHeight;
let scroll = el.scrollTop / maxScroll;
el.style.setProperty("--scroll", scroll);
});
}
// Put in CSS rules.
// .scrolling {
// background-image: linear-gradient(to right, white calc(100 * var(--scroll)), transparent 0)
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment