8

So, I have now made jQuery Ajax code that checks that the username and password are correct. But instead of just displaying a error message, I'd like to shake the element as well.

Define Shake?

That kind of shake that wordpress has. Go to wordpress.com/wp-login.php and fill there some random info and the element shakes.

That shake can be done by Animate.css.

What's the problem then?

When the login fails, jQuery makes this.

$('.element').addClass('shake');

But because the shake element has CSS Animations that run only once, we won't be needing the CSS shake class after it shaked the element.

So I tried:

$('.element').addClass('shake').removeClass('shake');

But that happens all too quickly.

So I tried again:

$('.element').addClass('shake').delay(1000).removeClass('shake');

Still not play the animation and then remove the class .shake from the .element. If the user enters wrong details more then once then shake won't work.

You can play with the fiddle here.

Goal is to be able to shake the element more then once by clicking the Shake button.

3 Answers 3

16

You could use the following to remove the class when the animation completes.

Updated Example

$(function () {
    $('button').click(function () {
        el = $('.element');
        el.addClass('shake');
        el.one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
        function (e) {
            el.removeClass('shake');
        });
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

That actually works! But I have no clue how it works since I don't really know much JS
I guess I have to look more into .one()
Daaamn I've been looking for this solution all over the place!! Been trying some hacks adding and removing the animation attribute but no luck. Now with this worked like a charm!!! thumbs up! Thank you
@Cʜᴜɴ - Awesome, that's good to know! I'm glad these answers are helping so many people.
2

Just remove the shake class, add a small time out and then add the shake class. That will make it shake every time:

$(function(){
$('button').click(function() {
    $('.element').removeClass('shake');
    setTimeout(function(){
          $('.element').addClass('shake');
    }, 50);
});

});

2 Comments

This works, but it produces a weird flicker for some reason: jsfiddle.net/VYuec (Chrome)
I can't reproduce the flickering here, but I would suggest: Let it shake first, have the time out afterwards and remove the class after the time out. That should avoid flickering.
1

addClass() and removeClass() don't respect delay(), so they'll ignore the delay and just do what they were told to like the delay() doesn't exist

fadeIn() does though.

So if you do this, it should work correctly. This calls the anonymous function to remove class shake after delay and fadeIn have finished.

$('.element').addClass('shake').delay(1000).fadeIn(0, function() { $(this).removeClass('shake'); });

3 Comments

There is no need for the element to fadeOut
It looks like @Stilly.stack used .fadeOut in order for .delay to work, otherwise .delay won't run (only runs against animations and not add/remove class)
I mean that now when jsfiddle.net/A8K3w/3 you hit Shake, it disappears after the animation is completed.

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.