Jump to content

Show animated GIF only when loaded


RLJ

Recommended Posts

Hi all,

 

I have an animated GIF banner on my website that is around 2MB. Currently, I use the following Javascript to preload the banner before the page is displayed:

 

<!-- preload images
banner = new Image();
banner.src = \"IMG/banner.gif\";
// End -->

 

However, for people with slower internet connections, I want to write some Javascript that will achieve the following:

 

- page loads up and displays with (small image size) loading.gif instead of banner.gif

- banner.gif starts downloading "in the background"

- when banner.gif has fully downloaded, loading.gif is replaced with banner.gif and banner.gif starts playing

 

This seems easy, but I'm new to Javascript so I could use some help! Can somebody show me how to do this?

 

Thanks!

 

P.S. if you're thinking why do I need Javascript to do this, it's because if I just include banner.gif as an HTML image (and without the pre-loading Javascript above), it starts playing (and jittering) before it's fully loaded. But perhaps this also depends on the browser.

 

Link to comment
https://forums.phpfreaks.com/topic/235695-show-animated-gif-only-when-loaded/
Share on other sites

You're forcing the user to download >2MB worth of data? Not such a problem on unlimited plans, but for those with limits (and especially for those with limits and slower connections) they certainly won't want that! Why does a banner even take 2MB?

I have tried the following:

<html> 
<head> 
<script type="text/javascript" language="javascript"> 
var banner = new Image();
var loading = new Image();
var bannerElement = document.getElementById("BANNER");
banner.src = "IMG/BANNER1-2.gif";
loading.src = "IMG/loading.gif";

banner.onload = function() {
    bannerElement.removeChild(bannerElement.lastChild);
    bannerElement.appendChild(banner);
};
bannerElement.removeChild(banner);
bannerElement.appendChild(loading);
</script> 
</head> 
    
<body> 
<div id="BANNER">
<img src="IMG/BANNER1-2.gif" alt="Banner" />
</div> 
</body> 

</html>

 

But it's not working, can someone see where I've gone wrong? Thanks.

 

A thanks, but I had already tried that as well, it doesn't seem to be working. What I have is the following:

<html> 
<head> 
<script type="text/javascript" language="javascript"> 
function bannerload(){
var banner = new Image();
var loading = new Image();
var bannerElement = document.getElementById("BANNER");
banner.src = "IMG/BANNER1-2.gif";
loading.src = "IMG/loading.gif";

banner.onload = function() {
    bannerElement.removeChild(bannerElement.lastChild);
    bannerElement.appendChild(banner);
};
bannerElement.removeChild(banner);
bannerElement.appendChild(loading);
}
</script> 
</head> 
    
<body onload="bannerload()"> 
<div id="BANNER">
<img src="IMG/BANNER1-2.gif" alt="Banner" />
</div> 
</body> 

</html>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.