Currently, I am in the process of creating a map of the United States. When hovering over any specific state, my goal is to replace the image with one of a different color.
The issue I am encountering lies in the fact that the image gets replaced and a new one is loaded each time there is a hover event.
My HTML structure looks like this:
<img class="state" id="alaska" src="img/united-states_Alaska.png" alt="alaska">
<img class="state" id="hawaii" src="img/united-states_hawaii.png" alt="hawaii">
Below is the jQuery code I am implementing:
$('.interactive-map img').each(function(e){
var src = $(this).attr('src');
$(this).hover(function(){
$(this).attr('src', src.replace('.png', '-hover.png'));
}, function(){
$(this).attr('src', src);
});
});
I am interested in finding an alternative method to either preload the images using JavaScript or prevent a new image request upon every hover action. Ideally, I would like to make these optimizations within the JavaScript without making significant changes to the HTML or CSS.