Implementing jQuery's shake effect with animate

前端 未结 9 2460
迷失自我
迷失自我 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 11:11

    I wrote some time ago a few simple jquery animations:

    https://github.com/yckart/jquery-custom-animations

    /**
     * @param {number} times - The number of shakes
     * @param {number} duration - The speed amount
     * @param {string} easing - The easing method
     * @param {function} complete - A callback function
     */
    jQuery.fn.shake =
    jQuery.fn.wiggle = function (times, duration, easing, complete) {
        var self = this;
    
        if (times > 0) {
            this.animate({
                marginLeft: times-- % 2 === 0 ? -15 : 15
            }, duration, easing, function () {
                self.wiggle(times, duration, easing, complete);
            });
        } else {
            this.animate({
                marginLeft: 0
            }, duration, easing, function () {
                if (jQuery.isFunction(complete)) {
                    complete();
                }
            });
        }
        return this;
    };
    

提交回复
热议问题