当前位置:首页 > VUE

vue实现当前元素高亮

2026-01-22 15:57:07VUE

Vue 实现当前元素高亮的方法

使用动态 class 绑定

通过 v-bind:class 或简写 :class 动态切换高亮样式。定义一个变量存储当前高亮元素的索引或唯一标识,点击时更新该变量。

<template>
  <div>
    <div 
      v-for="(item, index) in items" 
      :key="index"
      :class="{ 'highlight': activeIndex === index }"
      @click="activeIndex = index"
    >
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      activeIndex: null
    };
  }
};
</script>

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

使用计算属性

对于复杂逻辑,可通过计算属性返回高亮状态。

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ],
      activeId: null
    };
  },
  methods: {
    setActiveItem(id) {
      this.activeId = id;
    }
  },
  computed: {
    getHighlightClass() {
      return (id) => ({ 'highlight': this.activeId === id });
    }
  }
};
</script>

使用 Vuex 管理状态

若项目使用 Vuex,可将高亮状态集中管理。

// store.js
export default new Vuex.Store({
  state: {
    activeItemId: null
  },
  mutations: {
    setActiveItem(state, id) {
      state.activeItemId = id;
    }
  }
});

组件中通过 mapStatemapMutations 使用:

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

<script>
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState(['activeItemId'])
  },
  methods: {
    ...mapMutations(['setActiveItem'])
  }
};
</script>

使用路由高亮

若高亮与路由相关,可通过 router-linkactive-class 实现。

vue实现当前元素高亮

<template>
  <div>
    <router-link 
      v-for="route in routes" 
      :key="route.path"
      :to="route.path"
      active-class="highlight"
    >
      {{ route.name }}
    </router-link>
  </div>
</template>

注意事项

  • 样式优先级:确保高亮样式的优先级高于默认样式。
  • 性能优化:列表较大时,避免频繁的 DOM 操作,可使用虚拟滚动。
  • 无障碍:为高亮元素添加 aria-current 属性提升可访问性。

标签: 元素vue
分享给朋友:

相关文章

vue表格实现教学

vue表格实现教学

Vue 表格实现方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签,结合v-for动态渲染数据: <template> <table&g…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template&…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…