Jump to content
Search Community

Recommended Posts

Posted

I'm quite a beginner in GSAP stuff so I followed a tutorial (I will call it "cards stack" for now) to incorporate it into my working-in-progress. When I ran the cards stack code separately in other files, it worked just fine, but when I incorporated it into my on-going project, it had a weird white space in the middle. I only found out that it related to the pinSpacing when inspecting elements, and have tried various ways to fix it as well as visiting the forum for related issues but still cannot find a solution.

Here is the code: https://codesandbox.io/p/sandbox/s4g8sx

Thank you so much for your help.

Posted

Hi,

 

For the look of your project I think you are overcomplicating things, first you are pinning the entire first section of the page and then you are pinning a child element inside of it. This can lead to unexpected behaviour as explained in this post:

 

My suggestion would be to put everything in one timeline and just pin the entire section. The first part of the timeline should be the scale of the first section top content and then start with the cards animations.

 

Also this seems overkill to me:

onUpdate: (self) => {
  const progress = self.progress; // Current scroll progress (0-1)
  const totalCards = cards.length;
  const progressPerCard = 1 / totalCards; // Each card gets equal scroll segment

  // 10. Animate each card individually
  cards.forEach((card, index) => {
    const cardStart = index * progressPerCard;

    // Calculate card-specific progress (0-1)
    let cardProgress = (progress - cardStart) / progressPerCard;

    // Keep progress between 0-1
    cardProgress = Math.min(Math.max(cardProgress, 0), 1);

    // 11. Base vertical animation (bottom to center)
    let yPos = window.innerHeight * (1 - cardProgress);
    let xPos = 0;

    // 12. Diagonal movement when card is "done"
    if (cardProgress === 1 && index < totalCards - 1) {
      const remainingProgress =
            (progress - (cardStart + progressPerCard)) /
            (1 - (cardStart + progressPerCard));

      if (remainingProgress > 0) {
        // Calculate movement intensity (decreases for later cards)
        const distanceMultiplier = 1 - index * 0.15;

        // Calculate diagonal movement
        xPos =
          -window.innerWidth * 0.3 * distanceMultiplier * remainingProgress;
        yPos =
          -window.innerHeight *
          0.3 *
          distanceMultiplier *
          remainingProgress;
      }
    }

    // 13. Apply position updates
    gsap.to(card, {
      y: yPos, // Vertical position
      x: xPos, // Horizontal position
      duration: 0, // Apply immediately
      ease: "none",
    });
  });
},

You are calling all that code, which includes a loop, on every update of a ScrollTrigger instance, which can happen several times in one second, that's why I suggested to use a Timeline for everything, is far better and more optimized resource-wise.

 

Finally have a look at @mvaneijgen's guide on stacking cards animations:

 

Hopefully this helps

Happy Tweening!

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...