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?
This question is related to
javascript
css
animation
click
onclick
Found solution on css-tricks
const element = document.getElementById('img')
element.classList.remove('classname'); // reset animation
void element.offsetWidth; // trigger reflow
element.classList.add('classname'); // start animation
Add a
-webkit-animation-play-state: paused;
to your CSS file, then you can control whether the animation is running or not by using this JS line:
document.getElementById("myDIV").style.WebkitAnimationPlayState = "running";
if you want the animation to run once, every time you click. Remember to set
-webkit-animation-iteration-count: 1;
You just use the :active
pseudo-class. This is set when you click on any element.
.classname:active {
/* animation css */
}
You can do that by using following code
$('#button_id').on('click', function(){
$('#element_want_to_target').addClass('.animation_class');});
You can achieve this by binding an onclick listener and then adding the animate class like this:
$('#button').onClick(function(){
$('#target_element').addClass('animate_class_name');
});
var abox = document.getElementsByClassName("box")[0];_x000D_
function allmove(){_x000D_
abox.classList.remove("move-ltr");_x000D_
abox.classList.remove("move-ttb");_x000D_
abox.classList.toggle("move");_x000D_
}_x000D_
function ltr(){_x000D_
abox.classList.remove("move");_x000D_
abox.classList.remove("move-ttb");_x000D_
abox.classList.toggle("move-ltr");_x000D_
}_x000D_
function ttb(){_x000D_
abox.classList.remove("move-ltr");_x000D_
abox.classList.remove("move");_x000D_
abox.classList.toggle("move-ttb");_x000D_
}
_x000D_
.box {_x000D_
width: 100px;_x000D_
height: 100px;_x000D_
background: red;_x000D_
position: relative;_x000D_
}_x000D_
.move{_x000D_
-webkit-animation: moveall 5s;_x000D_
animation: moveall 5s;_x000D_
}_x000D_
.move-ltr{_x000D_
-webkit-animation: moveltr 5s;_x000D_
animation: moveltr 5s;_x000D_
}_x000D_
.move-ttb{_x000D_
-webkit-animation: movettb 5s;_x000D_
animation: movettb 5s;_x000D_
}_x000D_
@keyframes moveall {_x000D_
0% {left: 0px; top: 0px;}_x000D_
25% {left: 200px; top: 0px;}_x000D_
50% {left: 200px; top: 200px;}_x000D_
75% {left: 0px; top: 200px;}_x000D_
100% {left: 0px; top: 0px;}_x000D_
}_x000D_
@keyframes moveltr {_x000D_
0% { left: 0px; top: 0px;}_x000D_
50% {left: 200px; top: 0px;}_x000D_
100% {left: 0px; top: 0px;}_x000D_
}_x000D_
@keyframes movettb {_x000D_
0% {left: 0px; top: 0px;}_x000D_
50% {top: 200px;left: 0px;}_x000D_
100% {left: 0px; top: 0px;}_x000D_
}
_x000D_
<div class="box"></div>_x000D_
<button onclick="allmove()">click</button>_x000D_
<button onclick="ltr()">click</button>_x000D_
<button onclick="ttb()">click</button>
_x000D_
Try this:
<div>
<p onclick="startAnimation()">Start</p><!--O botão para iniciar (start)-->
<div id="animation">Hello!</div> <!--O elemento que você quer animar-->
</div>
<style>
@keyframes animationName {
from {margin-left:-30%;}
}
</style>
<script>
function startAnimation() {
document.getElementById("animation").style.animation = "animationName 2s linear 1";
}
</script>
Source: Stackoverflow.com