我正嘗試使用flexbox和transitions創建一個手風琴,使用jQuery添加和刪除一個摺疊類。我遇到的問題是如果我將flex基礎設置爲擴展div的auto,則動畫不起作用。示例見http://jsfiddle.net/vbLqg40h/。如果我將柔性基礎從自動更改爲500px,柔性容器的高度,動畫工作(http://jsfiddle.net/vbLqg40h/2/)......我不知道爲什麼。使用flexbox的css手風琴
HTML
<div id="wrapper">
<div id="box1" class="expand"></div>
<div id="box2" class="expand collapse"></div>
<div id="box3" class="expand collapse"></div>
</div>
CSS
#wrapper {
height: 500px;
display: flex;
flex-direction: column;
}
.expand {
flex: 1 1 auto;
transition: all 2s;
}
.collapse {
flex: 0 1 25px;
}
#box1 {
background-color: yellow;
}
#box2 {
background-color: green;
}
#box3 {
background-color: blue;
}
的JavaScript
$('.expand').click(function() {
var expandId = $(this).attr('id');
$('.expand').each(function() {
var thisId = $(this).attr('id');
if (expandId != thisId) {
$('#' + thisId).addClass('collapse');
} else {
$('#' + thisId).removeClass('collapse');
}
});
});
過渡或動畫僅對數字值起作用。 auto不是,並且沒有任何東西可以繼承或計算該值:( – 2014-09-05 18:05:13
基於@GCyrillus的評論和@DRD的回答,而不是javascript中的'.addClass'和'.removeClass'行我會做類似' .css('flex','0 0 25px')'和'.css('flex','1 1 450px')'我已經測試過了,並且正在運行,謝謝 – 2014-09-08 13:00:34