I want my HTML page to load like normal, but I want one section of the page to only completely load after it has retrieved all images (static and rollovers) of that section.
I have this map I created using an image map and poly shapes. When a user rolls over a certain state, an entirely new map is loaded but it creates the appearance that only that section of that map is reloaded. I have not seen this done before because changing images or colors on an image map is not possible, so I had to do it this way.
Here is the link to the page: http://www.imaspy.com/unitedStatesMap.html
As you can see it takes a while for the rollover image to load over each state… but once they are all loaded the map works perfectly. I want all the images loads before the map appears. How can I do this with JavaScript?
Thanks!
Like others have already pointed out, the usual solution is to preload the mouseOver images. This is pretty basic, RobG’s answer should work fine.
However, since you have an image of the entire map for the mouseOver state of each state, that’s not very friendly for your visitor’s bandwidth. At 222 KB each, that amounts to
(50 × 222) / 1024 ≈10.8 MB just to load the site. I would advise you to find another solution.Update: I thought of a mistake in the solution I proposed. For a solution and the details, see the end of the answer.
If you want to stick with you current solution as much as possible, here’s what I would do:
That object (or named collection) might look something like this:
Your preloader could look like this:
Because
for .. inenumerates over the keys in an object,stateis the name of the key (and thus the name of the state). We save a new<img />object (wrapped in a jQuery object) into thestatecollection (so we have access to it later) and we apply thetopandleftpositions to it.Now, to show those preloaded images to the user on mouseOver:
This just appends the preloaded image to the
#mapContentdiv. Don’t forget to set#mapContent‘spositiontorelative, or the position of the image will be relative to the document, not to#mapContent.Update
I just thought of a glaring mistake in my proposed solution:
Since the hover images will cover the original image, the
mouseOutevent would fire as soon as the hover image is shown. Listening to themouseOutevent on the hover image instead of on the original image would circumvent this, but would result in a rectangular hit area.Fortunately, there’s a simple solution: add a transparent image on top of the original image (and the hover images) and apply the
imagemapto that, instead.Here is a proof on concept: http://jsfiddle.net/PPvG/t8prx/
A few pointers:
My apologies for the oversight. 🙂