Created
July 19, 2022 13:13
-
-
Save bjornpatje/10980744f09239d83bd34f4db1526e00 to your computer and use it in GitHub Desktop.
Horizontal Scroll with 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
| <!-- HORIZONTAL SCROLL --> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/ScrollTrigger.min.js"></script> | |
| <script type="text/javascript"> | |
| if(jQuery('.page-id-735')){ | |
| gsap.registerPlugin(ScrollTrigger); | |
| var allSections = jQuery('.horizontal-section'); | |
| allSections.wrapAll('<div class="horizontal-container"/>'); | |
| const sections = gsap.utils.toArray(".horizontal-section"); | |
| let maxWidth = 0; | |
| const getMaxWidth = () => { | |
| maxWidth = 0; | |
| sections.forEach((section) => { | |
| maxWidth += section.offsetWidth; | |
| }); | |
| }; | |
| getMaxWidth(); | |
| ScrollTrigger.addEventListener("refreshInit", getMaxWidth); | |
| gsap.to(sections, { | |
| x: () => `-${maxWidth - window.innerWidth}`, | |
| ease: "none", | |
| scrollTrigger: { | |
| trigger: ".horizontal-container", | |
| pin: true, | |
| scrub: 1, | |
| end: () => `+=${maxWidth}`, | |
| invalidateOnRefresh: true | |
| } | |
| }); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment