Is it possible to restart an animated GIF used as background-image
?
Consider this HTML:
<div id="face">
<div id="eyes"></eyes>
</div>
And this style:
#eyes.blink {
background-image:url('blink.gif');
}
I would like the blink.gif
animation to play every time I add the class blink
to #eyes
, not just the first time.
I expected this to work:
function startBlink() {
$('#eyes').addClass('blink');
}
function stopBlink() {
$('#eyes').removeClass('blink');
}
The problem is that both Firefox and WebKit browser do not play a background-image GIF animation again once it has played once. Adding/removing the class blink only works the first time.
There is an alternative that does not reload the GIF every time and waste bandwidth.
It involves storing the GIF as Base64 in memory (circumventing browser cache), and uses the FileReader API (which seems to be supported in all modern browsers). Note that loading images this way is subject to cross-origin policy (unlike the image reload solutions.)
Update: Browser caching is getting smarter about caching background image data URI's, causing the animation not to start over. I found I had to add a cache-busting random string to the data url now (which according to the DataURI Scheme, should be considered an optional attribute. Tested in Chrome & IE Edge.)
See it in action: http://jsfiddle.net/jcward/nknLrtzL/10/
Here's how it works. This function loads the image as a Base64-encoded string.
Then, any time you want to restart the GIF animation, change the
background-image
property tonone
, then the base64 string (in some browsers, you need to re-add the child to trigger the update without a setTimeout):Thanks to this answer for the
toDataURL
function (with fix for IE11.)I combined several parts of the solution to make one whole solution that solves (hopefully) all problems:
background-image
)In my solution i create helper images that are added to the body but hidden in a way so they are still rendered by the browser but won't interact with the page visually using
position: absolute; left: -5000px;
.A reference to our helper images is cached in
resetHelperImages
so we can reuse them for the same image in subsequent calls.I am using jQuery for my example, but it could be adapted to work without jQuery, too.
Tested in: Chrome (Version 43.0.2357.130 m)
For some reason this works:
This requires an actual image DOM element to be appended to the page, but you can hide it with
visibility: hidden
. This doesn't require the image to be downloaded over the network multiple times.I only tested this in Firefox and Chrome. Not sure about other browsers.
Just because I still need this every now and then I figured the pure JS function I use might be helpful for someone else. This is a pure JS way of restarting an animated gif, without reloading it. You can call this from a link and/or document load event.
On some browsers you only need to reset the img.src to itself and it works fine. On IE you need to clear it before resetting it. This resetGif() picks the image name from the image id. This is handy in case you ever change the actual image link for a given id because you do not have to remember to change the resetGiF() calls.
--Nico
Regarding this answer posted by Frederic Leitenberger, I found it to work wonderfully.
However, it breaks down if your background-image has multiple, layered parts, like this:
To get around this limitation, I modified the line that finds the background image url, like so:
This uses a regular expression to extract just the URL portion of the background-image.
I would have added this as a comment to the linked answer, but I'm a noob without reputation, so was blocked from doing so. Those with adequate rep may want to add the line to the actual answer.
I've found you can also add a
?+Math.random()
to the end of the picture src and it'll reload the .gif.