0

我想要:

  1. 单击“NEW”按钮将新 DIV 添加到“CONTAINER”中 - 这很好用

  2. 单击“移动”按钮 - 获取 $array - 然后将容器移动到合适的位置 - 然后为 $array 中的每个项目在“CONTAINER”中附加新的“DIV” - 然后将“CONTAINER”动画到“left:0” - 这不会不行

  3. 单击“REMOVE”按钮 - 将“CONTAINER”动画到屏幕外,并从“CONTAINER”中删除所有 DIV

  4. JsFiddle 示例

  5. 为什么它在网络上不起作用?

HTML

<div class="panel">
    <button class='new'> + </button>
    <button class='move'> &gt; </button>
    <button class='remove'> &lt; </button>
</div>
<div class="container">
</div>

CSS

.block {
   margin       : 0px;
   width        : 200px;
   display      : inline-block;
   border-right : thin dashed #aaa;
   opacity      : 0;
}
.head {
   margin : 0px;
   padding: 5px;
   height : 30px;
   background-color: red;
   color  : white;
}
.body {
   margin : 0px;
   padding: 5px;
   height : 190px;
   background-color: #ddd;
   color  : black;
}
.panel {
   position  : absolute;
   top       : 50px;
   padding   : 5px;
   color     : #FFF;
   font-size : 15px;
   background: #d30;
   height    : 25px;
   -webkit-border-radius: 4px;
   -moz-border-radius: 4px;
   border-radius: 4px;
   cursor    : pointer;
}
.container {
   position: absolute;
   top  : 90px;
   left : 0px;
}
button{
   width   : 25px;
   background  : none;
   cursor      : pointer;
   font-family : 'voltaeftu-regular',Times New Roman;
   font-size   : 18px;
   color   : #fff;
   border  : none;
   margin  : 0px;
   padding : 0px;
}

jQuery:

$(".remove").click(function(){
    var x_width = $('.container').find('.block').width();
    var x_all = $('.container').find('.block').size();
    var all_width = -10 - ( x_width * x_all ) ;
    $(".container").animate({
        left: all_width
    }, 500 );
 });

$(".new").click(function()  {
       $('.container').append( $('<div class="block" id="new"><div class="head">HEADER</div><div class="body">text text text</div></div>', {
           css: {
               display: 'inline-block',
               opacity: 0
           }
       }).animate({ opacity: 1  }) );
});

// array
var $array = [ '001', '002', '003', '004', '005' ];

$(".move").click(function(){
   var array_length = $array.length;
   var array_width = 0 - array_length * 200;
   $('.container').css ({ left: array_width});
   $.each( $array , function(item, value){
               $('.container').apped('<div class="block" id="'+value+'"><div  class="head">HEADER '+value+'</div><div class="body">text text  text</div></div>', {
                       css: {
                           display: 'inline-block',
                           opacity: 0
                           }
                   }).animate({ opacity: 1  });
           });
    $('.container').animate({ left: 0});
});
4

3 回答 3

1

您遇到的一个问题是您在将原始块移动到时没有计算原始块的宽度left:0,这是一个修复:

var block_width = $(".block").width();
var array_width = (array_length * block_width) - block_width;
$('.container').css ({ left: array_width});

之前,您将它移动到其自身的宽度 * 数组的长度上,这意味着它被推离了屏幕,因为它已经存在的宽度没有被计算在内。

但是在我解决了这个问题之后,div 就有点堆积起来了。我假设您希望它们水平排列?

于 2012-04-18T06:22:53.483 回答
1

每当我完成 javascript 时,声明 DOM 对象和事件处理程序的顺序很重要。在您的网络示例(第 5 号)中,您的 jquery 事件处理(单击等)是在使用 HTML 创建这些 DOM 对象之前设置的。尝试将您的 js 脚本移动到页面底部的 end body 标记之前。

于 2012-04-18T06:23:32.533 回答
1

您应该在 $(document).ready() 函数中包含所有这些 javascript:

$(document).ready(function() {
   // put all your jQuery goodness in here.
 });
于 2012-04-18T06:28:34.813 回答