滑动吸顶 固定导航可使用粘性定位

position: sticky; 粘性定位

sticky(粘性定位)解释:

  • 粘性定位可以被认为是相对定位和固定定位的混合。元素在跨越特定阈值前为相对定位,之后为固定定位。设置该属性的元素并不脱离文档流,仍然保留元素原本在文档流中的位置。

  • 必须添加 top 、left、right、bottom 其中一个才有效

  • 父元素不能overflow:hidden或者overflow:auto属性。(会不起作用)

  • 父元素的高度不能低于sticky元素的高度

  • sticky元素仅在其父元素内生效

 使用粘性定位将页面内tab滑动后吸顶

<!DOCTYPE html>
<!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>Document</title>
  <style>
    .contentBox {
      height: 1500px;
      width: 100%;
      background-color: #fff;
    }
    .box1 {
      background-color: pink;
      height: 100px;
    }
    .box2 {
      background-color: #ccc;
      height: 100px;
    }
    .box3 {
      height:1000px;
      background-color: skyblue;
    }
    .sticky{
      position: sticky;
      top: 0;
    }
  </style>
</head>
 
<body>
  <div class="contentBox">
    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">
        3
        <p class="sticky">p的头部滑到顶部被吸住</p>
        <p>p的内容</p>
        <p>p的内容</p>
        <p>0的内容</p>
    </div>
  </div>
</body>
 
</html>

效果

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值