

实现代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}.box {width: 100%;height: 300px;display: flex;overflow-x: auto;/* 第一个参数表示滚动方向,x:横向滚动 y:纵向滚动 *//* 第二个参数表示吸附方式,mandatory:表示强制吸附 proximity:表示接近吸附*/scroll-snap-type: x mandatory;}.item {width: 100%;height: 100%;flex-shrink: 0;/* 吸附的对齐方式,start表示开始位置 center表示中间位置,end表示结束位置 */scroll-snap-align: start;/* 停在下一个元素 */scroll-snap-stop: always;}.item:nth-child(1) {background-color: rgb(13, 255, 0);}.item:nth-child(2) {background-color: rgb(255, 0, 149);}.item:nth-child(3) {background-color: rgb(255, 242, 0);}.item:nth-child(4) {background-color: rgb(0, 255, 47);}</style></head><body><div class="box"><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div></div></body>
</html>