Animated background image in a hidden <div> doesn't load or loads not animated
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>
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, ?).
精彩评论