当前位置:首页 > VUE

vue实现点击高亮效果

2026-02-20 23:50:53VUE

使用动态类绑定实现高亮

在Vue中可以通过v-bind:class或简写:class动态绑定CSS类来实现点击高亮效果。定义一个响应式数据存储当前选中项,点击时更新该数据。

<template>
  <div>
    <div 
      v-for="item in items" 
      :key="item.id"
      :class="{ 'active': activeItem === item.id }"
      @click="activeItem = item.id"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeItem: null,
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  }
}
</script>

<style>
.active {
  background-color: yellow;
}
</style>

使用内联样式绑定

通过v-bind:style直接动态修改元素的样式属性,适合简单的高亮效果。

<template>
  <div>
    <div 
      v-for="item in items"
      :key="item.id"
      :style="{ backgroundColor: activeItem === item.id ? 'yellow' : '' }"
      @click="activeItem = item.id"
    >
      {{ item.text }}
    </div>
  </div>
</template>

多选高亮实现

需要高亮多个元素时,可以使用数组或对象存储选中状态。

<template>
  <div>
    <div 
      v-for="item in items"
      :key="item.id"
      :class="{ 'active': selectedItems.includes(item.id) }"
      @click="toggleSelection(item.id)"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  },
  methods: {
    toggleSelection(id) {
      const index = this.selectedItems.indexOf(id)
      if (index === -1) {
        this.selectedItems.push(id)
      } else {
        this.selectedItems.splice(index, 1)
      }
    }
  }
}
</script>

使用计算属性优化

对于复杂的高亮逻辑,可以使用计算属性返回类名或样式对象。

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

<script>
export default {
  data() {
    return {
      activeItem: null,
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  },
  computed: {
    getItemClass() {
      return (id) => ({
        'active': this.activeItem === id,
        'important': id === 1 // 额外条件示例
      })
    }
  }
}
</script>

组件化实现

将高亮逻辑封装为可复用组件,通过props和events与父组件通信。

vue实现点击高亮效果

<!-- HighlightableItem.vue -->
<template>
  <div 
    :class="{ 'active': isActive }"
    @click="$emit('click')"
  >
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: {
    isActive: Boolean
  }
}
</script>

<!-- ParentComponent.vue -->
<template>
  <div>
    <highlightable-item
      v-for="item in items"
      :key="item.id"
      :is-active="activeItem === item.id"
      @click="activeItem = item.id"
    >
      {{ item.text }}
    </highlightable-item>
  </div>
</template>

标签: 效果vue
分享给朋友:

相关文章

vue伪函数实现

vue伪函数实现

Vue 伪函数实现方法 伪函数(Mock Function)在 Vue 中常用于测试或模拟某些逻辑,以下是几种常见的实现方式: 使用 Jest 的 jest.fn() 在单元测试中,可以通过 Jes…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue源码实现

vue源码实现

Vue 源码实现解析 Vue.js 的核心实现可以分为响应式系统、虚拟 DOM、模板编译、组件化等几个关键部分。以下是对这些核心机制的详细解析。 响应式系统 Vue 的响应式系统基于 Object.…

vue广播实现

vue广播实现

vue广播实现 在Vue中实现广播功能可以通过事件总线(Event Bus)或Vuex来完成。以下是两种常见的实现方式: 使用事件总线(Event Bus) 事件总线是一种简单的跨组件通信方式,适用…

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式完成,具体取决于项目结构和需求。 使用 router-link 组件 router-link 是 Vue Router…