CSS Flexbox-滚动与溢出滚动不可见:y

时间:2019-08-09 13:23:48

标签: html css flexbox

我有以下代码段:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body style='overflow: hidden;'>
<style>
        *, :after, :before {
            box-sizing: border-box;
        }

        .example {
            display: flex;
            flex-direction: column;
            justify-content: flex-end;
            height: 100%;
            max-height: 100%;
        }

        .bottom {
            overflow: auto;
            height: 140px;
            max-height: 100%;
            flex-shrink: 0;
            white-space: pre-wrap;
        }

        .top {
            position: relative;
            display: flex;
        }

        .scroll {
            overflow-y: scroll;
            width: 100%;
        }

        .elements {
            position: relative;
        }
        .test {
            margin-top: 20px;
        }
    </style>
<article class="example">
    <div class="top">
        <div class="scroll">
            <article class="elements">
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
                <article class="test">
                    TEST
                </article>
            </article>
        </div>
    </div>
    <article class="bottom">
        Bottom
    </article>
</article>
</body>
</html>
这个想法是“底部”文本总是可见的,带有“测试”元素的区域是可滚动的-但是滚动不起作用。您想花一分钟时间帮我弄清楚吗?预先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您没有为文章容器设置高度(也可以使用overflow:auto;)。

.elements {
   position: relative;
   overflow: auto;
   height: 150px;
}

答案 1 :(得分:1)

对您的代码进行位更改

*,
 :after,
 :before {
  box-sizing: border-box;
}

/* some normalization to document */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.example {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  height: 100%;
  max-height: 100%;
}

.bottom {
  overflow: auto;
  height: 140px;
  max-height: 100%;
  flex-shrink: 0;
  white-space: pre-wrap;
}

/* handling overflow here */

.top {
  position: relative;
  display: flex;
  overflow: auto;
}

/* removed overflow here */

.scroll {
  width: 100%;
}

.elements {
  position: relative;
}

.test {
  margin-top: 20px;
}
<article class="example">
  <div class="top">
    <div class="scroll">
      <article class="elements">
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
        <article class="test">
          TEST
        </article>
      </article>
    </div>
  </div>
  <article class="bottom">
    Bottom
  </article>
</article>