flex如何实现头部固定,内容区域滚动?

问题:类似PC端使用position:fixed能够实现的功能。

阅读 9.4k
3 个回答
html,body{
      margin: 0;
      padding: 0;
    }
    .wrap{
      display: flex;
      flex-direction: column;
      height: 100vh;
      overflow-y: hidden;
    }
    .head{
      height: 80px;
      background: #f00;
    }
    .footer{
      height: 80px;
      background: #fc0;
    }
    .content{
      display: flex;
      flex-direction: column;
      flex: 1;
      overflow-y: scroll;
    }
    .item{
      display: flex;
      height: 200px;
      flex-shrink: 0;
      background: #1ab394;
      margin-top: 10px;
    }
<div class="wrap">
    <div class="head">头部</div>
    <div class="content">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
      <div class="item">5</div>
    </div>
    <div class="footer">底部</div>
  </div>

<iframe height='265' scrolling='no' title='flex-srcoll' src='//codepen.io/rolitter/embed/mzLOxm/?height=265&theme-id=light&default-tab=result' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen flex-srcoll by rolitter (@rolitter) on CodePen.
</iframe>

我平常是给头部固定高度,然后内容页给height: calc(100% - 头部高度);overflow-y: scoll;来实现的。话说为什么要用flex去实现这个?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题