I'm trying to make an image gallery that will show lots of art. Therefore the website will have lots of images. I noticed that when I viewed my code on another computer the images would take time to load. While on my computer they would display straight away and I guessing this is due to them already being cached.
So I did some looking around to see if I could prevent this and I found lots of people mentioning preloading images. However, I couldn't really find a solution that fits my desire or a explanation I could understand(I hate using code I don't understand). So I tried to have a go myself and it didn't work out, but I felt the logic I what I writing was right but needs tweaking I'm going to write my code below and any advice on what need to do next would be greatly appreciated.
Preloading images code:
var allImages = $(img).attr('src'); //select every image
var preloadImages = [];
//add images to empty array
for(var i = 0; i < allImages.length; i++ ) {
preloadImages.push(allImages[i]);
}
//then preload images
for(var i = 0; i < preloadImages.length; i++ ) {
new Image().src = preloadImages[i];
}
So that is the code I wrote to preload images on my page. Let me know where I'm going wrong. I'm really not sure how I go about selecting all images on my website and loading them into an array I can then cycle over?
Thanks for the responses I guess I didn't explain my problem that well. A lot of the solutions to preloading images seem to be manually typing in every image src into and array. What I was looking for was a way to select every image I have and then put them into an array without actually typing them in. Is that possible? My reason for that is when I keep adding more and more images it would be great if the preloading could just take care of itself.