jQuery plugin: Spiral animations


/ Published in: jQuery
Save to your folder(s)

A jQuery plugin that adds the .spiral() method, wich moves an element along an archimedean spiral path.


Copy this code and paste it in your HTML
  1. /*
  2.  * Spiral [v1.1]
  3.  * Distributed under the Do-wathever-the-hell-you-want-with-it License
  4.  *
  5.  * Web site: http://claudiobonifazi.com
  6.  * Blog: http://claudiobonifazi.com?p=4
  7.  * Twitter: @ClaudioBonifazi
  8.  */
  9.  
  10.  
  11.  
  12. /*
  13. Options = {
  14. Radius: numeric (pixels), horizontal distance between starting and final points;
  15. Duration: number of milliseconds (as for animate) ;
  16. Easing: the number of cicles - if it is higher than 2 it will make more turns and will assume a 'bouncing' deceleration effect
  17. Queue: boolean (as for animate) ;
  18. Ydirection: boolean. If true, the rotation starts going up instead of down.
  19. Xdirection: boolean. If true the movement will be from left to the right, instead of right to left.
  20. InsideOut: boolean. If true, the object will whirl out from the inside of the spiral. Note that setting it to true changes the sense of Xdirection ;
  21. },
  22. Callback = function called when the animation ends (as for animate) ;
  23. */
  24. (function($){
  25. $.fn.spiral = function( Options, Callback ){
  26. /*-- Input filtering --*/
  27. Opt= {
  28. Radius: 150,
  29. Duration: 100,
  30. Easing: 2,
  31. Queue: false,
  32. Xdirection: false,
  33. Ydirection: false,
  34. InsideOut: false
  35. }
  36. $.extend(Opt,Options)
  37. if(!Callback)
  38. Callback= function(){return};
  39. /*-- Animation starts --*/
  40. return this.each(function(){
  41. var elem=$(this),
  42. start_l=parseInt(elem.css('margin-left')),
  43. start_t=parseInt(elem.css('margin-top')),
  44. start_z=elem.css('z-index');
  45. elem
  46. .animate({'z-index':start_z},{
  47. duration: Opt.Duration,
  48. complete: Callback,
  49. step:function(now,fx){
  50. fgamma = Opt.InsideOut ? Opt.Radius*(1+fx.pos) : Opt.Radius*(1-fx.pos);
  51. gamma=fx.pos*Opt.Easing*Math.PI;
  52. gamma=(Opt.Ydirection) ? -gamma : gamma;
  53. x=(Opt.Xdirection) ? (start_l+Opt.Radius-fgamma*Math.cos(gamma))+'px' : (start_l-Opt.Radius+fgamma*Math.cos(gamma))+'px';
  54. y=(start_t+fgamma*Math.sin(gamma))+'px';
  55. $(fx.elem).css({'margin-left':x,'margin-top':y})
  56. },
  57. queue: Opt.Queue
  58. })
  59. })
  60. }
  61. })(jQuery)

URL: http://claudiobonifazi.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.