Simple jQuery image rollover script

Just a simple script I use to automate image rollovers that may be of use to others. Just include this javascript:

$(function() {
	$('.rollover').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	});
});

This will pick up an image that looks as follows, and setup the rollover image:

<img src="first.gif" hover="second.gif"  class="rollover"/>
  • del.icio.us
  • Digg
This entry was posted in javascript, jquery. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

25 Comments

  1. Posted April 22, 2008 at 9:04 pm | Permalink

    do you know how to do this INSIDE a wordpress post? I am not a programmer but I am trying to figure this out.

  2. brad
    Posted April 22, 2008 at 9:07 pm | Permalink

    For wordpress, I would add this javascript in a script block inside one of the template files, like header.php for your theme. Then in your wordpress post, just make sure your embedded image has the “hover” and “class” attributes (you can manually set those if you go to your html view if you use the rich text editor or wysiwyg editor).

  3. Posted June 9, 2008 at 5:41 am | Permalink

    Nice and easy to use script. Too bad it isnt w3c valid.

  4. Posted July 11, 2008 at 8:01 pm | Permalink

    Simple and quite elegant idea. However, this would break the compliance w/ XHTML so I personally would try a different approach

  5. brad
    Posted July 14, 2008 at 8:25 am | Permalink

    In attempt to comply with standards, I think taking an approach using the new custom data attributes presented for HTML 5 would solve compliance issues in the future:

    http://www.w3.org/html/wg/html5/#custom

    So rather than using the attribute ‘hover’, we could use ‘data-hover’, and still be standards compliant (once html 5 is in place).

  6. Posted November 5, 2008 at 2:43 pm | Permalink

    Long this is not met.

  7. jm
    Posted January 6, 2009 at 6:21 pm | Permalink

    This is invalid markup and should never be used!

  8. brad
    Posted January 6, 2009 at 8:57 pm | Permalink

    Custom attributes do not make html invalid. Valid html is not the same as a valid DTD. I’d suggest reading the following post in regards to Dojo and the topic of validating the html vs. validating the DTD.

    HTML 5 also has a specification for allowing custom data attributes that John Resig commented about awhile back.

  9. Posted February 23, 2009 at 8:07 am | Permalink

    Great idea and very light code. Thanks a lot.

  10. Posted July 15, 2009 at 7:26 pm | Permalink

    Thanks for the script. Neat and clean. Regarding the W3C Compliance….I simply used the name attribute of the img instead of the “made up” hover attribute. Since the name attribute is a recognised attribute it worked perfectly fine and was compliant with w3c standards. All good :) (Unless of course you use the name attribute elsewhere in your code)

  11. Chet
    Posted July 27, 2009 at 8:57 am | Permalink
  12. Chet
    Posted July 27, 2009 at 8:58 am | Permalink

    I meant to say “…a surrounding ANCHOR tag…”

  13. Posted August 5, 2009 at 1:03 pm | Permalink

    Simple and effective

  14. Anon
    Posted August 7, 2009 at 9:41 am | Permalink

    Hi, thanks for a very useful piece of code. I simplified it a bit for my implementation:


        $('img[hover]').hover(function() {
            var currentImg = $(this).attr('src');
            $(this).attr('src', $(this).attr('hover'));
            $(this).attr('hover', currentImg);
        }, function() {
            var currentImg = $(this).attr('src');
            $(this).attr('src', $(this).attr('hover'));
            $(this).attr('hover', currentImg);
        });

    This version doesn’t require a special class on the image. It just performs the rollover on any image that has the custom “hover” attribute.

  15. Scott
    Posted August 15, 2009 at 3:31 pm | Permalink

    @Anon: The new code doesnt seem to work for me.

  16. Don
    Posted September 21, 2009 at 6:01 am | Permalink

    Thanks for the helpful idea for image rollover. Considering the discussiona above regarding compliance, here is something even simpler and clearly compliant – if you’re able to name your images in a consistent fashion, such as “myimage_orig.jpg” and “myimage_rollover.jpg”:

    $(’img.hover’).hover(function() {
    $(this).attr(’src’, $(this).attr(’src’).replace( /_orig/i, ‘_rollover’ ));
    }, function() {
    $(this).attr(’src’, $(this).attr(’src’).replace( /_rollover/i, ‘_orig’ ));
    });

    All you need to do is class the image as “hover” (or whatever)

  17. david
    Posted October 8, 2009 at 8:27 am | Permalink

    Thanks for the great script.

  18. Carl
    Posted November 3, 2009 at 1:04 pm | Permalink

    Does anyone have a simple online demo of that example?? It seems really usefull but it just doesn’t work for me. Maybe i did something wrong…

    Help would be appreciated :)

    Regards

  19. Carl
    Posted November 3, 2009 at 1:06 pm | Permalink

    Ok guys forget about it!!!

    I found the stupid mistake…

    Thanks anyway

  20. Posted November 9, 2009 at 5:07 am | Permalink

    Very nice code… I like its small size. However all my page content shifts with the larger image. Is it possible to have it above all the other content so this won’t happen?

  21. Posted November 23, 2009 at 1:16 pm | Permalink

    To be compliant merely change the code as so:

    $(function() {
    $(’.rollover’).hover(function() {
    var currentImg = $(this).attr(’src’);
    $(this).attr(’src’, $(this).attr(’onmouseover’));
    $(this).attr(’onmouseover’, currentImg);
    }, function() {
    var currentImg = $(this).attr(’src’);
    $(this).attr(’src’, $(this).attr(’onmouseover’));
    $(this).attr(’onmouseover’, currentImg);
    });
    });

    Then write your image tag as so:

  22. Posted November 23, 2009 at 1:18 pm | Permalink

    The image line didn’t appear as it thought I was trying to write html into the code. Write as so without the ((( )))

    ((()))

  23. Posted November 23, 2009 at 1:20 pm | Permalink

    Opps, didn’t like that either. Basically instead of hover=”second.gif” in your body code, write onmouseover=”second.gif”

  24. allistair
    Posted December 15, 2009 at 3:32 am | Permalink

    thanks for the snippet, very handy. i changed the hover attribute to use the A’s rel attribute. this makes it validate.

  25. SufferBoy
    Posted March 3, 2010 at 3:07 am | Permalink

    Could anyone show me where exactly in header.php i should put the java snippet?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>