試圖動畫display:none
是你的問題的根源。爲什麼淡入作品和淡出不會在混亂來自同時使用display
和opacity
的組合。
的display
屬性僅由是否被施加.active
決定;動畫實際上並沒有改變它,而opacity
是按預期動畫的。在淡入時,這意味着你立即顯示你的元素,然後從透明轉變爲不透明。在淡出時,這意味着您立即隱藏您的元素,然後在隱藏時從不透明變爲透明。
有,你可以解決這個問題了幾個不同的方式,但它有點取決於上下文。例如,你可以把它作爲一個block
元素,並使用height
屬性,使其崩潰:
$('button').on('click', function(e) {
e.preventDefault();
$(this).siblings('.box').toggleClass('active');
})
@import "bourbon";
* {
box-sizing: border-box;
text-align: center;
}
button {
margin: 30px 0;
border: 1px solid #ccc;
text-align: center;
padding: 1em;
background: none;
box-shadow: none;
}
.box {
margin: 0 auto;
width: 80%;
height: 0;
display: block;
line-height: 100px;
background: #ccc;
border: 1px solid #444;
text-align: center;
opacity: 0;
animation: FadeOut 1s ease-in-out;
}
.box.active {
display: block;
height: initial;
opacity: 1;
animation: FadeIn 1s ease-in-out;
}
@keyframes FadeIn {
0% {
opacity: 0;
height: initial;
}
100% {
opacity: 1;
height: initial;
}
}
@keyframes FadeOut {
0% {
opacity: 1;
height: initial;
}
99% {
opacity: 0;
height: initial;
}
100% {
height: 0;
opacity: 0;
height: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col">
<button class="toggle-good">Toggle display</button>
<p class="box good">I move nicely!</p>
<p>Extra paragraph to show collapse</p>
</div>
正如你看到的例子,我製作動畫的淡入 – Mick
當你。在'.box'選擇器中刪除'display:none',動畫變得平滑,但是框不會摺疊。 – sniels