/* Usage: $('.ro').rollover(); Options: 'states': For 'myimage_on.png' and 'myimage_off.png': $('.ro').rollover({'states':['_on','_off']}); or for 'img_ro.png' and 'img.png' (default) $('.ro').rollover({'states':['_ro','']}); */ (function($) { $.fn.rollover = function(options) { return $.fn.rollover.init(this, options); }; $.fn.rollover.init = function(images, options) { var settings = { 'states': ['_ro',''] }; if (options) $.extend(settings, options); if ($(images).length > 0) { $(images).each( function( i, img ) { // Reset already active images (might be from an active menu item) var offSrc = $(this).attr('src').replace(settings.states[0] + '.',settings.states[1] + '.'); var extension = offSrc.substring(offSrc.lastIndexOf('.'),offSrc.length); var onSrc = offSrc.replace(extension,settings.states[0] + extension); // Preload $('<img>').attr( 'src', onSrc ); $(this).hover( function () { $(this).attr( 'src', onSrc ); }, function () { $(this).attr( 'src', offSrc ); } ); }); } return images; }; })(jQuery);
Comments
Rollover
Wouldn't it be easier to use just a CSS class with :hover ?