我想:
点击“NEW”按钮将新的 DIV 添加到“CONTAINER”中 - 这很好用
单击“移动”按钮 - 获取 $array - 然后将容器移动到合适的位置 - 然后为 $array 中的每个元素在“CONTAINER”中添加新的“DIV” - 然后将“CONTAINER”动画化为“左侧” : 0"- 这不起作用
点击“REMOVE”按钮 - 将“CONTAINER”动画移出屏幕,并从“CONTAINER”中删除所有 DIV
为什么它在 web 上不起作用?
HTML
<div class="panel">
<button class='new'> + </button>
<button class='move'> > </button>
<button class='remove'> < </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/