JQuery fadeOut(function(){fadeIn});

半世苍凉 提交于 2019-12-02 13:13:38

问题


I have a problem with my webpage. I have 4 div's which should all fade in after the one before fades out. The code I use is:

$('.btn').click(function(){
    $('#box3').delay(5000).fadeOut('slow', function(){
        $('#box4').fadeIn('slow');
    });
});

With #box1 > #box2 it works, with #box2 > #box3 it works but when I try to go from #box3 > #box4 sometimes #box3 fades out then fades in with #box4. I have No idea why it is doing this.

Thanks,

http://jsfiddle.net/chLRa/4/ now working. Sometimes when going from 3 to 4 it still fades in 3 and 4


回答1:


Here's a simple helper function to help you do this.

function fade(thisIn, callback){
    boxes.not(thisIn).filter(':visible').delay(5000).fadeOut('slow', function(){
        thisIn.fadeIn('slow', callback);
    });
}

jsFiddle




回答2:


I'd say try using finish() method:

$('.btn').click(function(){
    $('#box3').finish().delay(5000).fadeOut('slow', function(){
        $('#box4').fadeIn('slow');
    });
});

Maybe would be better in your case to use it after delay()




回答3:


You may be better off using the callback version of those methods:

$('.btn').click(function(){
    $('#box1').fadeOut('slow', function() {
        $('#box2').fadeIn('slow', function() {
            $('#box2').fadeOut('slow', function() {
                $('#box3').fadeIn('slow', function() {
                    $('#box3').fadeOut('slow', function() {
                        $('#box4').fadeIn('slow', function() {
                            $('#box4').fadeOut('slow');
                        });
                    });
                });
            });
        });
    });
});

jsFiddle




回答4:


jQuery official documentation says, that second param is not a callback, but easing style.

http://api.jquery.com/fadeIn/#fadeIn-duration-easing-complete http://api.jquery.com/fadeOut/#fadeOut-duration-easing-complete

$('#el').fadeOut(750,'swing',function(){
    $('#el').fadeIn();
});

So just move your callback to 3rd param and everything will work.



来源:https://stackoverflow.com/questions/17819299/jquery-fadeoutfunctionfadein

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