views:

67

answers:

3

I made a very simple jQuery img rollover function inspired by this one: http://www.smileycat.com/miaow/archives/000224.php.

It just check all the imgs that contain _off in their name and swap them with an img with the same name but "_on" instead of "_off".

Since I can't use background imgs in my layout, I feel that's an handy solution. But I've got the feeling that the swapping is not smooth, like if the function runs slow. What do you think? Are there ways to optimize it?

Here is the code:

    function roll_over() {
        $("img[src*='_off']").hover(
            function() {
                var stringa = $(this).attr("src");
                var stringa = stringa.replace("_off", "_on");
                $(this).attr("src", stringa);
            },
            function() {
                var stringa = $(this).attr("src");
                var stringa = stringa.replace("_on", "_off");
                $(this).attr("src", stringa);
            }
        );
    }
A: 

Repeatedly doing $("img[src*='_off']") is inefficient. It means the browser is having to search everything on a page when you roll over. You should include jQuery Lint for information about optimisation...

Ross
I would agree that repeatedly calling this function on a mouseover event would be unnecessary, inefficient, and not advised. If they are calling it on the page load, it is a different matter entirely.
Buggabill
Also defining `stringa` variable twice??
Teja Kantamneni
yes, this is true
Ross
So please can you give me an hand in modify it?BTW I can't use background replacement because I resize the imgs
Gusepo
+2  A: 

Your code is bad. Why?

  1. It will fail when you'll have image like this: ...src="/images/my_office.png"...
  2. You use JS for something that is so primitive that can be done in pure CSS
  3. The *_on images will be loaded on mouseover event so you won't see any image for a while.

How to fix all of these issues? Use CSS Sprites.

  1. Create image like this one: http://www.digart.pl/gfx/ico/s/fb.gif
  2. HTML code: <a href="..." id="myId">blah</a> (of cource you don't have to use A element).
  3. CSS code:

    #myId {
        display: inline-block; /* or block, or even inline with correct line-height */
        width: 33px;
        height: 20px;
        background: url(/path/to/img) 0 0 no-repeat;
    }
    #myId:hover {
        background-position: 50% 0;
    }
    

If you still want to automatize whole process then use JS only to change background position instead of image.

Crozin
+1 CSS sprites will really make this a lot easier and quicker.
ryanulit
I am not sure why, but the OP says this in their question: "...I can't use background imgs in my layout." That may be why they are trying to do it this way.
Buggabill
@Buggabill: Ops... my fault. I didn't notice this.
Crozin
A: 

I found a good function here http://webdevel.blogspot.com/2008/04/rollover-images-with-jquery.html

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

I just specify the id or class of imgs

Gusepo