Hover over an image to see the js change.
These images are located in different directories and have different names.
They are sharing the same script to alter the hover states.
The only requirements to ensure this works are:
The following script NEEDS to be within a doc ready function
function getHoverPath (imgPath) {
//split imgPath after each '/' and create array to path directories and file name
var srcPathArray = imgPath.split('/');
//find image file name get length of srcPathArray and retrieve last item which is the image file ex: "images/buttons/IMG.gif"
var imgFile = srcPathArray[srcPathArray.length-1];
//image file name array split at "." to get:[fileName, ext]
var imgArray = imgFile.split('.');
//img name (first item in imgArray)
var imgName = imgArray[0];
//image ext (second item in imgArray)
var imgExt = imgArray[1];
//image src path
var srcPath = imgPath.split(imgFile , 1);
// concatenate path and add "_hover" to image file name
return srcPath + imgName+'_hover.'+ imgExt;
};
// get hover button array
var hoveredButtons = new Array();
$('.js_uniButton').mouseover (function() {
$this = $(this);
//get image path of this instance
var imgPath = $this.children('img').attr('src');
//cache img path url on initial mouseover
if (typeof(hoveredButtons[ imgPath ]) == 'undefined') {
hoveredButtons[ imgPath ] = getHoverPath( imgPath );
//console.log('cache image path ' + imgPath + ' and hover path ' + hoveredButtons[ imgPath ]);
}
$this.children('img').attr('src' , hoveredButtons[ imgPath ]);
//mouseout actions
$this.mouseout(function() {
//define this handler
var $this = $(this), handler = arguments.callee;
//unbind mouseout handler
$this.unbind('mouseout', handler);
// on mouseout, swap current image with original image path
$this.children('img').attr('src' , imgPath);
});
});
Hover over image to see state change