隐藏的<div>中的动画背景图像不会加载或加载不动画

use*_*ser 4 html javascript google-chrome progress-bar

我花了一整天的时间来创建一个脚本,在"提交"中隐藏表单并显示隐藏的动画进度条.问题是Internet Explorer不会在隐藏的div中显示动画gif图像.图像是静态的.我访问了很多网站,发现了一个脚本,它使用:

document.getElementById(id).style.backgroundImage ='url(/images/load.gif)';

最后,我的脚本可以在Internet Explorer,Firefox,Opera中运行但是...... Google Chrome根本不显示图像.我只看到div文本.经过多次测试后,我发现了以下内容:在Google Chrome浏览器中查看背景图片的唯一方法是在页面中的某个位置(隐藏div之外)包含1px维度的相同图像:

<img src="/images/load.gif" width="1" heigh="1" /> 
Run Code Online (Sandbox Code Playgroud)

这样做了但是......在这个脏解决方案之后,Microsoft Explorer出于某种原因再次将图像显示为静态.所以,我的问题是:有什么方法可以强制Gogle Chrome显示图片吗?谢谢.这是我的脚本:

<script language="JavaScript" type="text/javascript">

function ver (id, elementId){
if (document.getElementById('espera').style.visibility == "visible") {
    return false;
}else{
var esplit = document.forms[0]['userfile'].value.split(".");
ext = esplit[esplit.length-1];
    if (document.forms[0]['userfile'].value == '') {
        alert('Please select a file');
        return false;
    }else{
        if ((ext.toLowerCase() == 'jpg')) {
            document.getElementById(id).style.position = 'absolute';
            document.getElementById(id).style.display = 'inline';
            document.getElementById(id).style.visibility = "visible";
            document.getElementById(id).style.backgroundImage = 'url(/images/load.gif)';
            document.getElementById(id).style.height = "100px";
            document.getElementById(id).style.backgroundColor = '#f3f3f3';
            document.getElementById(id).style.backgroundRepeat = "no-repeat";
            document.getElementById(id).style.backgroundPosition = "50% 50%";

        var element;
            if (document.all)
                element = document.all[elementId];
            else if (document.getElementById)
                element = document.getElementById(elementId);
            if (element && element.style)
                element.style.display = 'none'; 

            return true;
        }else{
            alert('This is not a jpg file');    
            return false;
        }
    }
}
}  
</script>


<div id="frmDiv">
<form  enctype="multipart/form-data" action="/upload.php" method="post" name="upload3" onsubmit="return ver('espera','frmDiv');">
<input type="hidden" name="max_file_size" value="4194304" />
<table border="0" cellspacing="1" cellpadding="2" width="100%">
<tr bgcolor="#f5f5f5">
<td>File (jpg)</td>
<td>
<input type="file" name="userfile" class="upf" /></td></tr>
<tr bgcolor="#f5f5f5">
<td>&nbsp;</td>
<td>
<input class="upf2" type="submit" name="add" value="Upload" />
</td></tr></table></form>
</div>


<div id="espera" style="display:none;text-align:center;float:left;width:753px;">&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />
&nbsp;<br />Please wait...<br />&nbsp;
</div>
Run Code Online (Sandbox Code Playgroud)

Dav*_*och 8

尝试:

(new Image).src="/images/load.gif";
Run Code Online (Sandbox Code Playgroud)

大G本身使用这种技术在其主页上预加载DNS查找.


更新 由于令人难以置信的-1我收到了...我需要解释为什么上述可能对那些在他们不了解给定解决方案的可能性的情况下进行downvote的工作有效.

谷歌浏览器可以"智能地"通过分析它们是否实际显示在页面上来"智能地"选择要下载的资源(类似于浏览器不下载每个背景图像:在CSS文件中的url(图像)).如果这是真的,则以下情况也可能如此:如果要显示"load.gif"图像的时间少于图像下载所用的时间,则显示图像不显示完全(即使它只是被下载).

通过使用'(新图像).src ="image.gif";'预加载图像 方法我们确保图像在浏览器的缓存中就绪,因此在需要时立即可用.

至于为什么Internet Exploder只显示一帧,我不确定.页面中必须有其他变量导致此行为(长时间运行的脚本,限制在GIF本身编码的循环数,?).