我有一個 2×2 CSS 網格,它使用固定定位填滿瀏覽器視窗。它看起來像這樣:
----------------- | |xxxxxxxxxxxxx| ----------------- |x| | |x| | |x| | |x| | -----------------
頂行和左列各自適合其內容且大小為auto
。底行和右列分別用於佔用任何剩餘空間並具有大小 1fr
。
東北和西南單元格(圖中充滿了 X)都是可滾動的 Flex 父單元。它們包含任意數量的子元素,並且該數量可能會動態變化。我將每個屬性的 overflow
屬性設為 auto
並使用這些規則集設定捲軸的樣式:
.scrollable::-webkit-scrollbar { height: 10px; width: 10px; } .scrollable::-webkit-scrollbar-thumb { background: darkgray; } .scrollable::-webkit-scrollbar-track { background: #666666; }
當我在 macOS 上的 Chrome 中載入頁面時,捲軸會覆蓋在西南單元格的內容上。然後,當我調整瀏覽器視窗大小時,左列會展開以容納捲軸(它已經顯示為覆蓋層),並且它會移動到內容的右側。
我希望滾動條根本不覆蓋內容,並且我真的很想消除不相關的調整大小事件上的佈局移位。你知道我如何實現這些目標嗎?
此 Codepen 是一個最小的可重現範例。然而,它對我來說並不一致。當我重新加載頁面時,有時滾動條被覆蓋,有時該列已經足夠寬,滾動條可以顯示在內容的右側。我在這個獨立頁面上看到了錯誤的覆蓋和尺寸更一致。
P粉5026087992023-09-07 15:32:46
為了防止這種情況,網格需要更新其尺寸並考慮捲軸。另一個問題是如何僅使用 css 來做到這一點。我想到為 grid-template-columns
應用 animation
,它似乎有效:
* { box-sizing:border-box; } #grid { position: fixed; left: 0; right: 0; top: 0; bottom: 0; display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto 1fr; animation: grid 200ms; } @keyframes grid { to { grid-template-columns: auto 2fr; } } #nw { background-color: green; } .scrollable { display: flex; } #ne { background-color: red; flex-direction: row; overflow-x: auto; } #sw { background-color: yellow; flex-direction: column; overflow-y: auto; scrollbar-color: darkgray #666666; scrollbar-width: thick; } #se { background-color: blue; } .box { width: 4em; height: 4em; margin: 5px; border-radius: 5px; background-color: black; flex-shrink: 0; } .scrollable::-webkit-scrollbar { height: 10px; width: 10px; } .scrollable::-webkit-scrollbar-thumb { background: darkgray; } .scrollable::-webkit-scrollbar-track { background: #666666; }
<div id="grid"> <div id="nw"></div> <div id="ne" class="scrollable"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <div id="sw" class="scrollable"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <div id="se"></div> </div>
如果你想用 JavaScript 來實作:
setTimeout(() => grid.style.gridTemplateColumns = 'auto 2fr', 200)
* { box-sizing:border-box; } #grid { position: fixed; left: 0; right: 0; top: 0; bottom: 0; display: grid; grid-template-rows: auto 1fr; grid-template-columns: auto 1fr; } #nw { background-color: green; } .scrollable { display: flex; } #ne { background-color: red; flex-direction: row; overflow-x: auto; } #sw { background-color: yellow; flex-direction: column; overflow-y: auto; scrollbar-color: darkgray #666666; scrollbar-width: thick; } #se { background-color: blue; } .box { width: 4em; height: 4em; margin: 5px; border-radius: 5px; background-color: black; flex-shrink: 0; } .scrollable::-webkit-scrollbar { height: 10px; width: 10px; } .scrollable::-webkit-scrollbar-thumb { background: darkgray; } .scrollable::-webkit-scrollbar-track { background: #666666; }
<div id="grid"> <div id="nw"></div> <div id="ne" class="scrollable"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <div id="sw" class="scrollable"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <div id="se"></div> </div>