Share Posted June 3 Hello, I try to do onMouseEnter and onMouseLeave animation timeline. The example is located here https://codesandbox.io/s/serene-lena-5o2lhi?file=/src/App.js I store my timeline in useRef and then onEnter I use currentTarget and then just create the timeline and assign it tlRef. And in onLeave function I just do the reverse. From the first view, it seems it works fine, but when I try to hover and leave quickly and do it multiple times and the animation gets stuck. Could you please explain where I am wrong and what is the best practice for that? Link to comment Share on other sites More sharing options...
Share Posted June 4 Heya, something like this should workhttps://codesandbox.io/s/dreamy-snowflake-d91u3q?file=/src/App.js You don't want to be creating a new timeline every time you hover. timelines are also for sequences of animations, if you have one action happening on hover you can just use a tween. We have a whole guide here if you'd like to know more about best practises in React Good luck! Link to comment Share on other sites More sharing options...
Author Share Posted June 4 Thanks for your reply @Cassie and what if have complex button with icon and I want to move icon on hover and also overlay? Should I use the same principle and don’t create the timeline inside the hover function? Just modify your example https://codesandbox.io/s/runtime-rgb-kdtvbj?file=/src/App.js Seems it works fine. Actually my main concern is not to rewrite the same animation in onMouseLeave function. So, just wondering is it ok to write in context like this? Link to comment Share on other sites More sharing options...
Solution Solution Share Posted June 4 Yep, that's the idea! One tweak, keep all your animations inside the GSAP context so that they get killed on cleanup. ☺️ useLayoutEffect(() => { let ctx = gsap.context(() => { gsap.set(overlay.current, { xPercent: -100 }); tl.current = gsap.timeline({ paused: true }) .to(".text", { color: "black" }) .to(".overlay", { xPercent: 0 }, "<"); }); return () => ctx.revert(); }, []); 1 1 Link to comment Share on other sites More sharing options...
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