开发者

How to directly fade to another image (without any delay) using jquery?

开发者 https://www.devze.com 2023-04-01 06:41 出处:网络
My code works but it doesn\'t fade to second image directly: fades out first image -> delay -> fades in second one. How do I fade directly to second image?

My code works but it doesn't fade to second image directly: fades out first image -> delay -> fades in second one. How do I fade directly to second image?

My jquery code

$(document).ready(function() {
    var std = $(".fadeim").attr("src");
    var hover = std.replace(".png", "-hover.png");
    $(".fadeim"开发者_运维百科)
        .mouseover(function() {   
            $(this).fadeOut("fast",function(){  
                $(this).attr("src", hover);  
                $(this).fadeIn("fast");  
            });  
        })  
        .mouseout(function() {  
            $(this).fadeOut("fast",function(){  
                $(this).attr("src", std);  
                $(this).fadeIn("fast");           
            });  
        })  
    }); 
});


You can do the following [what i normally do]:

You can check out my testing link : http://jsfiddle.net/fJFmx/1/

I set a container [fixed width&height] for images. Set the style of for the images to position:absolute and with fixed dimensions.

HTML example:

<div id='slideContainer'>
    <img src='http://www.ct4me.net/images/dmbtest.gif' />
    <img src='http://pievscake.com/images/test.jpg' />
</div>

Then the Jquery:

$(function() {

    $('#slideContainer img:first').fadeIn();

    $('#slideContainer').hover(function() {
        $('#slideContainer img:first').fadeOut();
        $('#slideContainer img:last').fadeIn();
    }, function() {
        $('#slideContainer img:first').fadeIn();
        $('#slideContainer img:last').fadeOut();
    });

});

CSS

#slideContainer {border:1px solid #ffcc00;width:50px; height:50px;}
#slideContainer img {position:absolute; width:50px; height:50px; display:none;}

Hope this helps.


You might want to use CSS3 for what you're trying to do instead, as the image will not change if someone has JavaScript turned off. Try this CSS:

.fadeim {
    width: 200px;
    height: 100px;
    background: url(std.png) center center no-repeat;
    -webkit-transition: background 500ms linear;
    -moz-transition: background 500ms linear;
    transition: background 500ms linear;
}

.fadeim:hover {
    background: url(std-hover.png) center center no-repeat;
}

The above code assumes your image is 200×100px; you should change the width and height settings to the same size as the image. So if you open a page with this CSS applied, you'll see the image, and when you hover, the image should fade into the other one. This currently only works in WebKit (Chrome and Safari) and Mozilla (Firefox), so if someone uses an unsupported browser, the page will act exactly the same, but the image will immediately change without fading.

Ad@m


You need to place two elements over one another (via CSS absolute/relative positioning), then you will be able to decrease the opacity of the top one to make it look like it is fading into the bottom one.

After the fadeout is complete, quickly switch the SRC atributes of the images (user won't notice) and prepare for another cycle.

I suggest this HTML (because you only set width&height in one place and children will adapt to the parent size):

<div id='slideContainer' style='width:50px; height:50px; position: relative;'>
     <img src='hover.jpg' style='position:absolute; top:0px; right:0px; bottom:0px; left: 0px; display:none;' />
     <img src='original.jpg' style='position:absolute; top:0px; right:0px; bottom:0px; left: 0px; display:none;' />
</div>


While you hover the 'first' image, the second one is NOT yet loaded.

Try this way:

$('.fadeim').each(function() {

    var std = $(this).attr("src");
    var hover = std.replace(".png", "-hover.png");  

    $(this).clone().insertAfter(this).attr('src', hover).removeClass('fadeim').siblings().css({
        zIndex: '1',
        position:'absolute'
    });
    $(this).mouseenter(function() {
        $(this).stop().fadeTo(600, 0);
    }).mouseleave(function() {
        $(this).stop().fadeTo(600, 1);
    });

});

FIDDLE DEMO


The way to solve your problem:

pre load the second picture like Rok Kralj said,don't load it the second you need it.

otherwise you would have to wait for the browser fetching it from server

A better way

set one picture as src,other one as parentNode's background-image. only animate the image.

Because users don't need the blank gap,they just need the transforming for better experience

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号