Permalink
Browse files

source code

  • Loading branch information...
1 parent 7ae3744 commit 14b067a04ec716a195ae67c0aff6e82d81bc1209 @myang-git committed Feb 26, 2012
Showing with 29 additions and 0 deletions.
  1. +29 −0 jquery-element-shaker.js
@@ -0,0 +1,29 @@
+
+/**
+ * jQuery Element Shaker
+ * Shake an element for specified duration and drift range
+ * @param params dur: duration in millisecond, drift: maximum displacement in pixel
+ */
+jQuery.fn.shake = function(params) {
+ var dur = ('dur' in params) ? params.dur : 500;
+ var drift = ('drift' in params) ? params.drift : 5;
+ var element = $(this);
+ var pos = element.position();
+ var cssLeft = element.css('left');
+ var cssTop = element.css('top');
+ var cssPosition = element.css('position');
+ var timer;
+ var v = function () {
+ var dLeft = (Math.random()>0.5 ? 1.0 : -1.0) * Math.floor(drift * Math.random());
+ var dTop = (Math.random()>0.5 ? 1.0 : -1.0) * Math.floor(drift * Math.random());
+ var newLeft = pos.left + dLeft;
+ var newTop = pos.top + dTop;
+ element.css({left: newLeft, top: newTop, position: 'relative'});
+ dur = dur - 10;
+ if(dur<=0) {
+ element.css({left: cssLeft, top: cssTop, position: cssPosition});
+ clearInterval(timer);
+ }
+ };
+ timer = setInterval(v, 10);
+};

0 comments on commit 14b067a

Please sign in to comment.