jQuery fade out then fade in

前端 未结 4 634
失恋的感觉
失恋的感觉 2020-12-08 07:08

There\'s a bunch on this topic, but I havn\'t found an instance that applies well to my situation.

Fade a picture out and then fade another picture in. Instead, I\'m

相关标签:
4条回答
  • 2020-12-08 07:21

    After jQuery 1.6, using promise seems like a better option.

    var $div1 = $('#div1');
    var fadeOutDone = $div1.fadeOut().promise();
    // do your logic here, e.g.fetch your 2nd image url
    $.get('secondimageinfo.json').done(function(data){
      fadeoOutDone.then(function(){
        $div1.html('<img src="' + data.secondImgUrl + '" alt="'data.secondImgAlt'">');
        $div1.fadeIn();
      });
    });
    
    0 讨论(0)
  • 2020-12-08 07:28

    fade the other in in the callback of fadeout, which runs when fadeout is done. Using your code:

    $('#two, #three').hide();
    $('.slide').click(function(){
        var $this = $(this);
        $this.fadeOut(function(){ $this.next().fadeIn(); });
    });
    

    alternatively, you can just "pause" the chain, but you need to specify for how long:

    $(this).fadeOut().next().delay(500).fadeIn();
    
    0 讨论(0)
  • 2020-12-08 07:34

    With async functions and promises, it now can work as simply as this:

    async function foobar() {
      await $("#example").fadeOut().promise();
      doSomethingElse();
      await $("#example").fadeIn().promise();
    }
    
    0 讨论(0)
  • 2020-12-08 07:38

    This might help: http://jsfiddle.net/danielredwood/gBw9j/
    Basically $(this).fadeOut().next().fadeIn(); is what you require

    0 讨论(0)
提交回复
热议问题