I am developing a website which requires the background image of a div to change on hover of a link.
The way it works is by:
<a href="index.php" title="Home ">
<li id="current">
Home<br \>
<span class="nav_desc">Text text</span>
</li>
</a>
<a href="about.php" title="About" id="about-link"
onmouseover="hover('about');"
onmouseout="hoverClear();">
<li id="about">
About<br \>
<span class="nav_desc">About me</span>
</li>
</a>
<a href="more.php" title="More"
onmouseover="hover('portfolio');"
onmouseout="hoverClear();">
<li id="more">
More<br \>
<span class="nav_desc">More More More
</span>
</li>
</a>
js:
function hoverClear(){
$('.navReflect').css("background-image", "url(images/"+page+"/reflect.png)");
}
function hover(hover){
$('.navReflect').css("background-image", "url(images/"+page+"/reflect-"+hover+".png)");
}
So, when a link is hovered it does a function to change div background image. But the issue is when the page is first loaded and the links hovered for the first time there is slow loading of the image.
But once they have loaded it works seamless. I expect this is an issue with it needed to be loaded. So is there a way I can preload the images before hand and still use the same method for the hover.
Stack your images as one image, and then add a class that shifts the image’s background property to the appropriate offset.
Not only does this turn multiple images into one HTTP request, it also means you don’t have to bother writing code to preload the hover state of the images.
This is called a sprite sheet.