当前位置:首页 > 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 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contain…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDi…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(…