20

Today I tried to implement in my site animate.css

But I wanted to make sure that the 'effect is activated in the: hover So I used jquery for this

The code is :

 $(".questo").hover( function (e) {
    $(this).toggleClass('animated shake', e.type === 'mouseenter');
});

The problem is that once you remove the mouse, the 'effect is interrupted. It could land the 'effect even without hover effect once it has begun?

Thank you all in advance

2
  • The hover event is fired whe you enter/leave the element, what are you trying to achieve? Commented Jan 17, 2014 at 9:47
  • Thank you for your answer: when I remove the mouse 's effect stops abruptly,I would like to at least finish instead of the action Commented Jan 17, 2014 at 9:53

7 Answers 7

46

In your CSS file you could just add:

.questo:hover {
    -webkit-animation: shake 1s;
    animation: shake 1s;
}

The nice thing about this solution is that it requires no JavaScript. For reference look at this page

Sign up to request clarification or add additional context in comments.

3 Comments

This is great, except that the moment you stop hovering, the animation will be cut off.
@MikeLambert you can make the animation infinite by adding the keyword infinite next to shake: -webkit-animation: shake infinite 1s; animation: shake infinite 1s; this way it will not be cut off unless you remove the house from the element.
Just wanted to comment for any nubie reading that if you want to animate an icon (<i>) in a button you can just do button:hover i { to select it such that the button is hovered but only the icon is animated
10

See this variant (more interactive):

$(".myDiv").hover(function(){
    $(this).addClass('animated ' + $(this).data('action'));
});
$(".myDiv").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd",function(){
    $(this).removeClass('animated ' + $(this).data('action'));
});

http://jsfiddle.net/DopustimVladimir/Vc2ug/

1 Comment

You are a great, this effect is much better quality and much more useful thanks
5

Rather than toggle the classes on hover, add them on mouse enter, like this

$(".questo").mouseEnter(function(event) {
    $(this).addClass("animated shake");
});

Then you can remove the classes when the animation ends

$(".questo").on("webkitAnimationEnd mozAnimationEnd oAnimationEnd animationEnd", function(event) {
    $(this).removeClass("animated shake");
});

I would do it on mouse enter, rather than hover, because you could trigger the animation when the mouse moves out of the element.

See this jsFiddle for an example

Comments

1

I believe you are saying animation stops once the mouse leaves and you are trying to achieve animation that once started on hover would stay on.

In this case you have to set the animation iteration count to infinite. Please add

 animation-iteration-count:infinite 

to the animation class in your stylesheet.

3 Comments

or add a number instead of infinite to specify number of times
Have you checked the animation-duration: property - looks like you may have to change it to 2s or whatever you are trying to achieve.
Thanks for your response, I created a jsfiddle to help me better You can write them in code so as to see and understand the 'effect?
1

Instead of use toggleClass, change your code by adding the class in hover, and hook for the animation end css3 to finish and than remove the class.

Code:

$(".questo").hover(function (e) {
    $(this).addClass('animated shake');
});

$(".questo").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function () {
    $(this).removeClass('animated shake');
});

Demo: http://jsfiddle.net/IrvinDominin/xxJ7K/1/

1 Comment

@user3198605 Why have you removed the chackmark? My solution answer you question, the newly accepted answer is something else (and copied from my solution)
0

Assuming you want to add the class when mouse enter and remove the class when mouse leave....

You can try this:

$(".questo").hover( function (e) { $(this).addClass('animated shake'); }, function(e){ $(this).removeClass('animated shake'); });

Comments

0

i found best solution. we have to pause the animation on any class and also we can set animation s running to start that animation on that html element reference

.animatedhover {
  animation-play-state: paused;
}
.animatedhover:hover {
  animation-play-state: running;
}

1 Comment

Nope, this is not best. If you mouse out from element, animation is frost on last frame, instead, it should finish animation and start over on next hover event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.