【问题标题】:jQuery appendTo DIV and then animatejQuery appendTo DIV 然后动画
【发布时间】:2012-04-23 14:39:12
【问题描述】:

当我点击“新建”按钮时,开始 jQuery 动作:

1.) 创建DIV并将Class“块”添加到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');
 });

【问题讨论】:

    标签: jquery jquery-animate appendto


    【解决方案1】:

    您需要将 appendTo 部分更改为:

        .appendTo(".container" , {
            css: {
                display: 'inline-block',
                opacity: 0
            }
        }).animate({ opacity: 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  });
      
      }
      

      输出:

      【讨论】:

        【解决方案3】:

        您的代码似乎已损坏,括号/{} 不匹配。 这是一个修复程序,这是您想要的吗? http://jsfiddle.net/mamadrood/bckHD/1/

        【讨论】:

          猜你喜欢
          • 2012-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-18
          • 1970-01-01
          相关资源
          最近更新 更多