I am working on a Next.js project where I am trying to implement a horizontal scrolling section using GSAP and ScrollTrigger. However, I am facing an issue where the horizontal scrolling animation is not working as expected. The subsequent content scrolls up immediately while the horizontal scroll is occurring.
I have been troubleshooting this for quite a while but haven't been able to resolve the issue. I have also noticed that the scroller-start and scroller-end markers are being placed at the top right-hand corner of the screen, which seems incorrect.
I have created a simplified version of my setup on StackBlitz to illustrate the issue: https://stackblitz.com/edit/nextjs-7zxyqi?file=components%2FStylists.tsx
Here's the relevant code from my project:
// Stylists.tsx
import React, { useEffect, useRef } from 'react';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger);
const images = [...]
const styles = {...}
const Stylists: React.FC = () => {
const panelsContainerRef = useRef(null);
useEffect(() => {
const sections = gsap.utils.toArray('.panel');
const sectionWidth = 420;
const scrollTween = gsap.to(sections, {
xPercent: -100 * (sections.length - 1),
ease: 'none',
scrollTrigger: {
start: 'top top', // Offset start position
trigger: panelsContainerRef.current,
pin: true,
pinSpacing: false,
scrub: 1.23,
snap: 1 / (sections.length - 1),
// end
end: () => '+=2000',
markers: true,
},
});
return () => {
scrollTween.kill(); // Kill the GSAP animation
ScrollTrigger.getAll().forEach((st) => st.kill()); // Kill all ScrollTrigger instances
};
}, []);
return (
<div style={styles.myContainer}>
<div style={styles.panelsContainer} ref={panelsContainerRef}>
<h2 style={styles.headerText}>OUR STYLISTS</h2>
<div style={styles.flexContainer}>
{images.map((image, index) => (
<section style={styles.panel} className="panel" key={index}>
<img
src={image.src}
alt={`Image ${index + 1}`}
style={styles.image}
/>
<div style={styles.textContainer}>
<h4>{image.name}</h4>
<p>{image.title}</p>
</div>
</section>
))}
</div>
<button>EXPLORE STYLISTS</button>
</div>
</div>
);
};
Attempts to resolve the issue:
- Adjusted the start, end, trigger, pin, and other ScrollTrigger configuration options.
- Checked the HTML structure and CSS styles.
- Utilized the markers option in ScrollTrigger to visualize the trigger points and scroll regions.
- Tried simplifying the setup to isolate the problem.
- Tried 'gsap.context()' to encapsulate the GSAP animation and ScrollTriggers
- Tried 'useIsomorphicLayoutEffect' and 'useLayoutEffect'
- Downgraded to gsap version "3.8.0"
- Cleared browser cache
Additional Information:
OS: Ubuntu 22.04.3 LTS
Browser: Brave
I would really appreciate any insights or suggestions on how to resolve this issue. Thank you!