简体   繁体   English

如何在每个页面刷新/加载时进行“循环一次”动画GIF背景播放?

[英]How can I make a “loop once” animated GIF background play on every page refresh/load?

I have an animated GIF file as my div's background image, which was saved from Photoshop to loop only once. 我有一个动画GIF文件作为我的div的背景图像,它从Photoshop保存到循环只有一次。 So the only time it plays is on the original load of the page. 所以它播放的唯一时间是在页面的原始负载上。

Is there any method to essentially remove the background image and add it again on a page refresh? 有没有什么方法可以基本上删除背景图像并在页面刷新时再次添加它? Almost making the DOM think it's a new image thus looping it again? 几乎让DOM认为它是一个新的图像,从而再次循环它?

Here's the div: 这是div:

<div id="name-logo"></div>

and css: 和css:

  position: relative;
  width: 403px;
  height: 93px;
  margin: 50px auto 100px;
  background-image: url(my.gif);
  background-position: center;
  background-size: contain;
  background-repeat: no-repeat;

Thanks 谢谢

One way to achieve it is by versioning. 实现它的一种方法是通过版本控制。 This will make the dom believe that it a new image every. 这将使dom相信它每一个都是新的形象。

document.getElementById('name-logo').style.backgroundImage = "url('my.gif?v=" + new Date().valueOf() + "')"

I'm unsure if this will give you the exact result you're looking for, but you might be able to use JavaScript to set up an interval that runs every (however long your .gif naturally runs). 我不确定这是否会为您提供您正在寻找的确切结果,但您可以使用JavaScript来设置每个运行的间隔(无论您的.gif自然运行多长时间)。 So, if it takes a second for your .gif to run, you might do something like this: 所以,如果你的.gif运行需要一秒钟,你可能会这样做:

setInterval(function() {
     // clear out the current background image as it is
     document.getElementById('name-logo').style.backgroundImage = "url('')";
     // reset the background image
     document.getElementById('name-logo').style.backgroundImage = "url('my.gif')";
}, 1000);

Again, I haven't personally tested this, but it might lead you in a good direction. 同样,我没有亲自测试过这个,但它可能会引导你朝着一个好方向前进。

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

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