Restart an animated GIF from JavaScript without reloading the image

て烟熏妆下的殇ゞ 提交于 2019-11-27 03:51:27

You should preload your images into code.

var image = new Image();
image.src = "path";

when you want to use:

nextimg_img.attr('src', image.src);

Then when you swap the src out just swap from the preloaded image objects. That should do the trick to avoid redownloading.

// reset a gif in javascript
img.src = "your_image_url.gif"+"?a="+Math.random();

Tada!

Please note the GIF will be downloaded every time so keep it a small file.

I just solved it. Place this code (or another static image if you like) on the page first:

<img src="data:image/gif;base64,">

Then replace it with the following code when you are ready to play it:

<img src="data:image/gif;base64,R0lGODl...AH0AvI">

It will play from the beginning of the gif.

The only problem is you cannot use the URL of the gif but only its base64.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!