Jump to content
Search Community

Simple staggered animation issue

Spaghetti
Moderator Tag

Go to solution Solved by Spaghetti,

Recommended Posts

Posted

I don't know why this isn't working as intended

function MoviesList({ movies, onSelectMovie }) {
    const moviesList = useRef(null);

    useGSAP(
        () => {
            if (!movies.length) return;
            gsap.from(moviesList.current.querySelectorAll("li"), {
                opacity: 0,
                x: 120, // If you want movement, otherwise remove this line
                stagger: 0.2,
                duration: 0.5,
                ease: "power2.out",
            });
        },
        { scope: moviesList, dependencies: [movies] }
    );

    return (
        <ul
            className="list list-movies"
            ref={moviesList}
        >
            {movies?.map((movie) => (
                <Movie
                    movie={movie}
                    key={movie.imdbID}
                    onSelectMovie={onSelectMovie}
                ></Movie>
            ))}
        </ul>
    );
}

function Movie({ movie, onSelectMovie }) {
    return (
        <li onClick={() => onSelectMovie(movie.imdbID)}>
            <img
                src={movie.Poster}
                alt={`${movie.Title} poster`}
            />
            <h3>{movie.Title}</h3>
            <div>
                <p>
                    <span>🗓</span>
                    <span>{movie.Year}</span>
                </p>
            </div>
        </li>
    );
}

 

but this one which is exactly the same is working perfectly

function WatchedMoviesList({ watched, onDeleteWatchedMovie }) {
    const watchedContainerRef = useRef(null);


    useGSAP(
        () => {
            if (!watched.length) return;
            gsap.from(".watched-list li", {
                opacity: 0,
                x: 120,
                stagger: 0.2,
                duration: 0.5,
                ease: "power2.out",
            });
        },
        { scope: watchedContainerRef }
    );


    return (
        <ul
            className="list watched-list"
            ref={watchedContainerRef}
        >
            {watched.map((movie) => (
                <WatchedMovie
                    movie={movie}
                    onDeleteWatchedMovie={onDeleteWatchedMovie}
                    key={movie.imdbID}
                />
            ))}
        </ul>
    );
}


function WatchedMovie({ movie, onDeleteWatchedMovie }) {
    return (
        <li>
            <img
                src={movie.poster}
                alt={`${movie.title} poster`}
            />
            <h3>{movie.title}</h3>
            <div>
                <p>
                    <span>️</span>
                    <span>{movie.imdbRating}</span>
                </p>
                <p>
                    <span>🌟</span>
                    <span>{movie.userRating}</span>
                </p>
                <p>
                    <span></span>
                    <span>{movie.runtime}</span>
                </p>
                <button
                    className="btn-delete"
                    onClick={() => onDeleteWatchedMovie(movie.imdbID)}
                >
                    x
                </button>
            </div>
        </li>
    );
}

This is the issue I'm facing that the animation left isn't working properly and I can't figure it out

GSAPproblem.thumb.gif.9447d0bc50bda0162df4538e46171a7a.gif

See the Pen by (@) on CodePen.

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 the gsap-trial NPM package for using any of the bonus plugins: 

 

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. 

 

On a side note this could be about the scoping in this particular code:

const moviesList = useRef(null);

useGSAP(
  () => {
    if (!movies.length) return;
    gsap.from(moviesList.current.querySelectorAll("li"), {
      opacity: 0,
      x: 120, // If you want movement, otherwise remove this line
      stagger: 0.2,
      duration: 0.5,
      ease: "power2.out",
    });
  },
  { scope: moviesList, dependencies: [movies] }
);

Keep in mind that when you pass a scope in the useGSAP hook, there is no need to use the selector of the same element you're passing the scope, so in your case maybe this could work:

const moviesList = useRef(null);

useGSAP(
  () => {
    if (!movies.length) return;
    gsap.from("li", {
      opacity: 0,
      x: 120, // If you want movement, otherwise remove this line
      stagger: 0.2,
      duration: 0.5,
      ease: "power2.out",
    });
  },
  { scope: moviesList, dependencies: [movies] }
);

That will tell GSAP to look for all the <li> elements inside the scope you just pass in the config.

  • Like 1
  • Solution
Posted
11 hours ago, GSAP Helper said:

the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc

Thank you for your response! I just discovered what the issue was and came back to mark the question as resolved, only to find your helpful reply!

The problem turned out to be in the CSS. Since this is a course project, I didn’t write the CSS myself. The <li> items had transition: all 0.3s;, which seemed to interfere with the GSAP animation for the list. I fixed it by targeting a single CSS property instead: transition: background-color 0.3s;. After that, everything worked perfectly! Interestingly, I discovered this by chance when I decided to replace the class, and suddenly the animation worked.

Also, thank you for the tips about the selector and scope element! I didn’t know about those, as I’m still new to GSAP 😅.

Thank you again for your time and help ❤️!

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