Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: addClass, removeClass: animation works only once: why?

Tags:

jquery

css

When I use that:

$("#passwordConfig").removeClass('shake animated').addClass('shake animate');

This works only ONCE.

I need to add some timeOut to make it run as many times as I want!

$("#passwordConfig").removeClass('shake animated');
                    setTimeout(function() {
                        $("#passwordConfig").addClass('shake animated');
                    }, 50);

Any idea why ?

CSS:

.animated {
  -webkit-animation-duration: 1s;
  animation-duration: 1s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}

@-webkit-keyframes shake {
  0%, 100% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
  }

  10%, 30%, 50%, 70%, 90% {
    -webkit-transform: translate3d(-10px, 0, 0);
    transform: translate3d(-10px, 0, 0);
  }

  20%, 40%, 60%, 80% {
    -webkit-transform: translate3d(10px, 0, 0);
    transform: translate3d(10px, 0, 0);
  }
}

@keyframes shake {
  0%, 100% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
  }

  10%, 30%, 50%, 70%, 90% {
    -webkit-transform: translate3d(-10px, 0, 0);
    transform: translate3d(-10px, 0, 0);
  }

  20%, 40%, 60%, 80% {
    -webkit-transform: translate3d(10px, 0, 0);
    transform: translate3d(10px, 0, 0);
  }
}

.shake {
  -webkit-animation-name: shake;
  animation-name: shake;
}
like image 623
yarek Avatar asked Sep 26 '22 11:09

yarek


1 Answers

This is because removeClass and addClass happening so quickly it doesn't allow the DOM to catch up per say. You can do something like this if you really hate setTimeout:

$("#passwordConfig").removeClass('shake animated').delay(50).queue(
    function (next) {
        $(this).addClass('shake animated');
        next();
    }
);

http://jsfiddle.net/51dwhd2o/

Toggle class is fine too but seems like it would only work every other call of toggle.

like image 172
AtheistP3ace Avatar answered Sep 30 '22 01:09

AtheistP3ace