先摆脱自己的标题党嫌疑。先上代码。
.box{
scroll-snap-type: x mandatory;
}
.items{
scroll-snap-align: center;
}
这两句,可以完成一个简易的,拖拽翻片儿附带吸附性的轮播图。
当然这个是不能自动播放的轮播,如果需要自动播放,可能还需要加上animate库就可以了。
整套代码如下:
<template>
<div class="box">
<div class="box_items"></div>
<div class="box_items"></div>
<div class="box_items"></div>
<div class="box_items"></div>
</div>
</template>
<style lang="scss" scoped>
.box {
width: 200px;
height: 200px;
border: 1px solid black;
overflow: auto;
white-space: nowrap;
scroll-snap-type: x mandatory;
&_items {
width: 200px;
height: 100px;
display: inline-block;
scroll-snap-align: center;
&:nth-of-type(n) {
background: palevioletred;
}
&:nth-of-type(2n) {
background: green;
}
}
}
</style>