Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Make CSS Animations Ease Back To Position When No Longer Hovering?

In my example, I'm using "Bob" by Ian Lunn. I really like the Hover effect, but as soon as I stop hovering it jolts back to it's original position. How can I ease my div back to it's regular position?

Animation CSS:

animation-name: hvr-bob-float, hvr-bob;
animation-duration: .3s, 1.5s;
animation-delay: 0s, .3s;
animation-timing-function: ease-out, ease-in-out;
animation-iteration-count: 1, infinite;
animation-fill-mode: forwards;
animation-direction: normal, alternate;

jsfiddle: http://jsfiddle.net/v3z9rLae/

like image 420
CSS Apprentice Avatar asked Oct 02 '15 19:10

CSS Apprentice


People also ask

How do you stop hover transition in CSS?

By setting the animation-play-state to paused on hover, the animation will pause, your click targets will stop moving, and your users will be happy.

How do you rerun an animation in CSS?

The key to restarting a CSS animation is to set the animation-name of an animation to 'none' and then setting it back to the original animation. As you can see this requires two steps. One to switch it to a different animation and one to set it back.

Can we use transition without hover?

But transitions are not just limited to use with :hover . You can animate CSS properties, thus use CSS transitions without hover. This is done via transitions using some other CSS techniques, a number of which I've outlined below.

How do I smooth an animation in CSS?

The animation-timing-function specifies the speed curve of an animation. The speed curve defines the TIME an animation uses to change from one set of CSS styles to another. The speed curve is used to make the changes smoothly.


1 Answers

You can use a pseudo-element to make the circle and animate it with hvr-bob. Then use a transition on its parent to simulate the hvr-bob-float animation. It's not great, but it reduces some of the abruptness.

Here's an update to your fiddle

/* Bob */
    	@-webkit-keyframes hvr-bob {
    		50% {
    			-webkit-transform: translateY(4px);
    			transform: translateY(4px);
    		}
    	}
    	
    	@keyframes hvr-bob {
    		50% {
    			-webkit-transform: translateY(4px);
    			transform: translateY(4px);
    		}
    	}
    
    	.hvr-bob {
    		display: inline-block;
            height: 80px;
            width: 80px;
            margin: 2%;
    		vertical-align: middle;
    		-webkit-transform: translateZ(0);
    		transform: translateZ(0);
    		box-shadow: 0 0 1px rgba(0, 0, 0, 0);
    		-webkit-backface-visibility: hidden;
    		backface-visibility: hidden;
    		-moz-osx-font-smoothing: grayscale;
            
            /* use transition on parent */
            -webkit-transition: transform 0.3s ease-out;
            transition: transform 0.3s ease-out;
    	}
    
        /* the circle using a pseudo-element */
        .hvr-bob:before {
            content: "";
            display: block;
            background-color: #DDDDDD;
            border-radius: 100%;
            width: 100%;
            height: 100%;
        }
    
        /* use transform on parent */
         .hvr-bob:hover, .hvr-bob:focus, .hvr-bob:active {
            -webkit-transform: translateY(-8px);
            transform: translateY(-8px);
        }
    	
    	.hvr-bob:hover:before, .hvr-bob:focus:before, .hvr-bob:active:before {
    		-webkit-animation-name: hvr-bob;
    		animation-name: hvr-bob;
    		-webkit-animation-duration: 1.5s;
    		animation-duration: 1.5s;
    		-webkit-animation-delay: .3s;
    		animation-delay: .3s;
    		-webkit-animation-timing-function: ease-in-out;
    		animation-timing-function: ease-in-out;
    		-webkit-animation-iteration-count: infinite;
    		animation-iteration-count: infinite;
    		-webkit-animation-fill-mode: forwards;
    		animation-fill-mode: forwards;
    		-webkit-animation-direction: alternate;
    		animation-direction: alternate;
    	}
<div class="hvr-bob"></div>
like image 199
Jacob Avatar answered Oct 27 '22 23:10

Jacob