我在柔性盒容器中有2个DIV,它们都是并排开始的。通过删除其中一个div,另一个变成集中在容器内。动画2柔性盒DIV
我似乎无法找到一种从居中/未经过动画过渡的方式。有没有办法做到这一点?
HTML:
<div id='wrap'>
<div id='a'></div>
<div id='b'></div>
</div>
<button id='btna' onclick="toggle('a')">Toggle Red</button>
<br>
<button id='btnb' onclick="toggle('b')">Toggle Green</button>
CSS:
#wrap{
width: 400px;
height: 200px;
border: 1px dashed grey;
display: flex;
justify-content: center;
}
#a{
width: 200px;
height: 200px;
background-color: red;
}
#b{
width: 200px;
height: 200px;
background-color: green;
}
JS:
var displayed = [ true, true ];
function toggle(div)
{
if(div == 'a')
{
if(displayed[0])
{
$('#a').fadeOut(500);
}
else
{
$('#a').fadeIn(500);
}
displayed[0] = !displayed[0];
}
else
{
if(displayed[1])
{
$('#b').fadeOut(500);
}
else
{
$('#b').fadeIn(500);
}
displayed[1] = !displayed[1];
}
}
这里是我到目前为止有一个的jsfiddle:
https://jsfiddle.net/uvyLh8m9/6/
删除样式证明内容致电500毫秒后
Element.style.display = 'none';
:中心;他们将向左对齐。 –我希望它们居中,如果只显示一个。 – sam
本质上......没有。 'justify-content'不是可以动画的。您必须将div的宽度设置为零,然后将其删除。 –