隐藏的
中的动画背景图像不加载或加载时没有动画

发布于 2024-08-27 17:17:41 字数 3015 浏览 2 评论 0原文

我花了一整天的时间尝试制作一个脚本,该脚本在“提交”时隐藏表单并显示隐藏的动画进度条。问题是 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" /> 

这确实有效,但是......在这肮脏的之后解决方案 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>

I have spent the whole day trying to make a script which on "submit" hides the form and shows hidden with animated progress bar. The problem is that Internet Explorer doesn't show animated gif images in hidden divs. The images are static. I visited many websites and found a script which uses:

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

Finally, my script works in Internet Explorer, Firefox, Opera but... Google Chrome doesn't display the image at all. I see only div text. After many tests I discovered the following: the only way to see the background image in Google Chrome is to include the same image somewhere in the page (outside of hidden div) with 1px dimensions:

<img src="/images/load.gif" width="1" heigh="1" /> 

This did the trick but... after this dirty solution Microsoft Explorer for some reason shows the image as static again. So, my question is: is there any way how to force Gogle Chrome to show the image? Thanks. This is my script:

<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> </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;"> <br /> <br /> <br /> <br />
 <br />Please wait...<br /> 
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-09-03 17:17:41

尝试:

(new Image).src="/images/load.gif";

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


更新
由于我收到的令人难以置信的 -1...我需要解释为什么上述内容可能对那些在不理解给定解决方案的可能性时投反对票的傻瓜起作用。

Google Chrome 很可能通过分析资源是否实际显示在页面上来“智能”地选择要下载的资源(类似于浏览器不会下载每个 background-image:url(image)在 CSS 文件中)。如果这是真的,那么以下情况也可能是真的:
如果“load.gif”图像的显示时间小于下载图像所需的时间,那么看起来图像根本没有显示(即使它刚刚被下载)。

通过使用“(new Image).src ="image.gif";”预加载图像通过这种方法,我们确保图像在浏览器的缓存中准备就绪,从而在需要时立即可用。

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

Try:

(new Image).src="/images/load.gif";

The big G itself uses this technique to pre-load DNS lookups on their homepage.


UPDATE
Due to the boggling -1 I received...I need to explain why the above may work to the numbskulls that downvote when they don't understand the possibilities of a given solution.

Google Chrome may very well be "intelligently" choosing which resources to download by analyzing if they will actually be displayed on the page (similar to how browsers don't download every background-image:url(image) in a CSS file). If this is true the the following may also be true:
If the time the "load.gif" image is intended to be displayed is LESS than the time it takes for the image to be downloaded then it will appear like the image is not displayed at all (even though it is just being downloaded).

By pre-loading the image using the '(new Image).src ="image.gif";' approach we make sure the image will be ready in the browser's cache and thus immediately available when needed.

As to why Internet Exploder is only showing one frame I'm not sure. There must be other variables in the page causing this behavior (long running script, limit number of loops encoded in the GIF itself, ?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文