使用CSS实现滚动吸附效果

使用CSS实现滚动吸附效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title></title>
		<style>
			*{
				margin: 0;padding: 0;
			}
			.container{
				/*竖向
				 width: 100%;
				height: 100vh;
				overflow-y:scroll;
				scroll-snap-type: y proximity; */
				width: 100%;
				height: 400px;
				display: flex;
				overflow-x:scroll;
				scroll-snap-type: x mandatory;
			}
			.container::-webkit-scrollbar{
				width: 0;
			}
			.item{
				width: 100%;
				height: 100%;
				flex-shrink: 0;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 16px;
				scroll-snap-align: start;
				scroll-snap-stop: always;
				/* 竖向的时候  隐藏 scroll-snap-stop: always;height:60vh;*/
				
			}
			.item:nth-child(1){
				background-color: aqua;
			}
			.item:nth-child(2){
				background-color: rebeccapurple;
			}
			.item:nth-child(3){
				background-color:green;
			}
		</style>
	</head>
	<body>
		<div class="container">
			<div class="item">1</div>
			<div class="item">2</div>
			<div class="item">3</div>
		</div>
	</body>
</html>

你可能感兴趣的:(css,前端)