当前位置:首页 > 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 直接绑定样式对象

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

多选高亮实现

支持同时高亮多个列表项

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实现搜索

Vue 实现搜索功能 在 Vue 中实现搜索功能通常涉及数据绑定、事件监听和过滤逻辑。以下是几种常见的实现方式: 使用计算属性过滤列表 <template> <div>…

vue实现密码

vue实现密码

Vue 密码输入组件实现 基础密码输入框实现 使用 Vue 的 v-model 指令绑定数据,并通过 type="password" 设置输入类型为密码: <template> &…

vue实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要…