我第一次查看带有动画 .gif 的页面时,它在页面加载时播放良好(持续约 2 秒)。
刷新 (F5) 时,.gif 不再播放,仅显示 gif 动画的最后一帧。
我能做些什么来确保它每次都能播放吗?
我第一次查看带有动画 .gif 的页面时,它在页面加载时播放良好(持续约 2 秒)。
刷新 (F5) 时,.gif 不再播放,仅显示 gif 动画的最后一帧。
我能做些什么来确保它每次都能播放吗?
For the PHP the much better option then using date("Ymdgis");
is using time()
, like this:
<img src="picturePath.gif?<?php echo time();?>" />
影响每个浏览器的奇怪行为。
我通常用这个脚本手动刷新它(它使用 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>
这将刷新图像的 src 添加当前日期,因此浏览器将重新加载它,认为这是一个新图像。
否则以 PHP 方式(我更喜欢这个):
<img src="picturePath.gif?<?php echo date("Ymdgis");?>" />
//for the browser it will seems that's a new picture!
<img src="picturePath.gif?2012092011207">
对我有用的解决方法是使用 Javascript 手动重新加载 GIF
在 CSS 上实现的 GIF(背景图像)
var element = document.getElementById(name);
element.style.backgroundImage = "none";
element.style.backgroundImage = "url(imagepath.gif?"+new Date().getTime()+")";
在 HTML 上实现的 GIF(img 标签)
var element = document.getElementById(name);
element.src = "";
element.src = "imagepath.gif?"+new Date().getTime();
感谢@sekmo
这行得通,只需要它下面的一行,我建议一开始不要填写<img>
src 属性,这样页面就不会尝试加载任何不必要的资源。
<img id="gif" src=""/>
<script>document.getElementById('gif').src="path_to_picture.gif?a="+Math.random()</script>
可能是您的浏览器仅显示缓存版本。尝试按住 shift 并刷新,看看它是否有效。