Implementing jQuery's shake effect with animate

前端 未结 9 2484
迷失自我
迷失自我 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 not perfect, but functional

        // Example: $('#<% =ButtonTest.ClientID %>').myshake(3, 120, 3, false);
        jQuery.fn.myshake = function (steps, duration, amount, vertical) {
            var s = steps || 3;
            var d = duration || 120;
            var a = amount || 3;
            var v = vertical || false;
            this.css('position', 'relative');
            var cur = parseInt(this.css(v ? "top" : "left"), 10);
            if (isNaN(cur))
                cur = 0;
    
            var ds = d / s;
    
            if (v) {
                for (i = 0; i < s; i++)
                    this.animate({ "top": cur + a + "px" }, ds).animate({ "top": cur - a + "px" }, ds);
                this.animate({ "top": cur }, 20);
            }
            else {
                for (i = 0; i < s; i++)
                    this.animate({ "left": cur + a }, ds).animate({ "left": cur - a + "px" }, ds);
                this.animate({ "left": cur }, 20);
            }
    
            return this;
        }
    

提交回复
热议问题