Skip to content

Instantly share code, notes, and snippets.

@jeangontijo
Created January 14, 2020 20:56
Show Gist options
  • Save jeangontijo/0361b3ed66ff800e54685bf080dedc29 to your computer and use it in GitHub Desktop.
Save jeangontijo/0361b3ed66ff800e54685bf080dedc29 to your computer and use it in GitHub Desktop.
Keyframe Animation to Scroll
:root * {
/* Pause the animation */
animation-play-state: paused;
/* Bind the animation to scroll */
animation-delay: calc(var(--scroll) * -1s);
/* These last 2 properites clean up overshoot weirdness */
animation-iteration-count: 1;
animation-fill-mode: both;
}
document.addEventListener( 'DOMContentLoaded', function () {
var root = document.body.style;
console.log(root);
// Initialize variables
root.setProperty('--scroll',window.pageYOffset / (document.body.offsetHeight - window.innerHeight));
// Scroll events
window.addEventListener('scroll',scroll,false);
function scroll() {
root.setProperty('--scroll',window.pageYOffset / (document.body.offsetHeight - window.innerHeight));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment