Search code examples
jqueryjquery-animateappendto

jQuery appendTo DIV and then animate


When I clik on the button "new" start jQuery action:

1.) create DIV and addClass "block" to the DIV

2.) appendTo ".container"

3.) animate this DIV in container from opacity 0 to 1

JsFiddle Example

HTML

<div class="panel">
   <button class='new'> + </button>
</div>
<div class="container">
</div>

CSS

.block {
   margin: 0px;
   width: 200px;
   display: inline-block;
   border-right:thin dashed #aaa;
   opacity: 0;
}
.panel {
   position: absolute;
   top: 100px;
   padding: 5px;
   color: #FFF;
   font-size: 15px;
   background: #d30;
   height: 30px;
   cursor:pointer;
}
.container {
   position: absolute;
   top: 145px;
   left: 0px;
}
button{
   font-size: 20px;
   padding: 0px;
}

jQuery

function createChatBox(title) {
   $(" <div />" ).attr("id","block_"+title)
    .addClass("block")
    .html('<div class="title">text1 '+title+'</div>')
    .appendTo($( ".container" , {
        css: {
            display: 'inline-block',
            opacity: 0
        }
    }).animate({ opacity: 1  }) );
}

$(".new").click(function(){
     createChatBox('text2');
 });

Solution

  • You need to change your appendTo section to this:

        .appendTo(".container" , {
            css: {
                display: 'inline-block',
                opacity: 0
            }
        }).animate({ opacity: 1  });