Ask Your Question
2

What is the way to slow down the CSS animation and make it stop when hovering, but allow it to continue moving infinitely once the mouse leaves?

asked 2023-03-19 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-05-08 04:00:00 +0000

lalupa gravatar image

You can slow down the CSS animation using the "animation-duration" property and add the "animation-play-state" property to pause it when hovering. To allow it to continue moving infinitely once the mouse leaves, you can set the "animation-play-state" property back to "running". Here is an example code:

div {
  width: 100px;
  height: 100px;
  background-color: blue;
  animation: slide 5s infinite;
}

div:hover {
  animation-play-state: paused;
}

@keyframes slide {
  0% { transform: translateX(0); }
  100% { transform: translateX(200px); }
}

In this example, the animation will slide the blue div from left to right in 5 seconds and repeat infinitely. Then, when hovering the div, the animation will pause, but once the mouse leaves, the animation will resume from where it left off.

You can adjust the duration time and animation styles to fit your needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-03-19 11:00:00 +0000

Seen: 9 times

Last updated: May 08 '22