当前位置:首页 > VUE

vue实现点选效果

2026-02-17 07:50:40VUE

Vue 实现点选效果的方法

使用 v-bindv-on 绑定动态类与点击事件

通过 v-bind:class 动态绑定 CSS 类,结合 v-on:click 切换选中状态。以下是一个基础实现:

<template>
  <div 
    v-for="item in items" 
    :key="item.id"
    :class="{ 'active': selectedItem === item.id }"
    @click="selectItem(item.id)"
  >
    {{ item.name }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' }
      ],
      selectedItem: null
    };
  },
  methods: {
    selectItem(id) {
      this.selectedItem = id;
    }
  }
};
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

使用计算属性管理选中状态

通过计算属性判断当前项是否被选中,适合复杂逻辑的场景:

vue实现点选效果

<template>
  <div 
    v-for="item in items" 
    :key="item.id"
    :class="getItemClass(item.id)"
    @click="selectItem(item.id)"
  >
    {{ item.name }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' }
      ],
      selectedItem: null
    };
  },
  computed: {
    getItemClass() {
      return (id) => ({
        'active': this.selectedItem === id,
        'disabled': id === 2 // 示例:额外条件
      });
    }
  },
  methods: {
    selectItem(id) {
      this.selectedItem = id;
    }
  }
};
</script>

使用 Vuex 管理全局选中状态

若需跨组件共享选中状态,可通过 Vuex 实现:

vue实现点选效果

// store.js
import { createStore } from 'vuex';

export default createStore({
  state: {
    selectedItem: null
  },
  mutations: {
    setSelectedItem(state, id) {
      state.selectedItem = id;
    }
  }
});

组件中调用:

<template>
  <div 
    v-for="item in items" 
    :key="item.id"
    :class="{ 'active': $store.state.selectedItem === item.id }"
    @click="$store.commit('setSelectedItem', item.id)"
  >
    {{ item.name }}
  </div>
</template>

使用第三方库实现高级交互

对于拖拽点选等复杂场景,可借助 vuedraggable 或自定义指令:

<template>
  <draggable 
    v-model="items" 
    @start="onDragStart" 
    @end="onDragEnd"
  >
    <div 
      v-for="item in items" 
      :key="item.id"
      :class="{ 'active': selectedItem === item.id }"
      @click="selectItem(item.id)"
    >
      {{ item.name }}
    </div>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable';
export default {
  components: { draggable },
  data() {
    return {
      items: [/* ... */],
      selectedItem: null
    };
  },
  methods: {
    selectItem(id) {
      this.selectedItem = id;
    },
    onDragStart() { /* ... */ },
    onDragEnd() { /* ... */ }
  }
};
</script>

注意事项

  • 性能优化:对于长列表,建议使用 Object.freeze 冻结静态数据或虚拟滚动。
  • 无障碍访问:为选中项添加 aria-selected 属性:
    <div 
      :aria-selected="selectedItem === item.id"
    >
    </div>
  • 多选支持:将 selectedItem 改为数组即可实现多选逻辑。

标签: 点选效果
分享给朋友:

相关文章

vue实现日历效果

vue实现日历效果

Vue实现日历效果的方法 使用第三方库 Vue生态中有多个成熟的日历组件库,如v-calendar或vue2-calendar,可以快速实现日历功能。安装后直接引入组件即可使用。 npm i…

vue实现iframe效果

vue实现iframe效果

使用 Vue 实现 iframe 效果 在 Vue 中实现 iframe 效果可以通过直接使用 <iframe> 标签或结合动态绑定实现更灵活的功能。以下是几种常见方法: 基础 ifra…

vue实现歌词效果

vue实现歌词效果

Vue实现歌词效果 数据准备 歌词通常以时间轴和文本形式存储,可采用数组或对象格式。例如: lyrics: [ { time: 0.5, text: "第一句歌词" }, { time: 3…

vue 实现货架效果

vue 实现货架效果

实现货架效果的基本思路 货架效果通常指在网页中展示商品或物品的网格布局,可能包含拖拽排序、分类筛选、动画交互等功能。Vue.js 结合第三方库(如 vue-draggable 或 CSS Grid)可…

vue实现翻页动画效果

vue实现翻页动画效果

Vue 实现翻页动画效果的方法 使用 CSS Transition 和 Vue 过渡 在 Vue 中可以通过 <transition> 组件结合 CSS 实现翻页动画效果。定义一个过渡类名…

vue实现步骤动画效果

vue实现步骤动画效果

Vue实现步骤动画效果的方法 在Vue中实现步骤动画效果可以通过多种方式完成,以下是一些常见的方法: 使用Vue的过渡和动画系统 Vue提供了内置的过渡和动画系统,可以通过<transitio…