Flex布局實現(xiàn)上下固定中間部分滾動

flex 布局簡介在上一篇可以找到,不再贅述。
此篇主要介紹使用Flex布局、傳統(tǒng)的 position 布局如何解決上下固定中間部分滾動,工作之余寫了個demo,直接上代碼。

Flex布局實現(xiàn)上下固定中間部分滾動:
<div class="parent">
    <div class="header">header -- 固定</div>
    <div class="content">
      <p>content -- 滾動</p>
      <p>內容部分</p>
      <p>內容部分</p>
      <p>內容部分</p>
      <p>內容部分</p>
        ......
    </div>
    <div class="footer">footer -- 固定</div>
</div>

<style>
  .parent{
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .header {
    width: 100%;
    height: 50px;
    line-height: 50px;
    background: #b6efff;
    text-align: center;
  }

  .content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
  }

  p {
    height: 70px;
    line-height: 70px;
    border-bottom: 1px solid #ccc;
  }

  .footer {
    height: 50px;
    line-height: 50px;
    background: #b6efff;
  }

</style>

傳統(tǒng)布局實現(xiàn)上下固定中間部分滾動:
<div class="parent">
    <div class="top">top</div>
    <div class="center">
      <p>center1</p>
      <p>center2</p>
      <p>center3</p>
      <p>center...</p>
      <p>center.n</p>
    </div>
    <div class="bottom">bottom</div>
</div>

<style>
  * {
    margin: 0;
    padding: 0;
  }

  .parent {
    background: blanchedalmond;
    height: 100vh;
    overflow-y: hidden;
  }

  .top,
  .bottom {
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    background: seagreen;
    text-align: center;
    position: fixed;
  }

  .bottom {
    bottom: 0;
  }

  p {
    height: 50px;
    line-height: 50px;
    border: 1px dashed darkcyan;
  }

  .center {
    height: calc(100vh - 100px);
    position: relative;
    top: 50px;
    background: #ccc;
    overflow-y: scroll;
  }
</style>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容