我基本上有一個側邊欄,可以包含多個部分(藍色),每個部分可以打開/關閉,它可以包含多個項目(紅色)。當打開時,我希望擴展元素不要將其他藍色部分推到邊框之外,而是將它們推靠在邊框上,并在擴展部分上顯示溢出y軸。下面是它的樣子(1)和應該的樣子(2)的截圖:
下面是使用的代碼:
<div id="container" class="h-96 w-52 overflow-hidden bg-black">
<div class="h-[100px] w-full border border-black bg-blue-500"></div>
<div id="item-container" class="h-fit space-y-1 overflow-y-scroll">
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
</div>
<div class="h-20 w-full border border-black bg-blue-500"></div>
<div class="h-20 w-full border border-black bg-blue-500"></div>
</div>
<style>
.h-96{
height: 24rem;
}
.w-52{
width: 13rem;
}
.overflow-hidden{
overflow: hidden;
}
.bg-black{
background-color: rgb(0 0 0);
}
.w-full{
width:100%;
}
.border{
border-width: 1px;
}
.border-black{
border-color: rgb(0 0 0);
}
.bg-blue-500{
background-color: rgb(59 130 246);
}
.h-fit{
height: fit-content;
}
.space-y-1{
margin-top: 0.25rem;
}
.overflow-y-scroll{overflow-y: scroll;}
.h-10{height: 2.5rem;}
.bg-red-500{background-color: rgb(239 68 68);}
.h-20{height: 5rem;}
.h-\[100px\]{
height:100px;
}
</style>
# # #您可以使容器伸縮,并使藍色項目不可收縮。
容器的相關附加類:flex flex-col。 對于藍色項目:收縮-0。
<div id="container" class="h-96 w-52 overflow-hidden bg-black flex flex-col">
<div class="h-[100px] w-full border border-black bg-blue-500 shrink-0"></div>
<div id="item-container" class="space-y-1 overflow-y-scroll">
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
<div class="h-10 w-full bg-red-500"></div>
</div>
<div class="h-20 w-full border border-black bg-blue-500 shrink-0"></div>
<div class="h-20 w-full border border-black bg-blue-500 shrink-0"></div>
</div>