使用vant ui list列表check方法检查滚动跳的位置
时间: 2024-05-14 19:18:11 浏览: 151
可以使用`scroll-to-index`和`on-scroll`两个事件来实现。首先,在list组件上添加`ref`属性,然后在mounted生命周期中获取该组件的高度和每个item的高度。接着,监听`on-scroll`事件,获取当前滚动的距离并计算当前滚动到了第几个item,再触发`scroll-to-index`事件将该item滚动到可视区域内。代码示例:
```html
<template>
<van-list ref="list" :height="listHeight" :item-height="itemHeight" @scroll-to-index="scrollToIndex" @on-scroll="onScroll">
<van-checkbox v-model="checkedList" :label="item" v-for="item in list" :key="item">{{ item }}</van-checkbox>
</van-list>
</template>
<script>
export default {
data() {
return {
list: ['选项1', '选项2', '选项3', '选项4', '选项5', '选项6', '选项7', '选项8', '选项9', '选项10'],
checkedList: [],
listHeight: 0,
itemHeight: 0
}
},
mounted() {
this.listHeight = this.$refs.list.$el.clientHeight
this.itemHeight = this.$refs.list.$el.querySelector('.van-checkbox').clientHeight
},
methods: {
onScroll(event) {
const scrollTop = event.target.scrollTop
const index = Math.floor(scrollTop / this.itemHeight)
this.$refs.list.scrollTo(index)
},
scrollToIndex(index) {
this.$refs.list.scrollTo(index)
}
}
}
</script>
```
阅读全文