Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move to the certain keyframe in CSS3 animation?

Let's say we have an animation like here: http://jsfiddle.net/utterstep/JPDwC/

Layout:

<div>back</div>
<div class="animating"></div>
<div>forward</div>

And corresponding CSS:

@keyframes someanimation {
    0% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
    100% {
        width: 100px;
    }
}

.animating {
    width: 200px;
    height: 200px;
    background-color: red;
    animation: someanimation 5s infinite;
}

And I want to go to the next or previous animation state when I press back or forward. There can be more than one object and I would like to switch their animation states at the same time.

Is it someway possible via CSS or CSS+JS, or it maybe it will be easier for me just to rewrite my code in pure-JS? (currently I don't like this idea because I have a lot of properties to animate and CSS makes it much more easier for me)

like image 475
utter_step Avatar asked Jul 14 '13 23:07

utter_step


People also ask

How do you specify a keyframe for animation?

To use keyframes, create a @keyframes rule with a name that is then used by the animation-name property to match an animation to its keyframe declaration.

How do you make an animation move in CSS?

To make a CSS animation, you need three things: an HTML element to animate, a CSS rule which binds the animation to this element, and a group of keyframes that defines the styles at the start and end of the animation. You can also add declarations to further customize your animation, like speed and delay.

How does keyframe animation work CSS?

Each keyframe describes how the animated element should render at a given time during the animation sequence. Since the timing of the animation is defined in the CSS style that configures the animation, keyframes use a <percentage> to indicate the time during the animation sequence at which they take place.

Can you have multiple keyframes CSS?

Property Values Defines the name of the animation. Required. Percentage of the animation duration. Note: You can have many keyframes-selectors in one animation.


1 Answers

Perhaps you have got the answer in CSS+JS

Please refer Old Post

Here on fiddle from the same post Fiddle

And with CSS only here is the Fiddle I have tweaked with :hover property,

@-webkit-keyframes someanimation {
    0% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
    100% {
        width: 100px;
    }
}

@-moz-keyframes someanimation {
    0% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
    100% {
        width: 100px;
    }
}

@keyframes someanimation {
    0% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
    100% {
        width: 100px;
    }
}



@-webkit-keyframes someani2 {
    0%,100% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
}

@-moz-keyframes someani2 {
    0%,100% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
}

@keyframes someani2 {
    0%,100% {
        width: 100px;
    }
    50% {
        width: 200px;
    }
}



.animating {
    width: 200px;
    height: 200px;
    background-color: red;
    -webkit-animation: someanimation 5s infinite;
    -moz-animation: someanimation 5s infinite;
    animation: someanimation 5s infinite;
}
.animating:hover{
    color:#f60;
    -webkit-animation: someani2 6s 1;
    -moz-animation: someani2 6s 1;
    animation: someani2 6s 1;
}
<div>back</div>
<div class="animating"></div>
<div>forward</div>

it changes the animation on hover, just like back button.

I hope this will solve your purpose..

like image 103
MarmiK Avatar answered Oct 17 '22 04:10

MarmiK