<template>
<div>
<div class="scroll-selector" ref="scrollSelector">
<div class="item" v-for="(item, index) in items" :key="index">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], // 你的选项列表
selectedValue: 5, // 当前选择的值
};
},
mounted() {
this.setInitialValue();
},
methods: {
setInitialValue() {
const container = this.$refs.scrollSelector;
if (container) {
const containerHeight = container.clientHeight;
const itemHeight = container.firstChild.clientHeight;
const middleIndex = Math.floor(containerHeight / (2 * itemHeight));
this.selectedValue = this.items[middleIndex];
container.scrollTop = middleIndex * itemHeight;
}
},
},
};
</script>
<style>
.scroll-selector {
height: 100px; /* 自定义滚动选择器的高度 */
overflow: auto;
}
</style>