当前位置:首页 > VUE

vue实现li高亮效果

2026-02-22 04:02:48VUE

实现方法

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

使用动态class绑定

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

vue实现li高亮效果

<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动态绑定内联样式。

vue实现li高亮效果

<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,可以将高亮状态集中管理。

<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实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引入…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…