Created
November 30, 2020 13:27
-
-
Save celsowhite/1d28f17b82f5885bc1724bd469253379 to your computer and use it in GitHub Desktop.
Detect scroll direction using gsap.
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
import gsap from 'gsap'; | |
import { ScrollTrigger } from 'gsap/ScrollTrigger'; | |
function initScrollDirectionIndicator() { | |
// GSAP Plugins | |
gsap.registerPlugin(ScrollTrigger); | |
/*---------------------------- | |
Elements | |
----------------------------*/ | |
const body = document.querySelector('body'); | |
/*---------------------------- | |
Fixed Nav | |
----------------------------*/ | |
ScrollTrigger.create({ | |
markers: false, | |
trigger: body, | |
start: 'top -20%', | |
onUpdate: self => { | |
if (self.direction === 1) { | |
body.classList.add('scrolling-down'); | |
body.classList.remove('scrolling-up'); | |
} else { | |
body.classList.add('scrolling-up'); | |
body.classList.remove('scrolling-down'); | |
} | |
}, | |
onLeaveBack: () => { | |
body.classList.remove('scrolling-up'); | |
}, | |
}); | |
} | |
/*------------------------------- | |
Init | |
-------------------------------*/ | |
document.addEventListener('DOMContentLoaded', event => { | |
initScrollDirectionIndicator(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment