Web and SEO Professional
Friday March 12th 2010
affiliate_link

ads

Jquery Image rollover with demo

I have given a code in my earlier post for jquery image rollover. Now here are the final code and guide how to use this code.

  1. you need to creative two image ie: one for normal state and one for active/rollover state. name it like imagename.gif and imagename_active.gif.
  2. put your image in your HTML document.
  3. put all your image in normal state. remember to keep the active state image in same folder.
  4. include jquery latest script from jquery website and include script on your head section.
  5. copy below code and paste after jquery include. (you can put this code in common include file and include after jquery include.)
  6. don’t forget to change “#nav td” with your a element parent element in below script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function getLeaf(url) {
var splited=url.split('?');// remove all the parameter from url
url=splited[0];
return url.substring(url.lastIndexOf("/")+1);// return file name without domain and path
}

jQuery.fn.extend({
 enter: function() {//plugins creation
     return this.each(function() {
       var pth = $(this).find("img")[0];
      //alert($(this).children().attr("href"));
       if($(this).children().attr("href")==getLeaf(document.location.href)){// check that the link url and document url is same
           $(pth).attr("src",pth.src.replace(/.gif/g, '_active.gif'));
       } else{
               $(this).hover(function(){
                  $(pth).attr("src",pth.src.replace(/.gif/g, '_active.gif'));// mouse over Image
                  },function(){
                      $(pth).attr("src",pth.src.replace(/_active.gif/g, '.gif'));// mouse out image
                      });
               }
               });
     }
});
$(function(){  // Document is ready
 $("#nav td").enter();// call the function
});

Jquery Image rollover demo download Let me know if you are not able to implement this code. I will happy to assist you.

Buzz it!
Sign up for PayPal and start accepting credit card payments instantly.

Reader Feedback

16 Responses to “Jquery Image rollover with demo”

  1. Julia says:

    If I replace all of the “gif” with “jpg” should it work with jpgs as well, or only gifs?

  2. Elisa says:

    Hi Pankaj, your code works great….
    Sorry for my English and my ignorance in Js!

    I’m trying to use it associated to a tabbed menu…
    In this way the href for my image isn’t a new file but a div inside the document

    So the line of your code that controls what image has to remain active does’t work…

    if($(this).children().attr(“href”)==getLeaf(document.location.href))

    and the rollover nerver stop (for the active link)….
    So how can I change this line? (if it’s the right thing to change…)

    Thank you very much
    Elisa

  3. Jessie says:

    Can the image be called from the css instead of the html?

  4. Jessie says:

    OMG, i have been searching for days and couldn’t find a simple answer to this. Thank-you so much! Just a quick question though… jquery look for the image within a div in the html. Is is possible to have it so it calls the image from css? I’m selling themes that are digitally downloaded and therefore i don’t want to include the http://mysite/image and would like to know if it can be done where it calls the image from the css eg. .mydiv { background:url ../styleImages/buttons/LPmore2.gif) no-repeat; }

    Thanks
    Jessie

  5. Evan says:

    Hi Pankaj,

    Thank you for this code. It’s exactly what I have been looking for. In fact I can think of lots of situations when this could be very useful.

    I was able to implement it with no problem, and the rollover effect works great.

    For some reason the current page effect is not working on my site. I was hoping that you might offer some advice.

    Here is the situation: It’s a wordpress site. I’m using list style navigation. Here’s a little bit of list nav code:

    1
    2
    3
    4
    5
    6
    <div id="nav">
                <ul>

                      <li><a href="<?php bloginfo('wpurl'); ?>"><img alt="" height="36px" src="<?php bloginfo('template_url'); ?>/images/home.gif" width="58"/></a></li>

                      <li><a href="<?php bloginfo('wpurl'); ?>/category/blog/" ><img src="<?php bloginfo('template_url'); ?>/images/blog.gif" height="36px" width="61" border="0"/></a></li>

    Your demo works great, so the issue is with my implementation.

    I get rollover when I call (#nav ul li) or (#nav ul li a). But neither way gives results in a current page effect.

    Any advice on how to proceed with troubleshooting?

  6. Joe Ellis says:

    An easier way to do this might be to make two images, one with a suffix “_on” and the other with the suffix of “_off”. Then paste this code

    [code]

    $(function() {
    $("img.rollover").hover(
    function () { this.src = this.src.replace("_off","_on");
    },
    function () { this.src = this.src.replace("_on","_off");
    }
    );
    });

    [/code]

    And then you can just call rollovers on an image by adding ‘class=”rollover”‘, like

    [code]

    [/code]

Leave a Reply

Notify me of follow-up comments via email.