caktux

caktux putting things together

  • blog
  • projects
  • about
Home » Blogs » caktux's blog

Rollovers take #2009

by caktux | Mon, 02/02/2009 - 23:35
/*
  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);
 
  • caktux's blog
  • Add new comment
  • jquery
 
 

Comments

by Anonymous | Fri, 06/19/2009 - 06:22

Rollover

Wouldn't it be easier to use just a CSS class with :hover ?

  • reply
 
  • Seeding torrents from your Trash with Transmission
  • Image placement module for Drupal 6
  • Loader module for Drupal - Page loading progress evolved
  • SUY! jQuery UI based "IE6 is outdated" notification plugin
  • Page loading progress with jQuery
  • D7UX mindmap
  • Display problems with NVidia card on OS X and bad website CSS
  • Rollovers take #2009
  • Fully circular jCarousel for fancy content switching
  • Sliding thumbnail links
more

lately...

  • January 2009 (4)
  • February 2009 (2)
  • April 2009 (3)
  • May 2009 (3)
  • June 2009 (3)

hey!

Using something I did and made a few bucks out of it?
 
© Vincent Gariépy
Powered by Pressflow, an open source content management system