Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an element shake every 5 seconds?

I have an html with the following structure:

<li><a class="" href="?sort-by=popular">Barang Terpopuler</a></li>

How do I make this element shake (move left and right) every 5 seconds using jQuery? Is there a built in animation for this?

like image 291
adit Avatar asked Mar 20 '23 17:03

adit


1 Answers

Try the following:

jQuery.fn.shake = function() {
    this.each(function(i) {
        $(this).css({
            "position" : "relative"
        });
        for (var x = 1; x <= 3; x++) {
            $(this).animate({
                left : -25
            }, 10).animate({
                left : 0
            }, 50).animate({
                left : 25
            }, 10).animate({
                left : 0
            }, 50);
        }
    });
    return this;
}
setInterval(function() {
    $('li').shake();
}, 5000); 

Fiddle

like image 87
Milind Anantwar Avatar answered Apr 02 '23 05:04

Milind Anantwar