padding-bottom+负margin-top实现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>padding-bottom+负margin-top实现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 {
      width: 240px;
      height: 360px;
      resize: vertical;
      overflow-y: scroll;
      -webkit-overflow-scrolling: touch;
    }
    .wrapper {
      width: 100%;
      /*关键:min-height为100%或100vh*/
      min-height: 100%;
    }
    .content {
      /*关键:padding-bottom应等于footer的高度*/
      padding-bottom: 60px;
      width: 100%;
      padding-top: 20px;
      text-align: center;
      color: #fff;
      font-size: 20px;
      background-color: #71a8da;
    }
    .content p:first-of-type {
      margin-top: 0;
    }
    .footer {
      /*关键:margin-top应等于footer高度的负值*/
      margin-top: -60px;
      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="wrapper">
      <div class="content" id="content">
        <p>这里是内容</p>
      </div>
    </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-2.html

2+

效果:

Scroll Up