【发布时间】:2018-06-17 00:34:11
【问题描述】:
我正在尝试创建一个可以通过简单的滑出动画展开和折叠的框。如果您运行下面的示例,其想法是它以一条红线开始,当您单击按钮时,它会分成两条读取线并轻轻展开以显示内容,就像从桌子上拉出一张画一样。
我已经尝试过变换、动画、相对:顶部定位,但我无法获得想要的效果。
包含的盒子应该扩大尺寸
function expandContract() {
const el = document.getElementById("expand-contract")
el.classList.toggle('expanded')
el.classList.toggle('collapsed')
}
#container {
border: 1px solid black;
padding: 15px;
}
#top-section {
border-bottom: 1px solid red;
}
#expand-contract {
border-bottom: 1px solid red;
}
.expand-contract {
transform: translateY(-100%)
overflow: hidden;
}
@keyframes slide-in {
100% {
transform: translateY(0%)
}
}
.expanded {
background-color: green;
animation-name: slide-in;
animation-duration: 1s;
}
.collapsed {
background-color: red;
transform: translateY(-100%)
}
<div id="container">
<div id="top-section">
This is always displayed
</div>
<div id="expand-contract" class="expanded">
This section expands and contracts
<table>
<tr><td>test1</td></tr>
<tr><td>test2</td></tr>
<tr><td>test3</td></tr>
<tr><td>test4</td></tr>
</table>
</div>
<div id="bottom-section">
This section is always displayed
</div>
</div>
<button onclick="expandContract()">Expand/Contract</button>
【问题讨论】:
标签: javascript css