jQuery Hover image change animation

后端 未结 7 686
难免孤独
难免孤独 2021-01-03 02:54

I have an IMG tag with a grayscale image. I hooked up a Hover() event in order to change the \"src\" to a color image on hover, and back to grayscale on mouse-out.

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 03:13

    Here are a couple solutions. Your code doesn't work because you are setting the source which changes the image immediately. It's probably easier just to load both images, overlay them with CSS and then fade the color one in out when the parent container is moused over. Alternatively you could cross fade them

    css

    .fader { display: inline-block; }
    .fader img:last-child {
        position: absolute;
        top: 0; 
        left: 0;
        display: none;
    }​
    

    html

    fade in on mouseover

    http://jsfiddle.net/Xm2Be/3/

    $('.fader').hover(function() {
        $(this).find("img:last").fadeToggle();
    });​
    

    cross fade

    http://jsfiddle.net/Xm2Be/2/

    $('.fader').hover(function() {
        $(this).find("img").fadeToggle();
    });​
    

提交回复
热议问题