如何让一个元素每5秒震动一次?

adi*_*dit 5 javascript jquery

我有一个具有以下结构的 html:

<li><a class="" href="?sort-by=popular">Barang Terpopuler</a></li>
Run Code Online (Sandbox Code Playgroud)

如何使用 jQuery 让这个元素每 5 秒摇动一次(左右移动)?有为此内置的动画吗?

Mil*_*war 5

请尝试以下操作:

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); 
Run Code Online (Sandbox Code Playgroud)

小提琴