First of all,check out this image
Gmail uses this image to display the animated emoticon.
How can we show such animation using a png image?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to get the background from multiple images by
- void before promise syntax
Take a look at this:
http://www.otanistudio.com/swt/sprite_explosions/ and http://www.alistapart.com/articles/sprites The answer lies within.
CMS's answer is fine, but there's also the APNG (animated PNG) format that you may want to use instead. Of course the first frame (the one displayed even by browsers that don't support APNG) should be the "ending" frame and just specify to skip the first frame in the file.
Set the background image of an element to the first image, then use javascript to change the image by altering the style every x milliseconds.
CSS @keyframes can be used in this case
You can do it with TweenMax and steppedEase easing : http://codepen.io/burnandbass/pen/FfeAa or http://codepen.io/burnandbass/pen/qAhpj whatever you choose :)
I leave you a rough example so you can get a starting point:
I will use a simple div element, with the
width
andheight
that the animated image will have, the png sprite asbackground-image
andbackground-repeat
set tono-repeat
CSS Needed:
Markup needed:
The trick is basically to scroll the background image sprite up, using the
background-position
CSS property.We need to know the
height
of the animated image (to know how much we will scroll up each time) and how many times to scroll (how many frames will have the animation).JavaScript implementation:
EDIT: You can also set the CSS properties programmatically so you don't have to define any style on your page, and make a constructor function from the above example, that will allow you to show multiple sprite animations simultaneously:
Usage:
Implementation:
Notice that I added a
stopAnimation
method, so you can later stop a specified animation just by calling it, for example:Check the above example here.