<input type="text" class="animated">
.animated {
animation-duration: 1s;
animation-duration: 1s;
animation-fill-mode: both;
animation-fill-mode: both;
}
@media (prefers-reduced-motion) {
.animated {
animation: unset !important;
animation: unset !important;
transition: none !important;
transition: none !important;
}
}
@-webkit-keyframes shake {
from,
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
10%,
30%,
50%,
70%,
90% {
transform: translate3d(-10px, 0, 0);
transform: translate3d(-10px, 0, 0);
}
20%,
40%,
60%,
80% {
transform: translate3d(10px, 0, 0);
transform: translate3d(10px, 0, 0);
}
}
@keyframes shake {
from,
to {
transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
10%,
30%,
50%,
70%,
90% {
transform: translate3d(-10px, 0, 0);
transform: translate3d(-10px, 0, 0);
}
20%,
40%,
60%,
80% {
transform: translate3d(10px, 0, 0);
transform: translate3d(10px, 0, 0);
}
}
.shake {
animation-name: shake;
animation-name: shake;
}
$.fn.extend({
animateCss: function(animationName, callback) {
var animationEnd = (function(el) {
var animations = {
animation: 'animationend',
OAnimation: 'oAnimationEnd',
MozAnimation: 'mozAnimationEnd',
WebkitAnimation: 'webkitAnimationEnd',
};
for (var t in animations) {
if (el.style[t] !== undefined) {
return animations[t];
}
}
})(document.createElement('div'));
this.addClass('animated ' + animationName).one(animationEnd, function() {
$(this).removeClass('animated ' + animationName);
if (typeof callback === 'function') callback();
});
return this;
},
});
$('input').on('input', function() {
var value = $(this).val();
if (!value || value.length < 5) {
$(this).animateCss('shake', function() {
$(this).removeClass('shake');
});
} else {
$(this).removeClass('shake');
}
});
This Pen doesn't use any external CSS resources.