当前位置:首页 > VUE

vue实现列表高亮

2026-01-19 03:37:21VUE

Vue 列表高亮实现方法

动态类绑定

通过 v-bind:class 或简写 :class 实现条件高亮

<template>
  <ul>
    <li 
      v-for="(item, index) in list" 
      :key="index"
      :class="{ 'highlight': item.isActive }"
      @click="toggleHighlight(index)"
    >
      {{ item.text }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { text: 'Item 1', isActive: false },
        { text: 'Item 2', isActive: false }
      ]
    }
  },
  methods: {
    toggleHighlight(index) {
      this.list.forEach((item, i) => {
        item.isActive = i === index
      })
    }
  }
}
</script>

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

行内样式绑定

使用 :style 直接绑定样式对象

vue实现列表高亮

<li 
  v-for="(item, index) in list" 
  :key="index"
  :style="item.isActive ? activeStyle : {}"
>
  {{ item.text }}
</li>

<script>
export default {
  data() {
    return {
      activeStyle: {
        backgroundColor: '#ffeb3b',
        fontWeight: 'bold'
      }
    }
  }
}
</script>

多选高亮实现

支持同时高亮多个列表项

vue实现列表高亮

methods: {
  toggleHighlight(index) {
    this.list[index].isActive = !this.list[index].isActive
  }
}

高亮动画效果

添加过渡动画增强用户体验

.highlight {
  transition: all 0.3s ease;
  transform: scale(1.02);
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

基于路由的高亮

根据当前路由高亮对应导航项

computed: {
  activeIndex() {
    return this.list.findIndex(item => 
      item.path === this.$route.path
    )
  }
}

标签: 列表vue
分享给朋友:

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…