第一次加载页面时,如果有带动画的 gif 文件,它会正常播放(持续大约 2 秒钟)。
但是在刷新页面(按 F5)之后,这个 gif 文件将不再播放,只会显示最后一帧的动画。
我能做些什么来确保每次都播放?
第一次加载页面时,如果有带动画的 gif 文件,它会正常播放(持续大约 2 秒钟)。
但是在刷新页面(按 F5)之后,这个 gif 文件将不再播放,只会显示最后一帧的动画。
我能做些什么来确保每次都播放?
对于 PHP 来说,比使用 date("Ymdgis");
更好的选项是使用 time()
,像这样:
<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">
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();
<img>
的src属性,这样页面就不会尝试加载任何不必要的资源。<img id="gif" src=""/>
<script>document.getElementById('gif').src="path_to_picture.gif?a="+Math.random()</script>
可能是因为您的浏览器只显示了缓存版本。尝试按住Shift键并刷新页面,看看是否有效。