user1157393
user1157393

Reputation:

Animate sprite with jquery

Does anybody know the best approach to animating a sprite using jquery. There are 3 stages of an image that i need to gradually to morph on mouseover and eventually landing on the final one until mouse off.

Here is the image:

enter image description here

Thanks!

Upvotes: 0

Views: 2318

Answers (3)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123367

I tried something using CSS3 transition and animation (it only works on modern browser, see caniuse)

you can see my example fiddle here: http://jsfiddle.net/fcalderan/hfs22/
Main benefits:

  • No javascript required, it's 100% pure CSS;
  • No need to load images;
  • Smooth animation.

HTML is simply <div><span></span></div>​: I tried to avoid the inner span in favour of pseudoelements: on Chrome, unfortunately, transitions don't properly work when applied to such elements (see bug http://code.google.com/p/chromium/issues/detail?id=54699)

the CSS

div {
   height: 100px;
   width : 100px;
   -webkit-border-radius: 100px;
   -msie-border-radius: 100px;
   -moz-border-radius: 100px;
   border-radius: 100px;

   -webkit-box-sizing: border-box; 
   -moz-box-sizing: border-box;
   box-sizing: border-box;

   border  : 20px red solid;    
   cursor  : pointer;

}

div span {

   -webkit-transform: rotate(45deg);
   -msie-transform: rotate(45deg);
   -moz-transform: rotate(45deg);
   -o-transform: rotate(45deg);
   transform: rotate(45deg);

   display  : block;
   margin     : 10px 0 0 -20px;
   background : #fff;
   width    : 102px;
   height   : 40px;

   -webkit-transition: all 2s ease;
   -msie-transition: all 2s ease;
   -moz-transition: all 2s ease;
   -o-transition: all 2s ease;
   transition: all 2s ease;


}

div:hover {
   -webkit-animation : rotation 2s ease;
   -msie-animation : rotation 2s ease;
   -moz-animation : rotation 2s ease;
   -o-animation : rotation 2s ease;
   animation : rotation 2s ease;
}

div:hover span {
   height : 0;
   margin-top : 30px;
}

@-moz-keyframes rotation   {
     0%     { -moz-transform: rotate(45deg); }
     100%   { -moz-transform: rotate(-135deg); }
}

@-webkit-keyframes rotation   {
     0%     { -webkit-transform: rotate(45deg); }
     100%   { -webkit-transform: rotate(-135deg); }
}

@-msie-keyframes rotation   {
     0%     { -msie-transform: rotate(45deg); }
     100%   { -msie-transform: rotate(-135deg); }
}

@-o-keyframes rotation   {
     0%     { -o-transform: rotate(45deg); }
     100%   { -o-transform: rotate(-135deg); }
}


@keyframes rotation   {
     0%     { transform: rotate(45deg); }
     100%   { transform: rotate(-135deg); }
}

Upvotes: 1

RonnyKnoxville
RonnyKnoxville

Reputation: 6394

This will look much nicer than your chosen answer:

See my example

Underneath is an identical static image. If you can make the white parts of this image transparent in photoshop or something this will have a really nice smooth closing effect.

Upvotes: 0

Liviu
Liviu

Reputation: 452

here's quick sketch

http://jsfiddle.net/VEuyH/1/

you just need to adjust the image to the animation, i set background to switch -151px every frame, you can also adjust the delay time, enjoy ;)

Upvotes: 0

Related Questions