flex弹性盒子布局实现sticky footer

代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
  <title>flex弹性盒子布局实现sticky footer</title>
  <style>
    body {
      margin: 0;
    }
    .tip {
      margin: 10px 0;
      font-size: 14px;
    }
    input {
      margin-bottom: 15px;
      border: none;
      border-radius: 4px;
      padding: 5px 10px;
      font-size: 14px;
      color: #fff;
      background-color: #f85f2f;
      cursor: pointer;
      -webkit-appearance: none;
    }
    input + input {
      margin-left: 10px;
    }
    .container {
      /*关键:flex布局*/
      display: flex;
      flex-direction: column;
      width: 240px;
      height: 360px;
      overflow-y: scroll;
      -webkit-overflow-scrolling: touch;
    }
    .content {
      /*关键:flex为1,有多余空间就增大,所以始终往下撑*/
      flex: 1;
      width: 100%;
      text-align: center;
      color: #fff;
      font-size: 20px;
      background-color: #71a8da;
    }
    .footer {
      height: 60px;
      width: 100%;
      line-height: 60px;
      text-align: center;
      color: #fff;
      font-size: 20px;
      background-color: #f85f2f;
    }
  </style>
</head>
<body>
  <p class="tip">通过点击添加/清空按钮,查看内容增/减对页脚的影响</p>
  <p class="tip">提示:多点几次添加,让内容超过一屏,然后上下滑动查看效果</p>
  <input id="add" type="button" value="添加一段话">
  <input id="clear" type="button" value="清空">
  <div class="container">
    <div class="content" id="content">
      <p>这里是内容</p>
    </div>
    <div class="footer">这里是页脚</div>
  </div>
  <script>
    let addBtn = document.getElementById('add')
    let clearBtn = document.getElementById('clear')
    let content = document.getElementById('content')
    addBtn.addEventListener('click', () => {
      let p = document.createElement('p')
      p.innerText = '内容逐渐变多了'
      content.append(p)
    })
    clearBtn.addEventListener('click', () => {
      content.innerHTML = '<p>这里是内容</p>'
    })
  </script>
</body>
</html>

@sticky-footer-1.html

1+

效果:

Scroll Up