Solved

Preload Animated GIFs Before Animating - How?

Posted on 2007-04-03
7
1,346 Views
Last Modified: 2013-12-25
Hi all,

Is there a way I can force the browser to load all the animated gifs fully before they are displayed or start animating on the page.  I have four animated gifs that will be seen in a square fasion, i.e.:

IMG1 | IMG2
--------------
IMG3 | IMG4

The problem I have now is that the IMG1 will download first and start animating before the others are loaded, and so forth, which kinda makes things look off when looking at the page.  I don't mind if the browser shows the first frame of each animated gif on the page as they come, but I don't want the actual animation of any of the images to start until all the frames of all the animated gifs are downloaded and ready to go.

Any help on how to accomplish this would be greatly appreciated.  Thanks.

SuperG03
0
Comment
Question by:SuperG03
  • 3
  • 3
7 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 18853224
Hi,

I don't have the code to hand but I've done this sort of thing before with preloading images. See this page to help you:

http://www.pageresource.com/jscript/jpreload.htm

Regards,

Lee
0
 
LVL 6

Expert Comment

by:nbkbar7
ID: 19002248



<head>
<script language=javascript>

      var IMG1 = new Image()
      IMG1.src="images/image1.jpg"

      var IMG2 = new Image()
      IMG2.src="images/image2.jpg"

      var IMG3 = new Image()
      IMG3.src="images/image3.jpg"

      var IMG4 = new Image()
      IMG4.src="images/image4.jpg"


function load_images() {

      document.getElementById("Image1").src = IMG1;
      document.getElementById("Image2").src = IMG2;
      document.getElementById("Image3").src = IMG3;
      document.getElementById("Image4").src = IMG4;
}


</script>
</head>

<body...

<img id="Image1" src="" .../>
<img id="Image2" src="" .../>
<img id="Image3" src="" .../>
<img id="Image4" src="" .../>



...


      
0
 

Author Comment

by:SuperG03
ID: 19005947
nbkbar7,

   I tried what you said, but it didn't work.  The first two animated gifs downloaded first and started going, then the next two came in.  I need all four of them to be started at the same time.  The only things I modified in what you posted is, all the "IMGx.src="images/imagex.jpg"", I have "IMGx.src="my image link.gif"" and then in the body, I have src="", to src="my image link.gif".  What could be the problem?  Did I miss something?  Thanks.

SuperG03
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Accepted Solution

by:
nbkbar7 earned 50 total points
ID: 19008626

OK, How about we put a DIV around the images and hide them until the page loads. when the page loads we show the div. They should all be running then...

lem'me know...

Dave


<html>
<head>
<style>
div#hiddenImages {
      display:none;      
}
</style>
<script>
      var IMG1 = new Image()
      IMG1.src="/images/img1.gif"
      var IMG2 = new Image()
      IMG2.src="/images/img2.gif"
      var IMG3 = new Image()
      IMG3.src="/images/img3.gif"
      var IMG4 = new Image()
      IMG4.src="/images/img4.gif"

function load_images() {
      document.getElementById("Image1").src = IMG1;
      document.getElementById("Image2").src = IMG2;
      document.getElementById("Image3").src = IMG3;
      document.getElementById("Image4").src = IMG4;
}

function pageLoaded () {
      document.getElementById("hiddenImages").style.display="block";
}

</script>
</head>
<body onload="pageLoaded()">

<DIV id="hiddenImages">
<img id="Image1" src="/images/img1.gif" />
<img id="Image2" src="/images/img2.gif" />
<br>
<img id="Image3" src="/images/img3.gif" />
<img id="Image4" src="/images/img4.gif" />
</DIV>

</body>
</html>
0
 

Author Comment

by:SuperG03
ID: 19013702
nbkbar7,

   That helped a little, because now the four images now show up on the screen at the same time.  However, the animations are still not together.  It appears that even though the images aren't shown on the screen, the animation seems to start "behind the scene" so to speak as they are downloaded.  I should mention the images are not hosted on the same server as the html file.  So, when the images are shown, they all come at the same time, but the animations are still at random times.  Is there anything else I can do?  Thanks.

SuperG03
0
 
LVL 6

Expert Comment

by:nbkbar7
ID: 19015373
mmmmm, I understand now...

OK, the real answer is No. You can't do exactly what you want, which I think is that you want them to load stopped and then start when you tell them to.

Having said that, you may be able to get closer...

keeping them hidden, load them all in place (so that we know we have them) and then replace all four of them using the image.src code with another small gif, maybe a 1 pixel kinda thing.

Once everything is loaded call a/the function to show them (you can do this with a button for testing). Then right before you show the DIV replace the pixel images with the animated gifs and show the DIV.

That will probably get you the closest but it's a hack at best and even then you'll have to figure out how to get each of the functions called at the right time...

Also know these things might help:

When you stop a page everything stops, all your animations anyway. After loading the page you could call window.stop (I think, at least in Firefox) which will stop the animation. I'm not sure you can restart it though without refreshing which may throw it off again.

Another thing that I know will stop the animation is if you modify the IMG tag somehow. But I don't know how you would restart it.

You might consider trying to learn to use flash. I know that there is a way to control animated  gifs if they're wrapped in flash code...

Hope that helps a little...

Sorry to disappoint... :)

Dave


0
 

Author Comment

by:SuperG03
ID: 19089831
nbkbar7,

   Thanks for the help and for the advice on how to possibly perfect it even more.  I will look into it, if/when I get some time.  Until then, the previous codes seems to work well enough.  Thanks again.

SuperG03
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now