我想在flexbox布局中包含一個帶滾動條的圖庫,這樣當瀏覽器頁面大小改變時,圖庫也應該根據flex-grow索引進行縮放,這樣圖庫中的所有圖像仍然可以通過滾動條訪問。因此,我不想使用絕對單位。然而,我已經嘗試了幾個論壇和網站上建議的多種方法,但我仍然無法通過將畫廊包含在我的flexbox布局中來讓它工作。 預先非常感謝您的任何建議。
示例1:這是當瀏覽器頁面變得比顯示的圖像更小時,我想要實現的圖像庫行為:
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
background-color: dimgray;
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}
.gallery {
flex: 1 1 50%;
white-space: nowrap;
overflow: scroll;
}
</style>
</head>
<body>
<!-- Desired behaviour-->
<div class="gallery">
<img src="https://via.placeholder.com/100x100" alt="Tile1">
<img src="https://via.placeholder.com/100x100" alt="Tile2">
<img src="https://via.placeholder.com/100x100" alt="Tile3">
<img src="https://via.placeholder.com/100x100" alt="Tile4">
<img src="https://via.placeholder.com/100x100" alt="Tile5">
<img src="https://via.placeholder.com/100x100" alt="Tile6">
<img src="https://via.placeholder.com/100x100" alt="Tile7">
<img src="https://via.placeholder.com/100x100" alt="Tile8">
<img src="https://via.placeholder.com/100x100" alt="Tile9">
<img src="https://via.placeholder.com/100x100" alt="Tile10">
<img src="https://via.placeholder.com/100x100" alt="Tile11">
<img src="https://via.placeholder.com/100x100" alt="Tile12">
<img src="https://via.placeholder.com/100x100" alt="Tile13">
<img src="https://via.placeholder.com/100x100" alt="Tile14">
</div>
</body>
</html>
示例2:這是我的flexbox布局,我想在其中集成圖片庫:
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
background-color: dimgray;
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}
.container {
display: flex;
flex-direction: column;
height: 100%;
}
/************************/
.column {
display: flex;
flex-flow: column nowrap;
border: 1px solid black;
padding: 10px;
}
.column:nth-child(1) {
flex: 4 0 40%;
}
.column:nth-child(2) {
flex: 3 0 30%;
}
/************************/
.row {
display: flex;
flex-direction: row;
flex-flow: row nowrap;
}
.row:nth-child(1) {
flex: 1;
}
/************************/
.sub-row {
border-bottom: 1px solid black;
padding: 5px 0;
color: white;
}
.sub-row:last-child {
border-bottom: none;
}
/************************/
#row-middle-col-left1,
#row-middle-col-left2 {
flex-grow: 4;
}
#row-middle-col-right1 {
flex-grow: 10;
}
#row-middle-col-right2 {
/*max-width: 800px;*/
}
/************************/
.gallery {
flex: 1 1 50%;
white-space: nowrap;
overflow-x: scroll;
min-width: 0;
}
</style>
</head>
<body>
<!-- Not working like expected-->
<div class="container">
<div class="row" id="row-middle">
<div class="column" id="row-middle-col-middle">
<div class="sub-row" id="row-middle-col-left1">M1</div>
<div class="sub-row" id="row-middle-col-left2">M2</div>
</div>
<div class="column" id="row-middle-col-right">
<div class="sub-row" id="row-middle-col-right1">R1</div>
<div class="sub-row" id="row-middle-col-right2">R2
<div class="gallery">
<img src="https://via.placeholder.com/100x100" alt="Image1">
<img src="https://via.placeholder.com/100x100" alt="Image2">
<img src="https://via.placeholder.com/100x100" alt="Image3">
<img src="https://via.placeholder.com/100x100" alt="Image4">
<img src="https://via.placeholder.com/100x100" alt="Image5">
<img src="https://via.placeholder.com/100x100" alt="Image6">
<img src="https://via.placeholder.com/100x100" alt="Image7">
<img src="https://via.placeholder.com/100x100" alt="Image8">
<img src="https://via.placeholder.com/100x100" alt="Image9">
<img src="https://via.placeholder.com/100x100" alt="Image10">
<img src="https://via.placeholder.com/100x100" alt="Image11">
<img src="https://via.placeholder.com/100x100" alt="Image12">
<img src="https://via.placeholder.com/100x100" alt="Image13">
<img src="https://via.placeholder.com/100x100" alt="Image14">
</div>
</div>
</div>
</div>
</div>
</body>
</html>
對我最終找到的解決方案感興趣的人來說: 我給了畫廊一個固定的尺寸,然后從伸縮基準中減去這個尺寸:
#row-middle-col-right2 {
display: inline-flex;
flex-flow: row nowrap;
flex: 1 1 (10% - 500px);
overflow-x: scroll;
}
.gallery {
display: flex;
flex-flow: row nowrap;
width: 500px;
}
完整的代碼如下所示:
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
background-color: dimgray;
overflow: hidden;
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
}
.container {
display: inline-flex;
height: 100%;
width: 100%;
}
.column {
display: inline-flex;
flex-flow: column nowrap;
border: 1px solid black;
padding: 10px;
}
.column:nth-child(1) {
flex: 2 2 20%;
}
.column:nth-child(2) {
flex: 3 3 30%;
}
.row {
display: inline-flex;
flex-flow: row nowrap;
}
.row:nth-child(1) {
flex: 1 1 100%;
}
.sub-row {
display: inline-flex;
flex-flow: row nowrap;
border-bottom: 1px solid black;
}
.sub-row:last-child {
border-bottom: none;
}
#row-middle-col-left1,
#row-middle-col-left2 {
display: flex;
flex: 2 2 20%;
flex-flow: row nowrap;
}
#row-middle-col-right1 {
display: flex;
flex-flow: row nowrap;
flex: 6 6 60%;
}
#row-middle-col-right2 {
display: inline-flex;
flex-flow: row nowrap;
flex: 1 1 (10% - 500px);
overflow-x: scroll;
}
.gallery {
display: flex;
flex-flow: row nowrap;
width: 500px;
}
.gallery img {
white-space: nowrap;
padding: 10px;
}
</style>
</head>
<body>
<!-- Not working like expected-->
<div class="container">
<div class="row" id="row-middle">
<div class="column" id="row-middle-col-middle">
<div class="sub-row" id="row-middle-col-left1">M1</div>
<div class="sub-row" id="row-middle-col-left2">M2</div>
</div>
<div class="column" id="row-middle-col-right">
<div class="sub-row" id="row-middle-col-right1">R1</div>
<div class="sub-row" id="row-middle-col-right2">R2
<div class="gallery">
<img src="https://via.placeholder.com/100x100" alt="Image1">
<img src="https://via.placeholder.com/100x100" alt="Image2">
<img src="https://via.placeholder.com/100x100" alt="Image3">
<img src="https://via.placeholder.com/100x100" alt="Image4">
<img src="https://via.placeholder.com/100x100" alt="Image5">
<img src="https://via.placeholder.com/100x100" alt="Image6">
<img src="https://via.placeholder.com/100x100" alt="Image7">
<img src="https://via.placeholder.com/100x100" alt="Image8">
<img src="https://via.placeholder.com/100x100" alt="Image9">
<img src="https://via.placeholder.com/100x100" alt="Image10">
<img src="https://via.placeholder.com/100x100" alt="Image11">
<img src="https://via.placeholder.com/100x100" alt="Image12">
<img src="https://via.placeholder.com/100x100" alt="Image13">
<img src="https://via.placeholder.com/100x100" alt="Image14">
</div>
</div>
</div>
</div>
</div>
</body>
</html>