0

I have a website with 500+ icons. I have found that simply letting them load on their own via img.src = "/images/icon1.png" takes far too long and other elements on my site that are further down in the page take like 30 seconds to load due to all the icons loading first. What is the best way to work with a high amount of icons like this? How would you preload it, and preferably not making it into one large sprite! Ideally i would like to server side initialize them into a javascript variable so they can be reused on the client side

MrRed
  • 677
  • 1
  • 6
  • 21

1 Answers1

0

You can preload those images in javascript using:

var img = new Image();
img.src = "/images/icon1.png";

A better method is to use an array like so:

var preloader = ["a.png", "b.jpg", "c.gif"];

var img = [];

for (i = 0; i < preloader.length; i++) {
    img[i] = new Image();
    img[i].src = preloader[i];
}

Check out these posts:

The definitive best way to preload images using JavaScript/jQuery?

JavaScript Preloading Images

What is the best way to preload multiple images in JavaScript?

Community
  • 1
  • 1
Abk
  • 2,137
  • 1
  • 23
  • 33