position: 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>
效果