Jump to content
Search Community

Related CodePen Home GSAP cards how to recycle?

anecko test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Welcome to the forums, @anecko

 

I think the best way to approach this is to ignore ScrollTrigger initially - just focus on getting that animation of the cards unstacking to run the way you like (standalone). Sequence it in a gsap.timeline(). Then, once you're happy with that, it should be quite easy to hook that up to the scroll position by adding a ScrollTrigger to the timeline, and set scrub: true (or a number if you want to smooth it), and pin the container. 

 

Give it a crack and then if you get stuck, feel free to post your minimal demo here showing your progress. 👍

Link to comment
Share on other sites

You've got logic issues in your code - you're looping through every box and creating an entirely new timeline and ScrollTrigger each time through, and every one of those timelines is fighting for control of all of the boxes. Plus you're pinning the boxes instead of the container. So you've got a ton of conflicts going on. Since you have 3 boxes, you're creating 3 timelines, each of which is trying to control all 3 boxes (so 9 tweens).

 

I assume you meant to do this (I simplified the code for you too): 

https://stackblitz.com/edit/react-4q639w?file=src%2FApp.js

 

Better? 

Link to comment
Share on other sites

Hi,

 

There are a few issues in your setup.

 

You are already importing both hooks (useLayoutEffect and useRef) so there is no need to use the React. notation there, just use the hooks directly:

import React, { useLayoutEffect, useRef } from 'react';
import gsap from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger);

export default function Scroll() {
  const contentRef = useRef(null);

  useLayoutEffect(() => {}, []);

  return ();
}

Also you have an issue with scoping on your GSAP Context instance. This is your HTML
 

<div ref={contentRef} className="paralax">
  <div className="card one">1</div>
  <div className="card two">2</div>
  <div className="card three">3</div>
</div>

And this is your GSAP Context:

const ctx = gsap.context((self) => {
  let tl = gsap.timeline({
    scrollTrigger: {
      trigger: ".paralax",
      start: 'top top',
      end: '+=1000',
      scrub: 1,
      pin: true,
      markers: true,
    },
  });
  tl.to('.one, .two, .three', { yPercent: 150, stagger: -0.5, ease: "none" });
}, contentRef);

Basically you're passing he same class selector that the scope parameter contentRef so GSAP is looking for an element with a .paralax class within the scope and is not finding it because that is the actual scope ref. See the problem?

 

This seems to work the way you expect:

export default function Scroll() {
  const contentRef = useRef(null);

  useLayoutEffect(() => {
    const ctx = gsap.context((self) => {
      let tl = gsap.timeline({
        scrollTrigger: {
          trigger: contentRef.current,
          start: 'top top',
          end: '+=1000',
          scrub: 1,
          pin: true,
          markers: true,
        },
      });
      tl.to('.one, .two, .three', {
        yPercent: 150,
        stagger: -0.5,
        ease: 'none',
      });
    }, contentRef);
    return () => ctx.revert();
  }, []);

  return (
    <div ref={contentRef} className="paralax">
      <div className="card one">1</div>
      <div className="card two">2</div>
      <div className="card three">3</div>
    </div>
  );
}

Here is a fork of your Stackblitz example:

https://stackblitz.com/edit/react-mptzzr?file=src%2Fstyle.css,src%2FApp.js

 

Finally I strongly recommend you to read the articles Jack linked, since they provide a solid starting point for using GSAP in your React apps.

 

Happy Tweening!

  • Like 3
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...