jquery - 使用绝对位置动画在单击时隐藏和显示 div,并在隐藏时删除它们

标签 jquery css position jquery-animate css-position

我想:

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

  2. 单击“移动”按钮 - 获取 $array - 然后将容器移动到合适的位置 - 然后为 $array 中的每个元素在“CONTAINER”中添加新的“DIV” - 然后将“CONTAINER”动画化为“左侧” : 0"- 这不起作用

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

  4. JsFiddle Example

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

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});
});

最佳答案

您遇到的一个问题是在将原始 block 移动到 left:0 时没有计算原始 block 的宽度,这是一个解决方法:

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

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

但在我修复它之后,div 只是堆叠起来。我假设您希望它们水平排列?

关于jquery - 使用绝对位置动画在单击时隐藏和显示 div,并在隐藏时删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10203662/

相关文章:

c# - 使用 LINQ 在 C# 中获取 List<> 元素位置

jquery - DIV 绝对定位 - 在浏览器窗口调整大小后保持位置

jquery - EmberJS 应用程序...可以与 UI 框架集成吗?

jquery - 如何顺利地改变元素的左边距

CSS - 如何为 css 形状添加边框颜色

html - 站点一侧的额外空白

html - 将表单元素 <fieldset> 叠加到图像上;显示完整图像并随设备宽度缩放的位置

html - z-index 值未按应有的方式工作 - z-index 50 元素显示在下方

jQuery on() 不适用于移动 Safari

jquery - 如何从每个子元素中选择第一个元素