简体   繁体   English

base64编码动画gif作为CSS背景?

[英]base64 encoded animated gif as css background?

I have an animated gif encoded into my css as such: 我有一个动画gif编码到我的CSS中,如下:

.magicBg {
background-image: url(data:image/gif;base64,R0lGODlhAQBkAPcAAAAAAAEBAQICAgMDAwQEBAUFBQYGBgcHBwgICAkJCQoKCgsLCwwMDA0ND ...
}

This animated gif is encoded to play once. 这个动画gif被编码为播放一次。

However, once this animated gif plays once as background on an element, it never plays again. 然而,一旦这个动画gif作为元素的背景播放一次,它就再也不会播放了。 Using 运用

$('body').removeClass( 'magicBg' );
$('body').addClass( 'magicBg' )

Has no effect -- we start on the final frame of the gif. 没有效果 - 我们从gif的最后一帧开始。 Any ideas how to make this play from the beginning every time the class is added? 任何想法如何在每次添加课程时从头开始制作这个游戏?

Unfortunately, another answer suggests reloading the gif from the server using some random url parameters. 不幸的是, 另一个答案建议使用一些随机的url参数从服务器重新加载gif。 Alas, this is not going to work for my case as I am encoding the image into the css itself. 唉,这对我的情况不起作用,因为我将图像编码到css本身。 Can you add a random date stamp to base64 data? 你能为base64数据添加随机日期戳吗?

You can add additional bogus fields to the data url to force replaying of the animated gif: 您可以向数据网址添加其他虚假字段以强制重播动画gif:

//use date timestamp to ensure no duplicates
url(data:image/gif;bogus:ABCDEF;base64,R0lGODlhA...);

Glad to see the trend of embedding base64 -encoded loading gifs is taking off! 很高兴看到嵌入base64 -encoded 加载GIF的趋势正在起飞!

Back to the issue at hand, there`sa pretty common typo in your background declaration, fixed that for you . 回到手头的问题,你的背景声明中有一个相当常见的拼写错误,为你解决了这个问题 The following: 下列:

base64,R0lGODlhEAAQAOMAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkv/////////////////////...

should have been instead 本来应该是

base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwE...

Seriously though, you've probably just encoded a corrupted file (not impossible if you were using an online encoding tool). 但是说真的,你可能只是编写了一个损坏的文件(如果你使用的是在线编码工具,这不是不可能的)。

To troubleshoot the issue, I've first downloaded the file you've embedded ( right click > view background image > right click > save as... on Firefox). 要解决此问题,我首先下载了您嵌入的文件( 右键单击>查看背景图片>右键单击>另存为...在Firefox上)。 That image could not render correctly either: 该图像也无法正确呈现: 不会“旋转”!

Funny enough, it looked just fine in Finder file preview when I was uploading it to SO imgur . 有趣的是,当我将它上传到SO imgur时,它在Finder文件预览中看起来很好。 Broken index? 破指数? File upload terminated too soon? 文件上传太快了? I don't know enough about technicalities of the GIF format to offer insight beyond speculation, but I've encoded the fixed file. 我不太了解GIF格式的技术性,以提供超出推测的洞察力,但我编码了固定文件。

NB: no need to use quotes in url() 注意:不需要在url()使用引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM