Image Rollovers in WordPress

July 28th, 2009 by Brian under Web Development

Image Rollovers in WordPress

This article is an expansion upon my last article which discusses how to set up jQuery in WordPress. In this article, you will learn how to create a simple rollover image using jQuery.

First of all, you need to have your images created of course. For the sake of this article, the images should be named imagename.png, and imagename-hover.png (or any other picture file extension).

As you learned in the previous article, you need to enclose your jQuery function in a special function of its own.

This is called from an external .js file, and will work for any images inside of a div called #quote (or whatever fits your website).

jQuery(function($) {                    <--This is the enclosing function
    $(document).ready(function(){       <--This is the overall jQuery function
 
        $("#content #quote img").each(function () {    <--This function determines img src
            rollsrc = $(this).attr("src");
            rollON = rollsrc.replace(/.png$/gi, "-hover.png");
            $("<img>").attr("src", rollON);
        });
 
        $("#content #quote a").mouseover(function () {  <--This displays the -hover image
            imgsrc = $(this).children("img").attr("src");
            matches = imgsrc.match(/-hover/);
            if (!matches) {
                imgsrcON = imgsrc.replace(/.png$/gi, "-hover.png");
                $(this).children("img").attr("src", imgsrcON);
            }
        });
 
        $("#content #quote a").mouseout(function () {  <--This returns the image to normal
            $(this).children("img").attr("src", imgsrc);
        });
 
    });
});

The first part of each of the 3 functions that actually change the image contain a css path to the hyperlink (‘a’ tag). This will vary depending on the structure of your website. A good way to find the css path is to use the Firefox plugin called FireBug.

I hope this helps other developers in their WordPress and jQuery endeavors!

If you enjoyed this article then please share it!
  • email
  • Twitter
  • Digg
  • del.icio.us
  • LinkedIn
  • StumbleUpon
  • Facebook
  • Google Bookmarks
  • Reddit

Tags: , ,

12 Responses to “Image Rollovers in WordPress”

  1. Alain says:

    Very useful. Thank you!

  2. Egil Arntzen says:

    Isn’t this bad for the SEO? That Google won’t find your text in the menu?

    • Brian says:

      That’s a good question, and the answer is yes. You would not want to use a technique like this for main navigation items. In the case of my website it works fine for me because the two images I use the rollover script with are in addition to regular text links to those pages. They are for the reader, not the search engine.

      If you are looking for a way to do rollovers for links that are Search Engine friendly, you should look into using CSS and the a:hover attribute. This can be used to replace the background-image behind a text link. The top navigation menu on my website uses this technique. Pretty soon CSS3 will be viable though, so I can use CSS for rounded corners instead of images!

      If you have further questions I will be happy to answer them!

  3. [...] tutorial i browsed swiftly a while ago … hopefully it helps you out … (this one uses jQuerry) Simple Image Rollovers in WordPress with jQuery – Kalamazoo Web Design and Development Professional … (this one uses CSS) WordPress › Support Rollover buttons breaking menu, not following [...]

  4. apple1987 says:

    Keep up the interesting posts. I love to see keen bloggers!

  5. Tnelson says:

    Super-Duper site! I am loving it!! Will come back again – taking your feeds too now, Thanks.

  6. Bill Bartmann says:

    Excellent site, keep up the good work. I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, :)

    A definite great read..

    -Bill-Bartmann

  7. alvabenton6279 says:

    Thank you very much for that wonderful piece of text.

  8. millionaire says:

    [...] Originally posted here: Simple Image Rollovers in WordPress with jQuery [...]

Leave a Reply