jQuery appendTo DIV然后动画

时间:2012-04-23 14:39:13

标签: jquery jquery-animate appendto

当我按下“new”按钮时启动jQuery动作:

1。)创建DIV并将addClass“block”创建到DIV

2。)appendTo“.container”

3。)在容器中为此DIV设置动画,从不透明度0到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');
 });

3 个答案:

答案 0 :(得分:3)

您需要将appendTo部分更改为:

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

答案 1 :(得分:2)

<强> Is this what you are looking for:

jQuery的:

function createChatBox(title) {

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

}

输出:

enter image description here

答案 2 :(得分:1)

您的代码似乎已被破坏,某些不匹配的括号/ {}。 这是一个修复,它是你想要的? http://jsfiddle.net/mamadrood/bckHD/1/