I'm trying to create a marquee using the following css styling:
@keyframes scroll {
0% { transform: translateX(0) }
100% { transform: translate(-100%) }
}
animation: scroll var(--duration) linear infinite;
I want to make it slows down and stops on hover, then continue looping onMouseLeave, but I can't seem to achieve this effect due to css animation recomputing / resetting position each time I apply a new animation.
Is there a way to achieve this effect? Thank you!
You can toggle the
animation-play-state: pauseof the marquee, on:hoverof the parent component.With the marquee paused, you can then animate the parent's
transform: translateX();on:hoverwhich you can control to make it feel like the Marquee is slowing down to a stop.When
:hoveris removed, the DOM will resume the Marquee animation, with the combination of the parent wrapper'stranslatereturning to 0.Here is a working example of what I'm talking about: