纯CSS实现吸顶效果

1,217 阅读1分钟
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<style type="text/css">
			.box {
				position: -webkit-sticky;
				position: sticky;
				width: 100%;
				height: 30px;
				text-align: center;
				color: #fff;
				margin-bottom: 50px;
			}
			
			.box1 {
				top: 0;
				z-index: 1;
				background: #007bff;
			}
			
			.box2 {
				top: 30px;
				z-index: 1;
				background: #ffc107;
			}
			
			.box3 {
				background: #f4516c;
			}
		</style>
	</head>

	<body>
		<div style="height:600px">滚动试试</div>
		<div class="box box1">box1</div>
		<div class="box box2">box2</div>
		<div class="box box3">box3</div>
		<div style="height:600px"></div>
	</body>

</html>