gif animation not playing on refresh

旧城冷巷雨未停 提交于 2019-12-18 07:33:15

问题


First time I view the page with an animated .gif it plays fine on page load (lasts about 2 secs).

On refresh (F5), the .gif no longer plays and only the last frame of gif animation is shown.

Is there anything I can do do to make sure it plays everytime?


回答1:


For the PHP the much better option then using date("Ymdgis"); is using time(), like this:

<img src="picturePath.gif?<?php echo time();?>" />



回答2:


Strange behavior that's affects every browser..
I usually refresh it manually with this script (it uses jQuery)

<img id="gif_animata" src="picturePath.gif">
<script type="text/javascript">
    var gifSource = $('#gif_animata').attr('src'); //get the source in the var
    $('#gif_animata').attr('src', ""); //erase the source     
    $('#gif_animata').attr('src', gifSource+"?"+new Date().getTime()); //add the date to the source of the image... :-) 
</script>

This will refresh the src of the image adding the current date, so the browser will re-load it, thinking that's a new image.

Otherwise in PHP way (I prefer this one):

<img src="picturePath.gif?<?php echo date("Ymdgis");?>" />

//for the browser it will seems that's a new picture!
<img src="picturePath.gif?2012092011207">



回答3:


The workaround that works for me for this issue is to reload the GIF manually using Javascript

GIF implemented on CSS (background-images)

var element = document.getElementById(name);
element.style.backgroundImage = "none";  
element.style.backgroundImage = "url(imagepath.gif?"+new Date().getTime()+")";

GIF implemented on HTML (img tag)

var element = document.getElementById(name);
element.src = "";  
element.src = "imagepath.gif?"+new Date().getTime();

Thanks to @sekmo




回答4:


This works, only requires one line below it and I suggest not filling the <img> src attribute at first so the page doesn't try to load any unnecessary resources.

<img id="gif" src=""/>
<script>document.getElementById('gif').src="path_to_picture.gif?a="+Math.random()</script>



回答5:


It could be that your browser is just showing the cached version. Try holding shift and refreshing, and see if it works.



来源:https://stackoverflow.com/questions/11455788/gif-animation-not-playing-on-refresh

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