如何创建包含多行的水平可滚动容器

时间:2020-12-20 01:25:31

标签: javascript html css flexbox

我试图制作一个数据网格,其中粘性导航栏可以在其下方的内容旁边水平滚动。然而,行为在两个场景中发生了变化。

  • 当最外面的框太窄时,主区域可以水平滚动,但是 navrows 的宽度被限制在父框的宽度上,导致不受欢迎的行为 背景颜色。
    • 这可以通过将 navrows 的宽度都设置为 fit-content 来解决
  • 当由于宽度设置为 fit-content 导致最外框太宽时,所有行都会收缩到左侧而不是填充整行。
    • 这可以通过将 navrows 的宽度都设置为 100% 来解决

那么我应该怎么做才能让它们在两种情况下都能正确运行。

我尝试过的其他解决方案:

  • flex-rowwarp 一起使用,但是,当内容的宽度太小时,导航栏和行将在同一行
  • inline-block中使用nav,在inline-flex中使用rows,行为与上述相同。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .grid {
            height: 60px;
            width: 200px;
            overflow: auto;
        }
        .subgrid {

        }
        .nav {
            position: sticky;
            top: 0;
            background: #da1039;
        }
        .rows {
            display: flex;
            flex-direction: column;
            flex-wrap: nowrap;
            background: #00b89c;
        }
        .row {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            justify-content: space-evenly;
        }
        .rowitem {
            border: 1px solid;
            min-width: 20px;
        }
    </style>
</head>
<body>
    <div class="grid">
        <div class="subgrid">
        <div class="nav">
            <div class="row">
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
            </div>
        </div>
        <div class="rows">
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
        </div>
        </div>
    </div>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .grid {
            height: 60px;
            width: 50px;
            overflow: auto;
        }
        .subgrid {

        }
        .nav {
            position: sticky;
            top: 0;
            background: #da1039;
        }
        .rows {
            display: flex;
            flex-direction: column;
            flex-wrap: nowrap;
            background: #00b89c;
        }
        .row {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            justify-content: space-evenly;
        }
        .rowitem {
            border: 1px solid;
            min-width: 20px;
        }
    </style>
</head>
<body>
    <div class="grid">
        <div class="subgrid">
        <div class="nav">
            <div class="row">
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
            </div>
        </div>
        <div class="rows">
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
        </div>
        </div>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您实际上可以将两者结合起来。

例如:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .grid-1 {
            height: 60px;
            width: 200px;
            overflow: auto;
        }

        .grid-2 {
            height: 60px;
            width: 900px;
            overflow: auto;
        }
        /* here */
        .nav {
            min-width: fit-content;
            max-width: 100%;
            position: sticky;
            top: 0;
            background: #da1039;
        }
        .rows {
            min-width: fit-content;
            max-width: 100%;
            display: flex;
            flex-direction: column;
            flex-wrap: nowrap;
            background: #00b89c;
        }
        .row {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            justify-content: space-evenly;
        }
        .rowitem {
            border: 1px solid;
            min-width: 20px;
        }
    </style>
</head>
<body>
    <div class="grid-1">
        <div class="nav">
            <div class="row">
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
            </div>
        </div>
        <div class="rows">
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
        </div>
        </div>
    </div>
    <div style="height: 20px"></div>
    <div class="grid-2">
        <div class="nav">
            <div class="row">
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
                <div class="rowitem">HHH</div>
            </div>
        </div>
        <div class="rows">
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
            <div class="row">
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
                <div class="rowitem">AAA</div>
            </div>
        </div>
    </div>
</body>
</html>