Forked from celsowhite/scroll-direction-indicator.js
Created
December 10, 2021 23:12
-
-
Save Xceldeveloper/fef974aeabef4ed8c1e50eae58d86b2e 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