示例
by acccco
HTML
<!-- Learn about this code on MDN: https://developer.mozilla.org/zh-CN/docs/Web/CSS/scroll-snap-type -->
<div class="container mandatoryScrollSnapping">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<div class="container proximityScrollSnapping">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
CSS
.container {
width: 100%;
overflow: auto;
white-space: nowrap;
scroll-snap-points-x: repeat(100%);
scroll-snap-type: mandatory;
font-size: 0;
}
.mandatoryScrollSnapping {
margin-bottom: 20px;
scroll-snap-type: x mandatory;
}
.proximityScrollSnapping {
scroll-snap-type: x proximity;
}
.container > div {
width: 100%;
height: 100px;
display: inline-block;
line-height: 100px;
text-align: center;
font-size: 50px;
scroll-snap-align: center;
}
.container > div:nth-child(even) {
background-color: #87EA87;
}
.container > div:nth-child(odd) {
background-color: #87CCEA;
}