I’m making a site in which I’m using jQuery to change background images on a page. There will be quite a few images substituted through the course of the site.
By declaring variables like this at the beginning of the script, will it preload the images and therefore allow for faster substitution of images? Or, if I do it with too many images (like 50), will it slow things down? or do the variables use too much memory?
Are there any issues you see with this plan, or is there a better way?
var img1 = "url('images/wackawacka.jpg') repeat";
var img2 = "url('images/wabbaawacka.jpg') repeat";
var img3 etc...
If you’re just declaring those variables as strings, JavaScript won’t preload those images.
Take a look at this article for how to preload images with JavaScript: http://www.techrepublic.com/article/preloading-and-the-javascript-image-object/5214317
Using that method when the document is loaded, you can load all the images into the users’ cache after the page loads, so when they switch, they’ll be quickly accessible.