Implementing jQuery's shake effect with animate

前端 未结 9 2491
迷失自我
迷失自我 2020-12-14 10:38

I\'ve been given a cut down subset of the jQuery lib one of the key features I\'m missing is the .effect functions. I do however have .animate. I w

9条回答
  •  暖寄归人
    2020-12-14 10:59

    This is a more clean and smooth way to do the animation.

    jQuery.fn.shake = function(shakes, distance, duration) {
        if(shakes > 0) {
            this.each(function() {
                var $el = $(this);
                var left = $el.css('left');
                $el.animate({left: "-=" + distance}, duration, function(){
                    $el.animate({left: "+=" + distance * 2}, duration, function() {
                        $el.animate({left: left}, duration, function() {
                            $el.shake(shakes-1, distance, duration); });});
                });
            });
        }
        return this;
    };
    

提交回复
热议问题