How can we effectively halt a keyframe CSS animation activated by hovering for a seamless transition?

When using a simple rotation keyframe animation on hover, the cube starts fine but suddenly stops and jumps back to its initial state when the pointer is moved out. Is there a way to add easing time or another solution in CSS to make the stopping smoother when the pointer is removed?

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
#cube:hover {
  animation: rotating 12s linear infinite;
}

#cube{
width: 100px;
height: 100px;
background:red;
transition-duration: 3s; /*Doesn't work*/
}


#cubeb {
width: 100px;
height: 100px;
background:red;
transition: transform 12s linear;
}

#cubeb:hover {
          transform: rotate(360deg);
}
<div id="cube">Jump When hover ends</div>

<br/>
<div id="cubeb">No Jump but does not continuously rotate on hover</div>

Answer №1

If you're looking to add some movement to your webpage, consider utilizing the animation-play-state property.

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

#cube:hover {
  animation-play-state: running;
}

#cube {
  width: 100px;
  height: 100px;
  background: red;
  animation: rotating 12s linear infinite paused;
}
<div id="cube">Jump When hover ends</div>

Answer №2

To enhance the rotation animation, consider incorporating an easing effect for a smoother transition when moving the pointer away.

Try using the ease-in-out timing function to gradually accelerate and decelerate the rotation:

#cube:hover {
  animation: rotating 10s ease-in-out infinite;
}

You can also customize the length of the easing effect:

#cube:hover {
  animation: rotating 10s cubic-bezier(0.2, 0.15, 0.35, 1.0) infinite;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Fixing the TopMenu in ASP.NET: A step-by-step guide

No Fixed https://i.sstatic.net/84wsi.png Fixed https://i.sstatic.net/y0NVY.png Layout Issue (_Layout Side) <div style="text-align:center; max-width:1200px; min-width:1200px; min-height:30px; padding:5px 0; text-align:left; left:26.3%; position:fixe ...

The font family specified in the Materia UI theme is experiencing compatibility issues on browsers that are not Chrome

Having difficulty overriding some elements with a specific font, as it seems to work perfectly on Chrome but not on other browsers like Safari and FireFox. Could there be something overlooked? Below is the code snippet: const customTheme = createMuiTheme( ...

Demonstrate the smooth transition of background colors

I am attempting to add a subtle animation to the background-color in my navbar, as it currently appears suddenly. I want the transition to be smooth and gradual. Check out the JSFiddle demo here: https://jsfiddle.net/3f0czkpt/ The jQuery ...

Encountering an unforeseen issue with my code

I am currently developing a web application in advanced Java and incorporating various external libraries such as choosen.js, summernote.js, Bootstrap, BootstrapValidator, etc. I have encountered some errors and am struggling to pinpoint the root cause of ...

What could be the reason for the hr tag's CSS not appearing in React code?

In my React code, I am trying to create a border using the following code: <hr className="borderLine" /> However, when I attempt to style it with CSS, I utilize this snippet of code: .divider { margin: 2rem; width: 100%; color: ...

What is the process for changing the background color with Angular?

It should be a straightforward task, but for some reason, it just won't cooperate. Within my controller: $scope.projects = [ //... { background: "#ffffcc" }, //... ]; In the HTML: <div ng-repeat="project in projects" ng-style="{ ...

Three image resources combined to create a unique border design in CSS

I am currently working on designing a webpage and am in need of creating a background border. I have access to three image resources for this task: one for the left side, another for the top, and the third for the right side. My struggle lies in finding s ...

Implementing CSS keyframes when a specified PHP condition is satisfied

I am looking to implement an opening animation on my website that should only play when a user visits the site for the first time. I want to avoid displaying the animation every time the page is reloaded, so it should only run if a cookie indicating the us ...

Tips on aligning an entire text in R Markdown using the bookdown::gitbook function as the desired output

I've been looking for a way to justify the text in my gitbook, but haven't had any luck finding a solution. I've attempted: Adding "text-align: justified" to the CSS right after the YAML Header: <style> body { text-align: justify ...

Issue with Background Color in WordPress Simple CSS PostGrid

Currently in the process of developing a demo website at I am attempting to add a grey border (#eeeeee) around the Post Grid-Elements located at the top and bottom of the page. However, it seems like there is an error somewhere. Can someone help me identi ...

Establishing a font style for all text with the exception of a specific CSS class

Below is the CSS code I am currently using html:not(.stylenotneeded), body:not(.stylenotneeded), form, fieldset, table, tr, td, img { font: 100%/150% calibri; } Here is the corresponding HTML: <div class="stylenotneeded"> <font face="Impac ...

When the React button is clicked, it displays a stylish blue border

Hey there, I'm currently working on a project using React and facing an issue with some buttons I've styled. Whenever I click on them, they show a strange blue border that I can't seem to get rid of. I've tried setting border:none !impo ...

An unusual misalignment in Internet Explorer 8

This website is set to launch soon, but I am facing a challenge with the header being shifted 17px down in IE8. Despite trying the IE developer tools, I have been unsuccessful in resolving this issue. Can anyone provide assistance? :/ ...

Unlock the innerHTML of a DIV by clicking a button with ng-click in Angular JS

I am curious about something. There is a <div> and a <button> in my code. I am looking to access the inner markup of the <div> within the ng-click function without relying on jQuery. Can you give me some guidance? <div id = text-entry ...

Populate a <div> element with text and an <input> field

Is there a way to automatically adjust the size of an input element inside a <div> when the <div> is resized? I've been trying to figure it out for the past 2 hours and I'm just not getting it. ...

To get the jQuery show() function to work properly, you must invoke an alert immediately

When using jquery.show() to display a div, I was unable to see the display effect unless I included an alert after the show method, like so: JavaScript: $("#screenAsLayer").show(); $("#screenAsLayer_content").show(); alert("aa"); HTML: <div id="s ...

Can you provide guidance on adjusting the HTML/CSS code in order to have the container expand horizontally to accommodate the floated children?

This arrangement causes the child divs to line up vertically. HTML <div class="outer"> (other content goes here) <div class="containingBox"> <div class="container"> <div class="child"></div> ...

What is the best way to create an adaptive <pre> tag for ASCII art without using media queries?

Currently, my username is displayed as an ASCII art banner/header and I am looking to enhance its responsiveness. Instead of relying solely on media queries, I am exploring the possibility of making it scale within a CSS grid container. This approach would ...

The importance of CSS z-index in optimizing for mobile responsiveness

In my jquery modal dialog box, I wanted to create a shadow effect only between the dialog box and the screen. To achieve this, I added an overlay using a <div>: <div id="overlay" class="overlay btn-hidden"></div> <d ...

Prevent user scrolling when full-screen menu is activated

Currently, I am developing a full-screen menu for a website and facing an issue with disabling the user's ability to scroll when the menu is open. Despite searching online, I have not found a suitable solution. It would be greatly appreciated if someo ...