Created
January 17, 2018 20:24
-
-
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
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
//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