flex实现(伪)顶部悬浮


这种方法和position: fixed;不同,不绝对浮动在顶部,原理是将页面分成独立的区块,滚动条使用的是overflow-y: scroll;实现。

效果:

效果和使用flxed差不多,但是耦合度更低,可以随意调上下位置

代码实现:

DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Documenttitle>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .d1 {
      display: flex;
      flex-direction: column;
      height: 100vh;
    }
    .d2 {
      height: 200px;
      background-color: red;
    }
    .d3 {
      flex: 1;
      background-color: blue;
      overflow-y: scroll;
    }
  style>
head>
<body>
  <div class="d1">
    <div class="d2">头部div>
    <div class="d3">
      <div style="height: 2000px;">div>
    div>
  div>
body>
html>