Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Animation onClick

How can I get a CSS Animation to play with a JavaScript onClick? I currently have:

.classname {   -webkit-animation-name: cssAnimation;   -webkit-animation-duration:3s;   -webkit-animation-iteration-count: 1;   -webkit-animation-timing-function: ease;   -webkit-animation-fill-mode: forwards; } @-webkit-keyframes cssAnimation {   from {     -webkit-transform: rotate(0deg) scale(1) skew(0deg) translate(100px);   }   to {     -webkit-transform: rotate(0deg) scale(2) skew(0deg) translate(100px);   } } 

How can I apply an onClick?

like image 207
tekknolagi Avatar asked Jan 31 '11 05:01

tekknolagi


People also ask

How do you trigger an onclick animation?

On the Animations tab, in the Advanced Animation group, click Animation Pane. In the Animation Pane, select the animation that you want to trigger. In the Advanced Animation group, click Trigger, point to On Click of, and select the object that you want to trigger the animation.

Should I use CSS or JavaScript for animations?

Use CSS animations for simpler transitions, like one-shot transitions. For example, we can say like toggling UI element states. Use JavaScript animations when you want to have advanced effects like bounce, stop, pause, rewind, slow-down or if you need a control of when and what to animate.

How do I delay an animation in CSS?

The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.


2 Answers

Are you sure you only display your page on webkit? Here is the code, passed on safari. The image (id='img') will rotate after button click.

function ani() {   document.getElementById('img').className = 'classname'; }
.classname {   -webkit-animation-name: cssAnimation;   -webkit-animation-duration: 3s;   -webkit-animation-iteration-count: 1;   -webkit-animation-timing-function: ease;   -webkit-animation-fill-mode: forwards; }  @-webkit-keyframes cssAnimation {   from {     -webkit-transform: rotate(0deg) scale(1) skew(0deg) translate(100px);   }   to {     -webkit-transform: rotate(0deg) scale(2) skew(0deg) translate(100px);   } }
<input name="" type="button" onclick="ani()" value="Click"> <img id="img" src="https://i.stack.imgur.com/vghKS.png" width="328" height="328" />
like image 172
Shisoft Avatar answered Sep 22 '22 07:09

Shisoft


You just use the :active pseudo-class. This is set when you click on any element.

.classname:active {     /* animation css */ } 
like image 38
Paul Fisher Avatar answered Sep 24 '22 07:09

Paul Fisher