Spaghetti Posted December 30, 2024 Posted December 30, 2024 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 See the Pen by (@) on CodePen.
GSAP Helper Posted December 30, 2024 Posted December 30, 2024 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: React (please read this article!) Next Svelte Sveltekit Vue Nuxt 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. 1
Solution Spaghetti Posted December 31, 2024 Author Solution Posted December 31, 2024 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 ❤️!
GSAP Helper Posted December 31, 2024 Posted December 31, 2024 Yes, that can be a source of trouble indeed. Here you can learn more about it: https://gsap.com/resources/mistakes#using-css-transitions-and-gsap-on-the-same-properties 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now