2014-09-05 61 views
0

我正嘗試使用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'); 
    } 
}); 
}); 
+1

過渡或動畫僅對數字值起作用。 auto不是,並且沒有任何東西可以繼承或計算該值:( – 2014-09-05 18:05:13

+0

基於@GCyrillus的評論和@DRD的回答,而不是javascript中的'.addClass'和'.removeClass'行我會做類似' .css('flex','0 0 25px')'和'.css('flex','1 1 450px')'我已經測試過了,並且正在運行,謝謝 – 2014-09-08 13:00:34

回答

1

,使過渡工作,你需要明確設置flex-basis。以下是點擊激活手風琴的CSS解決方案:http://jsfiddle.net/zx854854/。如果您想堅持點擊狀態,則可以使用收音機inputlabel組合。

HTML:

<div id="wrapper"> 
    <div tabindex = "1"></div> 
    <div tabindex = "2"></div> 
    <div tabindex = "3"></div> 
</div> 

CSS:

#wrapper { 
    height: 300px; 
    display: flex; 
    flex-direction: column; 
} 

#wrapper > * { 
    flex: 0 0 25px; 
    outline: 0; 
    transition: flex-basis 0.3s linear; 
} 

#wrapper > div:first-of-type { 
    background-color: blue; 
    order: 3; 
} 

#wrapper > div:first-of-type:not(:focus) + div:not(:focus) + div:not(:focus) { 
    flex-basis: 250px; 
} 

#wrapper > div:nth-of-type(2) { 
    background-color: green; 
    order: 2; 
} 

#wrapper > div:nth-of-type(3) { 
    background-color: yellow; 
    order: 1; 
} 

#wrapper > div[tabindex]:focus { 
    flex: 1 1 250px; 
} 
+0

好的CSS唯一的解決方案。我將堅持使用jQuery來設置flex基礎,以便我可以調整不同大小的屏幕。 – 2014-09-08 12:56:51

1

我實現了這個使用下面的代碼:

HTML

<ul class="actions-list"> 
     <li class="action-item facebook"> 
      Facebook 
     </li> 
     <li class="action-item google"> 
      GooglePlus 
     </li> 
     <li class="action-item linkedin"> 
      LinkedIn 
     </li> 
     <li class="action-item picasa"> 
      Picasa 
     </li> 
     <li class="action-item twitter"> 
      Twitter 
     </li> 
     <li class="action-item wikipedia"> 
      Wikipedia 
     </li> </ul> 

CSS

/* Flex box define */ 
.actions-list { 
    display: -webkit-box; 
    display: -webkit-inline-flex; 
    display: -moz-inline-flex; 
    display: -ms-inline-flexbox; 
    display: inline-flex; 
} 

.actions-list .action-item { 
    -webkit-box-flex: 1; 
    -webkit-flex: 1 1 auto; 
    -moz-flex: 1 1 auto; 
    -ms-flex: 1 1 auto; 
    flex: 1 1 auto; 

    -webkit-transition: all 300ms ease; 
    -moz-transition: all 300ms ease; 
    -o-transition: all 300ms ease; 
    transition: all 300ms ease; 

    -webkit-box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    box-sizing: border-box; 

    overflow: hidden; 
} 

/* Design: widths, colors, borders, etc... */ 
.actions-list { 
    margin: 0; 
    padding: 0; 
} 
.actions-list .action-item { 
    font-family: Helvetica, Arial, sans-serif; 
    font-weight: lighter; 
    cursor: pointer; 
    background-color: #66bbcc; 
    border-left: 1px solid rgba(0, 0, 0, 0.2); 
    color: #000000; 
    padding-left: 52px; 
    background-repeat: no-repeat; 
    background-position: left 10px center; 
    background-size: 32px; 
    line-height: 52px; 
    height: 52px; 
    max-width: 50px; 
} 
.actions-list .action-item:hover { 
    max-width: 150px; 
    background-color: #ff9966; 
    padding-right: 10px; 
} 
.actions-list .action-item:first-child { 
    border: none; 
} 

.facebook { 
    background-image: url(http://www.webdeveasy.com/code/assets/images/facebook.png); 
} 
.google { 
    background-image: url(http://www.webdeveasy.com/code/assets/images/google.png); 
} 
.linkedin { 
    background-image: url(http://www.webdeveasy.com/code/assets/images/linkedin.png); 
} 
.picasa { 
    background-image: url(http://www.webdeveasy.com/code/assets/images/picasa.png); 
} 
.twitter { 
    background-image: url(http://www.webdeveasy.com/code/assets/images/twitter.png); 
} 
.wikipedia { 
    background-image: url(http://www.webdeveasy.com/code/assets/images/wikipedia.png); 
} 

JS Fiddle link of the accordian

參考

Click here for reference of the project

+0

在我看來,在你的例子中,過渡工作的原因是設置了'max-width'而不是'flex-basis'。我仍然需要明確地設置這個值,但它可能會更方便一些 – 2014-09-08 13:06:17

+0

是的,請確保。回答如果你覺得它有幫助:) – sukantk 2014-09-08 14:08:28