0
How to add the animation effect only when the object is clicked? I’m already using animations with Animate.css, but I’d like the effect to start only when I click above. How to do this?
0
How to add the animation effect only when the object is clicked? I’m already using animations with Animate.css, but I’d like the effect to start only when I click above. How to do this?
6
You don’t need to put an entire, relatively heavy library like jQuery just to use Animate.css
just add the classes with .className
to the desired element, and use the events webkitAnimationEnd
, mozAnimationEnd
, MSAnimationEnd
, oanimationend
and animationend
(events for old and new browsers, to maintain compatibility) to detect when animation is over
Example when clicking on H1:
var foo = document.querySelector('#foo');
foo.addEventListener('click', function () {
foo.className = 'animated bounce';
});
foo.addEventListener('webkitAnimationEnd', removeClass);
foo.addEventListener('mozAnimationEnd', removeClass);
foo.addEventListener('MSAnimationEnd', removeClass);
foo.addEventListener('oanimationend', removeClass);
foo.addEventListener('animationend', removeClass);
function removeClass()
{
foo.className = '';
}
h1 {
color: #f35626;
background: -webkit-linear-gradient(92deg,#f35626,#feab3a);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 48pt;
text-align: center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<h1 id="foo">Stack Overflow</h1>
example when clicking another element:
var foo = document.querySelector('#foo');
var botao = document.querySelector('#animar');
botao.addEventListener('click', function () {
foo.className = 'animated bounce';
});
foo.addEventListener('webkitAnimationEnd', removeClass);
foo.addEventListener('mozAnimationEnd', removeClass);
foo.addEventListener('MSAnimationEnd', removeClass);
foo.addEventListener('oanimationend', removeClass);
foo.addEventListener('animationend', removeClass);
function removeClass()
{
foo.className = '';
}
h1 {
color: #f35626;
background: -webkit-linear-gradient(92deg,#f35626,#feab3a);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 48pt;
text-align: center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<h1 id="foo">Stack Overflow</h1>
<button id="animar">Animar</button>
3
With jQuery, you can add the Animated.css class into a function click
.
Edit. In the documentation there are some methods to check the end of the animation, and so includes in the answer this option so that the user can click and make the animation whenever he wants (thanks to Mr. @dvd for his help)
Take the example
$(document).ready(function () {
$(".box").on("click", function(){
var box = $(this);
var classes = "animated wobble";
box.addClass(classes)
.on("animationEnd oAnimationEnd mozAnimationEnd webkitAnimationEnd", function(){
box.removeClass(classes);
});
});
});
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.box {
cursor: pointer;
display:inline-block;
}
<link rel="stylesheet" type="text/css" media="screen" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css"
/>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="box">
<img src="http://placecage.com/100/100">
</div>
how simple!!! worked out! Thank you very much!
@Rafaelalopes looks at the edit I made, now always at the end of the animation the user has the chance to click again to see the effect if you want it. But if you don’t want to leave it the way it was before
If you’re using the animate.css
, it would not be idle processing loss to use jQuery?
Wallace Maxters name may confuse things a bit, but actually Animate.css is a 100% css framework. Enter his link and look at the Download link there https://daneden.github.io/animate.css/ Without jQueri she would have to use a chackbox to add and remove the classes on Click. Animate.css did not originally have JS for this
@Wallacemaxters actually just saw the source code of the project itself and if you notice there at the end of the doc has a script, including in jQuery that it uses to run the examples of the page by clicking on the btn "Animate It"
Yeah, you’re right. The doc itself teaches using jQuery [which I find quite unnecessary, since jQuery has a lot of animation functions].
AP may already be using jQuery on the page.
Much better to use CSS animation than jQuery. D
@I saw that you made an example even better than mine, I had not noticed that in the documentation had the events of the end of the animation. If Mr. do not mind I can include in my reply?
Of course, at ease.
@dvd is, but now that I saw that the question was edited. When I made the comment, I think the tags were not there. I think the AP ended up not specifying such a thing. Anyway, if the lib itself encourages using jQuery, do what, right? Lately, I’ve been an anti-jQuery evangelizer
@Wallacemaxters I particularly like the animations of CSS because you can control them easier, using delays, stopping at the end, using Cubic-Bezier etc, can control the fragments of the animation and each element that will be animated more simply, I prefer to leave jQuery in this case just to do the "Trigger", but this goes from each one, I have more ease with css so I prefer to go in this
@dvd I appreciate, includes credits []s
@hugocsl the idea of having a "Trigger" I think it’s good, I just don’t like to carry all the rest of jQuery. I even think it has alternative libs (lighter) than jQuery.
Browser other questions tagged html5 css-animations
You are not signed in. Login or sign up in order to post.
Can you demonstrate with code what you’ve done, please?
– Lucas de Carvalho
but I don’t know how it does, so I’m asking =) I’m just using simple animation. For example: <H1 class="Animated shake">Animation when clicked</H1>. Then the animation occurs so that I update the page, but I want it to start only after I click on this H1, understood?
– Rafaela Lopes
They made an issue there by putting jQuery. Is @Rafaelalopes really using jQuery or not? That information wasn’t there initially.
– Wallace Maxters
@Wallacemaxters sorry if you leave things confused, I myself had included the new tags, because they were used to click in the animation, only for research purposes in the future etc, since they were used in the answer. Without js it would be more complex to trigger the animation as the original classes of Animated.css
– hugocsl
@hugocsl I even understood your point. Relatively, things with jQuery are usually (or used to be) simpler, but in the case at hand, note that your code practically has the same amount of lines as Guilherme’s.
– Wallace Maxters
@hugocsl the tags have to be relevant to the problem and not the answers, the answers can fit the tags or go beyond and only. If I gave an answer that used SVG somehow combined with Animate.css this would not make it right to put the SVG tag in the question.
– Guilherme Nascimento
@Wallacemaxters would certainly be the most coherent option if she is no longer using jQuery in the project. I agree with you. And about the tags I’m even reading this here rss https://pt.meta.stackoverflow.com/questions/3829/addir-tags-as-replies
– hugocsl
@Guilhermenascimento as it is I’m even reading about it in Meta here :)
– hugocsl