重新启动动画 GIF 作为背景图像
是否可以重新启动用作背景图像的 GIF 动画?
考虑这个 HTML:
<div id="face">
<div id="eyes"></eyes>
</div>
和这种样式:
#eyes.blink {
background-image:url('blink.gif');
}
我希望每次将 blink
类添加到 #eyes
时都播放 blink.gif
动画,不仅仅是第一次。
我预计这会起作用:
function startBlink() {
$('#eyes').addClass('blink');
}
function stopBlink() {
$('#eyes').removeClass('blink');
}
问题是 Firefox 和 WebKit 浏览器在播放一次背景图像 GIF 动画后都不会再次播放。添加/删除类闪烁仅在第一次有效。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以通过重新加载 gif 动画来重播它。这对于带宽来说并不理想,尤其是当您的图像很大时,但它会强制重新启动动画。
在我的示例中,我添加和删除了
的
onclick
:实际演示。
You can get the animated gif to replay by reloading it. This isn't ideal for bandwidth, especially if your image is large, but it will force a restart of the animation.
In my example I'm adding and removing it
onclick
of<div id="animated">
:Demo of it in action.
我发现您还可以在图片 src 的末尾添加
?+Math.random()
,它会重新加载 .gif。I've found you can also add a
?+Math.random()
to the end of the picture src and it'll reload the .gif.有一种替代方法可以避免每次都重新加载 GIF 并浪费带宽。
它涉及将 GIF 作为 Base64 存储在内存中(绕过浏览器缓存),并使用 FileReader API(似乎支持 在所有现代浏览器中)。请注意,以这种方式加载图像受跨域策略的约束(与图像重新加载解决方案不同)。
更新:浏览器缓存在缓存背景图像数据 URI 方面变得越来越智能,导致动画无法重新开始。我发现我现在必须向数据 url 添加一个缓存破坏随机字符串(根据 DataURI Scheme< /a>,应该被认为是一个可选属性。在 Chrome 和 IE Edge 中测试。)
查看它的实际情况:http://jsfiddle.net/jcward/nknLrtzL/10/
这是它的工作原理。此函数将图像加载为 Base64 编码的字符串。
然后,任何时候您想要重新启动 GIF 动画,请将
background-image
属性更改为none
,然后将 base64 字符串(在某些浏览器中,您需要重新添加孩子在没有 setTimeout 的情况下触发更新):感谢 这个
toDataURL
函数的答案(针对 IE11 进行了修复。)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.)我将解决方案的几个部分组合起来,形成一个完整的解决方案,可以解决(希望)所有问题:
background-image
)在我的解决方案中,我创建了辅助图像,这些图像添加到主体中,但以某种方式隐藏,以便它们仍然存在渲染者浏览器但不会使用
position:absolute; 与页面进行视觉交互左:-5000px;
。对辅助图像的引用缓存在
resetHelperImages
中,因此我们可以在后续调用中将它们重用于同一图像。我在示例中使用 jQuery,但它也可以在没有 jQuery 的情况下工作。
测试环境:Chrome(版本 43.0.2357.130 m)
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)
您是否考虑过使用同一个图像两次(称为blink.gif 和blink2.gif),为它们添加两个类并在类之间切换?
Have you considered using the same image twice called blink.gif and blink2.gif, adding two classes for them and toggling between classes?
只是因为我仍然时不时地需要这个,我认为我使用的纯 JS 函数可能对其他人有帮助。这是重新启动动画 gif 的纯 JS 方式,无需重新加载它。您可以从链接和/或文档加载事件中调用它。
在某些浏览器上,您只需将 img.src 重置为自身即可正常工作。在 IE 上,您需要在重置之前清除它。这个resetGif()从图像id中选择图像名称。如果您更改给定 id 的实际图像链接,这会很方便,因为您不必记住更改 resetGiF() 调用。
——妮可
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
由于某种原因,这是可行的:
这需要将实际的图像 DOM 元素附加到页面,但您可以使用
visibility: hide
隐藏它。这不需要通过网络多次下载图像。我只在 Firefox 和 Chrome 中测试过。不确定其他浏览器是否如此。
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.
关于这个答案由Frederic发布Leitenberger,我发现它非常有效。
但是,如果您的背景图像具有多个分层部分,则它会崩溃,如下所示:
为了解决此限制,我修改了查找背景图像 url 的行,如下所示:
这使用正则表达式仅提取 URL 部分背景图像的。
我本想将此作为评论添加到链接的答案中,但我是一个没有声誉的菜鸟,因此被阻止这样做。那些具有足够代表的人可能希望将该行添加到实际答案中。
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.