Skip to content

Instantly share code, notes, and snippets.

@celsowhite
Created November 30, 2020 13:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save celsowhite/1d28f17b82f5885bc1724bd469253379 to your computer and use it in GitHub Desktop.
Save celsowhite/1d28f17b82f5885bc1724bd469253379 to your computer and use it in GitHub Desktop.
Detect scroll direction using gsap.
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