-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery.ui.shake.js
49 lines (43 loc) · 1.36 KB
/
jquery.ui.shake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(function($) {
$.fn.shake = function(o) {
if (typeof o === 'function')
o = {callback: o};
// Set options
var o = $.extend({
direction: "left",
distance: 20,
times: 3,
speed: 140,
easing: "swing"
}, o);
return this.each(function() {
// Create element
var el = $(this), props = {
position: el.css("position"),
top: el.css("top"),
bottom: el.css("bottom"),
left: el.css("left"),
right: el.css("right")
};
el.css("position", "relative");
// Adjust
var ref = (o.direction == "up" || o.direction == "down") ? "top" : "left";
var motion = (o.direction == "up" || o.direction == "left") ? "pos" : "neg";
// Animation
var animation = {}, animation1 = {}, animation2 = {};
animation[ref] = (motion == "pos" ? "-=" : "+=") + o.distance;
animation1[ref] = (motion == "pos" ? "+=" : "-=") + o.distance * 2;
animation2[ref] = (motion == "pos" ? "-=" : "+=") + o.distance * 2;
// Animate
el.animate(animation, o.speed, o.easing);
for (var i = 1; i < o.times; i++) { // Shakes
el.animate(animation1, o.speed, o.easing).animate(animation2, o.speed, o.easing);
};
el.animate(animation1, o.speed, o.easing).
animate(animation, o.speed / 2, o.easing, function(){ // Last shake
el.css(props); // Restore
if(o.callback) o.callback.apply(this, arguments); // Callback
});
});
};
})(jQuery);