当前位置:首页 > VUE

vue实现picker

2026-01-12 22:17:34VUE

Vue 实现 Picker 组件

使用原生 HTML 和 Vue 实现基础 Picker

通过 <select><option> 标签结合 Vue 的 v-model 实现数据绑定:

<template>
  <select v-model="selectedOption">
    <option v-for="option in options" :value="option.value">
      {{ option.text }}
    </option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      selectedOption: '',
      options: [
        { text: '选项1', value: '1' },
        { text: '选项2', value: '2' }
      ]
    }
  }
}
</script>

使用第三方库(如 Vant)

Vant 提供了现成的 Picker 组件,支持多列选择和复杂数据:

<template>
  <van-picker
    v-model="selectedValue"
    :columns="columns"
    @confirm="onConfirm"
  />
</template>

<script>
import { Picker } from 'vant';

export default {
  components: { 'van-picker': Picker },
  data() {
    return {
      selectedValue: '',
      columns: ['杭州', '宁波', '温州']
    }
  },
  methods: {
    onConfirm(value) {
      console.log('选择的值:', value);
    }
  }
}
</script>

自定义 Picker 组件

通过 CSS 和 Vue 事件实现滚动选择器:

<template>
  <div class="custom-picker">
    <div 
      v-for="item in items" 
      :key="item"
      @click="selectItem(item)"
      :class="{ selected: item === selectedItem }"
    >
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItem: null,
      items: ['选项A', '选项B', '选项C']
    }
  },
  methods: {
    selectItem(item) {
      this.selectedItem = item;
      this.$emit('select', item);
    }
  }
}
</script>

<style>
.custom-picker {
  display: flex;
  flex-direction: column;
  max-height: 200px;
  overflow-y: auto;
}
.custom-picker div {
  padding: 10px;
  cursor: pointer;
}
.custom-picker .selected {
  background-color: #f0f0f0;
}
</style>

移动端优化方案

针对移动端触摸事件实现平滑滚动:

// 在自定义组件中添加触摸事件处理
mounted() {
  this.$el.addEventListener('touchstart', this.handleTouchStart);
  this.$el.addEventListener('touchmove', this.handleTouchMove);
},
methods: {
  handleTouchStart(e) {
    this.startY = e.touches[0].clientY;
  },
  handleTouchMove(e) {
    const deltaY = e.touches[0].clientY - this.startY;
    this.$el.scrollTop -= deltaY;
    this.startY = e.touches[0].clientY;
  }
}

性能优化建议

对于大型数据集考虑虚拟滚动技术:

vue实现picker

// 使用 vue-virtual-scroller 等库
import { RecycleScroller } from 'vue-virtual-scroller';

components: {
  RecycleScroller
},
// 模板中使用
<RecycleScroller
  :items="largeData"
  :item-size="50"
  key-field="id"
  v-slot="{ item }"
>
  <div @click="selectItem(item)">{{ item.name }}</div>
</RecycleScroller>

标签: vuepicker
分享给朋友:

相关文章

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

vue 实现templateurl

vue 实现templateurl

在 Vue 中,templateUrl 是 Angular 中的一个概念,用于指定外部 HTML 文件作为模板。Vue 本身不直接支持 templateUrl,但可以通过以下方法实现类似功能: 使用…

vue实现销售程序

vue实现销售程序

Vue实现销售程序的基本步骤 搭建Vue项目基础结构 使用Vue CLI或Vite初始化项目,安装必要依赖(如Vue Router、Vuex/Pinia、Element UI/Ant Design V…

vue 实现登录验证

vue 实现登录验证

Vue 实现登录验证的方法 使用 Vue Router 进行路由守卫 在 Vue 项目中,可以通过 Vue Router 的路由守卫(Navigation Guards)实现登录验证。路由守卫可以在路…