当前位置:首页 > 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直接动态修改元素的样式属性,适合简单的高亮效果。

vue实现点击高亮效果

<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>

多选高亮实现

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

vue实现点击高亮效果

<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与父组件通信。

<!-- 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中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…