Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ability to rotate also. rotatation by css is from http://css3please.com

  • Loading branch information...
commit 739f2ec22becf0ae8b57630a951bdd3121b71cfe 1 parent 0c54636
@myang-git authored
Showing with 33 additions and 7 deletions.
  1. +33 −7 jquery-element-shaker.js
View
40 jquery-element-shaker.js
@@ -9,19 +9,45 @@ jQuery.fn.shake = function(params, shakerDidFinish) {
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 cssOriginalLeft = element.css('left');
+ var cssOriginalTop = element.css('top');
+ var cssOriginalPosition = element.css('position');
var timer;
+
+ var randomSign = function() {
+ return (Math.random()>0.5 ? 1.0 : -1.0);
+ };
+
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 dLeft = randomSign() * Math.floor(drift * Math.random());
+ var dTop = randomSign() * Math.floor(drift * Math.random());
+ var rotationDeg = randomSign() * Math.floor(Math.random() * 3);
var newLeft = pos.left + dLeft;
var newTop = pos.top + dTop;
- element.css({left: newLeft, top: newTop, position: 'relative'});
+ var rotation = 'rotate(' + rotationDeg + 'deg)';
+ element.css({
+ 'left': newLeft,
+ 'top': newTop,
+ 'position': 'relative',
+ '-webkit-transform': rotation,
+ '-moz-transform': rotation,
+ '-ms-transform': rotation,
+ '-o-transform': rotation,
+ 'transform': rotation
+ })
+ ;
dur = dur - 10;
if(dur<=0) {
- element.css({left: cssLeft, top: cssTop, position: cssPosition});
+ element.css({
+ 'left': cssOriginalLeft,
+ 'top': cssOriginalTop,
+ 'position': cssOriginalPosition,
+ '-webkit-transform': 'rotate(0deg)',
+ '-moz-transform': 'rotate(0deg)',
+ '-ms-transform': 'rotate(0deg)',
+ '-o-transform': 'rotate(0deg)',
+ 'transform': 'rotate(0deg)'
+ });
clearInterval(timer);
if(shakerDidFinish!=undefined) {
shakerDidFinish();
Please sign in to comment.
Something went wrong with that request. Please try again.