当前位置:首页 > 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中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null } },…