Created
April 16, 2024 10:56
-
-
Save HadalyVillasclaras/ff5ceda000713c321ef4d1ed0d294124 to your computer and use it in GitHub Desktop.
Panel swiper using GSAP scrollTo and ScrollTrigger
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
export function FilmsPage() { | |
const mainCont = useRef(); | |
const scrollTween = useRef(); | |
const { contextSafe } = useGSAP(() => { | |
const panels = gsap.utils.toArray('.prv-sect'); | |
panels.forEach((panel, i) => { | |
ScrollTrigger.create({ | |
trigger: panel, | |
markers: true, | |
start: 'top bottom', | |
// end: '+=200%', | |
onToggle: ((self) => { | |
return (self.isActive && !scrollTween.current && goToSection(i)) | |
}), | |
id: 'sect-' + i, | |
}); | |
}); | |
ScrollTrigger.normalizeScroll(true); | |
return () => { | |
if (scrollTween.current) { | |
scrollTween.current.kill(); | |
} | |
}; | |
//// just in case the user forces the scroll to an inbetween spot (like a momentum scroll on a Mac that ends AFTER the scrollTo tween finishes): | |
// ScrollTrigger.create({ | |
// start: 0, | |
// end: 'max', | |
// snap: 1 / (panels.length - 1), | |
// onToggle: ((self) => { | |
// if(self.isActive) { | |
// setIsCursorVisible(false); | |
// document.body.style.cursor = 'none'; | |
// } else { | |
// document.body.style.cursor = 'default'; | |
// } | |
// }), | |
// }); | |
}, | |
{ scope: mainCont } | |
); | |
const goToSection = contextSafe((i) => { | |
scrollTween.current = gsap.to(window, { | |
scrollTo: { y: i * window.innerHeight, autoKill: false }, | |
duration: 1, | |
ease: "power2.inOut", | |
id: 'scrollTween', | |
onComplete: (() => { | |
return (scrollTween.current = null); | |
}), | |
overwrite: true, | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment