1 回答
TA貢獻1827條經驗 獲得超8個贊
通過一些重組,您將能夠僅使用 CSS 來實現您想要的效果。在這個實現中,我使用了:not CSS 選擇器,并結合了一些多級:hover選擇器(因為.container和.child占用相同的空間而起作用)
雖然,只是作為一個建議:利用 flex 的自動增長,并width: 20%;完全刪除邏輯。這樣,未懸停組件的子組件將占用相等的空間,懸停的子組件仍將增長到 80% 的寬度。這將允許您動態添加更多項目,而無需更改硬編碼的 CSS。
.container {
display: flex;
}
.child {
width: 50%;
height: 150px;
padding: 10px;
transition: all 0.5s;
}
.child--red {
background-color: rgba(227, 29, 103, 1);
}
.container:hover .child--red:not(:hover) {
background-color: rgba(227, 29, 103, 0.4);
}
.child--green {
background-color: rgba(40, 251, 202, 1);
}
.container:hover .child--green:not(:hover) {
background-color: rgba(40, 251, 202, 0.4);
}
.container:hover .child:not(:hover) span {
display: none;
}
.container:hover .child {
width: 20%;
}
.container:hover .child:hover {
width: 80%;
}
<div class="container">
<div class="child child--green">
<span>This content will show up directly in its container.</span>
</div>
<div class="child child--red">
<span>This content will show up directly in its container.</span>
</div>
</div>
添加回答
舉報
