Append an element with fade in effect [jQuery]

后端 未结 4 992
太阳男子
太阳男子 2020-12-22 18:53
var html = \"
Hello stuff here
\" $(\"#mycontent\").append(html).fadeIn(999);

This doesn\'t seem to work.

相关标签:
4条回答
  • 2020-12-22 19:06

    Adding a little more info:

    jQuery implements "method chaining", which means you can chain method calls on the same element. In the first case:

    $("#mycontent").append(html).fadeIn(999);
    

    you would be applying the fadeIn call to the object which is target of the method chain, in this case #mycontent. Not what you want.

    In @icktoofay's (great) answer you have:

    $(html).hide().appendTo("#mycontent").fadeIn(1000);
    

    This basically means, create the html, set it as hidden by default, append it to #mycontent and then fade it in. The target of the method chain now is hmtl instead of #mycontent.

    0 讨论(0)
  • 2020-12-22 19:10

    since the fadeIn is a transition from hide to show, you'll have to hide the "html" element when you append it and then to show it.

    var html = "<div id='blah'>Hello stuff here</div>"
    
    $("#mycontent").append(function(){
      return html.hide();
    });
    
    $('#blah').fadeIn(999);

    0 讨论(0)
  • 2020-12-22 19:16

    This also works

    $(Your_html).appendTo(".target").hide().fadeIn(300);
    

    Regards

    0 讨论(0)
  • 2020-12-22 19:22
    $(html).hide().appendTo("#mycontent").fadeIn(1000);
    
    0 讨论(0)
提交回复
热议问题