Jump to content
Search Community

Have to use multiple timelines, scrollTrigger

learn_gsap test
Moderator Tag

Recommended Posts

 

Hello everyone, I hope you're doing great.

I have a strange problem with GSAP. In the demo I provided, various animations are supposed to happen during scrolling. Initially, the subtitle and description move up and disappear, then the title shrinks and moves to the left. After that, the slider moves up and aligns exactly next to the titles, and after each scroll, the slider starts moving. Once the slides are completed, the slider moves up, and the user proceeds to the rest of the page.

After a week of trying to accomplish this, I have failed. Some parts work well, but when I try to move the slider up and then make it start working, I encounter strange issues.

I would appreciate it if you could help me.

 

const getScrollAmount = () => {
    const scrollContainer = document.querySelector('.slider-container') as HTMLDivElement;
    const scrollContainerWidth = scrollContainer?.offsetWidth;
    return -(scrollContainerWidth - window.innerWidth);
  };

  useEffect(() => {
    gsap.registerPlugin(ScrollTrigger);

    const containerElm = document.getElementsByClassName('container');
    const containerHeight = containerRef.current?.offsetHeight;

    console.log('======================');
    console.log('container height', containerHeight);

    gsap.to('.extra-move', {
      y: -2 * containerHeight,
      opacity: 0,
      scrollTrigger: {
        trigger: '.container',
        start: 700,
        end: 'bottom 200px',
        scrub: 1,
      },
    });

    gsap.to('.inves-title', {
      x: -100,
      margin: 0,
      fontSize: '32px',
      transformOrigin: '0% 100%',
      scrollTrigger: {
        trigger: '.container',
        start: 700,
        end: 'bottom 200px',
        scrub: 1,
        pin: true,
      },
    });

    gsap.to('.sub-titles', {
      opacity: 1,
      visibility: 'visible',
      scrollTrigger: {
        trigger: '.container',
        start: 700,
        end: 'bottom 200px',
        scrub: true,
      },
    });

    const slidersElm = gsap.utils.toArray('.slider');

      gsap.from('.slider-scroll', {
        top: 1200,
        left: 0,
        scrollTrigger: {
          trigger: '.container',
          start: 'top top',
          end: 'bottom bottom',
          scrub: 1,
        },
      })
      gsap.to(slidersElm, {
        xPercent: -100 * (slidersElm.length - 1),
        duration: 3,
        ease: 'none',
        scrollTrigger: {
          trigger: '.slider-scroll',
          start: 'clamp(top top)', // Adjust the start position as needed
          end: `+=${getScrollAmount() * -1}`, // Specify the end point of the animation
          pin: true,
          scrub: 1,
          markers: true,
        },
      });
  }, []);

 

See the Pen zYQpYdx by Hooseinsalari (@Hooseinsalari) on CodePen

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...