replaceWith() while elements fadeOut() and fadeIn() in JQuery

二次信任 提交于 2020-01-16 01:04:10

问题


What I'm trying to do is simply, fadeout all images inside containers, replace #next1's image to #active and after that fadein all images again.

here is my code:

$('.logo').fadeOut('slow', function() {
    $('#active>img').replaceWith( $('#next1>img') );
}).fadeIn('slow', function() {});

this does not work. i found myself looking at the empty #active

but this however;

$('.logo').fadeOut('slow', function() {}).fadeIn('slow', function() {});
$('#active>img').replaceWith( $('#next1>img') );

makes the replacing just fine but not the animation i'm trying to do.

i get same results with both chrome and ie.


回答1:


My suggestion here would be to look at the promise/done methods in jQuery. As an example here you could do something like:

$('.logo').fadeOut('slow').promise().done(function(logo) {
    $('#active>img').replaceWith($('#next1>img'));
    $(logo).fadeIn('slow');
});

jQuery promise - http://api.jquery.com/promise/




回答2:


Try:

$('.logo').fadeOut('slow', function() {
    $('#active>img').replaceWith( $('#next1>img') );
    $(this).fadeIn('slow');
});

Assuming what you want to achieve is fading out, then replacing the content while .logo is hidden, then fading in after the logo is replaced.



来源:https://stackoverflow.com/questions/7870225/replacewith-while-elements-fadeout-and-fadein-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!