How to fade out div slowly, update content, and then fade in div slowly, using jQuery?

前端 未结 6 1557
梦如初夏
梦如初夏 2020-12-10 02:52

I have a div I want to fade out, update its content, and then fade back in. So far I have tried:

$(\'#myDivID\').fadeOut(\'slow\', function() {
         


        
相关标签:
6条回答
  • 2020-12-10 03:26

    You should do it this way (this works, is tested code):

    $('#myDivID').fadeOut('slow', function() {
        $('#myDivID').html(content);
        $('#myDivID').fadeIn('slow');
    });
    

    Your code wasn't working because the new created div is instantly visible. Another solution is to add a display:none like the following:

       $('#myDivID').fadeOut('slow', function() {
          $('#myDivID').replaceWith("<div id='myDivID' style='display:none'>" + content + "</div>");
          $('#myDivID').fadeIn('slow');
      });
    

    Hope this helps Cheers

    0 讨论(0)
  • 2020-12-10 03:26

    Something like this would work:

    $('#myDivID').fadeOut('slow', function() {
        $('#myDivID').replaceWith("<div id='myDivID'>" + content + "</div>")
        $('#myDivID').hide().delay(2000).fadeIn('slow'); 
    });
    

    Test here: http://jsfiddle.net/tomgrohl/PgcTZ/

    I've put the hide before the delay to make the animation work.

    0 讨论(0)
  • 2020-12-10 03:34

    When you use replaceWith the content will be visible that is why there is no smooth transition.

    First hiding the div and then calling fadeIn will give smooth transition.

    $('#myDivID').fadeOut('slow', function() {
        $('#myDivID').replaceWith("<div id='myDivID' style='display:none'>" + content + "</div>");
        $('#myDivID').fadeIn('slow');
    });
    
    0 讨论(0)
  • 2020-12-10 03:39

    Try this.

    http://jsfiddle.net/X3cnT/

    $('#myDivID').fadeOut('slow', function() {
            $('#myDivID').html("all this text");
            $('#myDivID').fadeIn('slow');
    });
    
    0 讨论(0)
  • 2020-12-10 03:52

    use SetTimeOut

    setTimeout(function() { $('#myDivID').fadeIn('slow'); }, 5000);
    

    this works

    jsFiddle http://jsfiddle.net/3XYE6/1/

    $('#doit').click(function(){
        $('#myDivID').fadeOut('slow', function() {
            $('#myDivID').html('New content in MyDiv ('+Math.random()+')')
            setTimeout(function() { $('#myDivID').fadeIn('slow'); }, 5000);
        });    
    })
    
    0 讨论(0)
  • 2020-12-10 03:52

    this should do it!

    http://jsfiddle.net/3XYE6/

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