This post expands on my original jQuery Preload and Swap Navigation Images post. The original code required you to enter the file extension (eg: jpg, png, gif). The modifications below make this jQuery hover/rollover/swap code totally dynamic 🙂
$(document).ready(function() { //preload images var preloadedImages = new Array(); $('#container img').each(function(i) { preloadedImages[i] = new Image(); var ext = $(this).attr('src').split('.').pop(); preloadedImages[i].src = $(this).attr('src').split('.' + ext).join('-hover.' + ext); }); //rollover effect $('#container a').hover(function() { var ext = $(this).find('img').attr('src').split('.').pop(); $(this).find('img').attr('src', $(this).find('img').attr('src').split('.' + ext).join('-hover.' + ext)); }, function() { var ext = $(this).find('img').attr('src').split('.').pop(); $(this).find('img').attr('src', $(this).find('img').attr('src').split('-hover.' + ext).join('.' + ext)); }); });
The only significant change from my original code to grab the file extension (var ext = value) before preloading or swapping the images…
The way I decided to do this was to split the string by into an array by ‘.’ and “pop” the last element. This also ensures that the extension length does not become an issue (‘.jpg’ and ‘.jpeg’ will work)
Now the preloader and rollovers can be used for ANY element with markup similar to…
<div id="container"> <a href="page1.htm"><img src="images/page1btn.jpg" alt=""></a> <a href="page2.htm"><img src="images/page2btn.jpg" alt=""></a> <a href="page3.htm"><img src="images/page3btn.jpg" alt=""></a> </div>
The script simply looks inside of anchor tags for an image to apply the effect to. Common with image-based navigation.
Feel free to comment 😀
I found your script on google and have found it to be extremely useful! Haven’t had any problems with it so far and the preloader works great.
I have slightly modified it so it can be applied to any element with a “src” attribute with the class “rollover”. I had a couple of image input elements I needed to have rollovers too.
Here is the link for anyone who might find it useful: http://borzale.com/public/js/hover.js
Cheers mate!
Here is borzale’s code…
I have written new code that uses the same method. The only difference is I used existing selectors instead of adding a new class.
Example:
To make sure others see your version, do you mind if I post your code here in case the linked file is ever (re)moved?
If using this for navigation, how could I add a selected state to use the same image as hover?
Just add class=”current” to the “img” tag and include something like this to set the image during page load…
Code hasn’t been tested but should achieve what you’re looking for. It will reverse the mouseover to the “current” image. If you want to avoid that, just add another hover event for that image that does nothing (eg: return false) 😉
Yeah no worries mate, post away!
Thanks again for the script.
One thing I forgot to mention for some who may not be JS savvy… if you need to manually preload an image after processing the automated ones, you can include this snippet later further down and duplicate it as needed for additional images.