Jump to content
Search Community

How to prevent GSAP resetting the animation on window resize using from

dxc01

Recommended Posts

Posted

I'm having an issue where on window resize my elements return to an opacity of 0. This is not ideal. 

 

const animateBoxes = () => {
  const isDesktop = window.innerWidth > 768;

  boxRefs.current.forEach((ref, i) => {
    if (!ref || !ref.parentElement) return;

    const dataKind = ref.parentElement.getAttribute("data-kind");

    // Determine animation parameters
    let x = 0, y = 0, delay = 0;

    if (isDesktop) {
      switch (dataKind) {
        case "1":
          x = -100;
          delay = i * 0.1;
          break;
        case "2":
          y = 100;
          delay = i * 0.2;
          break;
        case "3":
          x = 100;
          delay = i * 0.3;
          break;
        default:
          return;
      }
    } else {
      x = dataKind === "1" ? -100 : 100;
      delay = dataKind === "1" ? i * 0.1 : i * 0.2;
    }

    let tl = gsap.timeline();
    tl.from(ref, {
      opacity: 0,
      x,
      y,
      duration: 2,
      delay,
      onComplete: () => {
        tl.pause();
      }
    });
  });
};

I essentially have three columns (2 on mobile). This may help illustrate my logic. 

 

I'm using an aria-kind="number" to determine which ones should animate and how so. They animate in columns.

 

The issue is that the moment the window is resized GSAP tries to reset the animation.

 

I have tried a pause() and also a kill() but no luck. 

 

This post on here does suggest the reason is because of the .from() animation, however I can't see on the .from() GSAP docs why this is happening and how to stop it. 

 

I guess I could go the other way with a .to() however this would mean that the CSS would need to be setup to handle it prior to load which technically adds more code? 

GSAP Helper
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen.

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import GSAP as shown in the Install Helper in our Learning Center : 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Posted

Hi,

 

By any chance are you using a resize event handler in order to call the animateBoxes function on every resize?

 

If that is the case then maybe you should take a look at GSAP MatchMedia:
https://gsap.com/docs/v3/GSAP/gsap.matchMedia()

 

Based on the setup you have, perhaps just animate the third column inside a GSAP MatchMedia instance, since the first two columns will always animate regardless of the screen size, but this is mostly a blind guess, a minimal demo (emphasis on minimal) would go a long way in expedite support here.

 

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