当前位置:首页 > VUE

vue实现li高亮效果

2026-02-22 04:02:48VUE

实现方法

在Vue中实现li高亮效果可以通过多种方式完成,以下是几种常见的方法:

使用动态class绑定

通过v-bind:class或简写:class动态绑定class,根据条件切换高亮样式。

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

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

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

使用内联样式绑定

通过v-bind:style或简写:style动态绑定内联样式。

<template>
  <ul>
    <li 
      v-for="(item, index) in items" 
      :key="index"
      :style="activeIndex === index ? highlightStyle : {}"
      @click="activeIndex = index"
    >
      {{ item }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      activeIndex: -1,
      highlightStyle: {
        backgroundColor: 'yellow',
        fontWeight: 'bold'
      }
    };
  }
};
</script>

使用计算属性

通过计算属性返回高亮样式或class,适合复杂逻辑。

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

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      activeIndex: -1
    };
  },
  methods: {
    getClass(index) {
      return {
        highlight: this.activeIndex === index
      };
    }
  }
};
</script>

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

使用Vuex管理状态

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

vue实现li高亮效果

<template>
  <ul>
    <li 
      v-for="(item, index) in items" 
      :key="index"
      :class="{ 'highlight': $store.state.activeIndex === index }"
      @click="$store.commit('setActiveIndex', index)"
    >
      {{ item }}
    </li>
  </ul>
</template>

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

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

注意事项

  • 确保v-for中的:key唯一且稳定。
  • 高亮样式可以根据需求自定义,如颜色、边框等。
  • 如果列表数据动态变化,需确保activeIndex同步更新。

以上方法均能实现li高亮效果,选择适合项目需求的方式即可。

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

相关文章

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue 实现弹窗

vue 实现弹窗

Vue 实现弹窗的方法 在 Vue 中实现弹窗功能可以通过多种方式完成,以下是几种常见的方法: 使用组件和 v-if/v-show 控制显示 创建一个独立的弹窗组件,通过 v-if 或 v-show…