Solved

Preload Animated GIFs Before Animating - How?

Posted on 2007-04-03
7
1,355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

718 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