0

My own jQuery shake plugin

JQuery

A newfound love and it with jQuery, and here why I can do more and write less just like it says. I have also just created my own jQuery shake plugIn and I have to say it was a lot easier than I thought it would be. So here it is.


<img src="/site/assets/uploads/photogalleries/tn/gravatar.jpg" style="position:relative;" class="shake">
<script>
jQuery.fn.shakeit = function(times){
   if(times == 'undefined')
      times=1;
   this.each(
      function(init){
         var jqNode = $(this);
         for (var x = 1; x <= times; x++){
            jqNode
               .animate({ left: -25 },10)
             .animate({ left: 0 },50)
             .animate({ left: 25 },10)
             .animate({ left: 0 },50);
         }
         
         
      }
   
   );
   return this;

}

(document).ready(function(){
   $('img.shake').bind('click',
         function(){
            $(this).shakeit(3);
         
         }
      );
}
);
</script>

tags:
jquery
Mountain/\Ash said:
 
Hay - thanks for the script. Just wanted to post up some modifications I made to it:
- fixed if no shake count arg is passed
-changed so that it will not move absolute positioned items from their original position

$.fn.shakeit = function(times) {
   if (times == undefined)
      times = 1;
   this.each(function(init) {
      var jqNode = $(this);
      var currentleft = parseInt( jqNode.css('left') );
      for (var x = 1; x <= times; x ) {
         jqNode
            .animate({ left: currentleft -25 }, 10)
            .animate({ left: currentleft }, 50)
            .animate({ left: currentleft 25 }, 10)
            .animate({ left: currentleft }, 50);
      }
   });
   return this;
}
 
posted 233 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Matt  Graf
View Matt Graf's profile on LinkedIn