PDA

View Full Version : Preloading images for Accordion Menu script



AshleyQuick
12-07-2008, 02:31 AM
1) Script Title: Accordion Menu

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu-glossy.htm

3) Describe problem: I'd like to add a script that preloads all the images. Can someone assist?

jscheuer1
12-07-2008, 06:53 AM
Well, loading and preloading take the same amount of time, so if there are any images that will be displayed anyway, as the page loads, don't bother with them. Also, preloading is best used only for small images, like for a menu, so if I understand you correctly, it would be fine here. A nice and simple preload routine is:


<script type="text/javascript">
if(document.images)
(function(){
var myImages = ['some.gif', 'another.gif', 'image73.gif', 'bobs.gif'];
for (var s, i = myImages.length - 1; i > -1; --i){
s = myImages[i];
myImages[i] = new Image();
myImages[i].src = s;
}
})();
</script>

List as many images as you like, but try to keep it down to just essential, small images that won't otherwise be loaded with the page. The above may be placed in the head of your page, or its code:


if(document.images)
(function(){
var myImages = ['some.gif', 'another.gif', 'image73.gif', 'bobs.gif'];
for (var s, i = myImages.length - 1; i > -1; --i){
s = myImages[i];
myImages[i] = new Image();
myImages[i].src = s;
}
})();

may be placed at the beginning of the ddaccordion.js file. If placing it in an external file, it's best to use the absolute paths to the images.

AshleyQuick
12-07-2008, 01:19 PM
Very nice, thank you!