当前位置:首页 > VUE

Vue实现列表选择变色

2026-02-21 23:04:11VUE

Vue实现列表选择变色的方法

使用v-bind动态绑定class

通过v-bind:class结合条件判断,为选中的列表项添加特定样式类。当点击列表项时,更新当前选中项的索引或ID,触发样式变化。

<template>
  <ul>
    <li 
      v-for="(item, index) in list" 
      :key="index"
      @click="selectedIndex = index"
      :class="{ 'active': selectedIndex === index }"
    >
      {{ item }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      list: ['Item 1', 'Item 2', 'Item 3'],
      selectedIndex: null
    }
  }
}
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

使用v-bind动态绑定style

直接通过v-bind:style动态修改内联样式,适合需要更灵活样式控制的场景。

<template>
  <ul>
    <li 
      v-for="(item, index) in list" 
      :key="index"
      @click="selectedIndex = index"
      :style="selectedIndex === index ? activeStyle : {}"
    >
      {{ item }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      list: ['Item 1', 'Item 2', 'Item 3'],
      selectedIndex: null,
      activeStyle: {
        backgroundColor: '#42b983',
        color: 'white'
      }
    }
  }
}
</script>

使用计算属性管理选中状态

对于复杂逻辑,可以使用计算属性返回样式对象或类名数组,保持模板简洁。

<template>
  <ul>
    <li 
      v-for="(item, index) in list" 
      :key="index"
      @click="selectedIndex = index"
      :class="getItemClass(index)"
    >
      {{ item }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      list: ['Item 1', 'Item 2', 'Item 3'],
      selectedIndex: null
    }
  },
  methods: {
    getItemClass(index) {
      return {
        'active': this.selectedIndex === index,
        'disabled': index === 0 // 示例:额外条件
      }
    }
  }
}
</script>

<style>
.active {
  background-color: #42b983;
}
.disabled {
  opacity: 0.5;
}
</style>

使用Vuex管理全局状态

当选中状态需要跨组件共享时,可以通过Vuex管理当前选中项。

Vue实现列表选择变色

// store.js
export default new Vuex.Store({
  state: {
    selectedItemId: null
  },
  mutations: {
    setSelectedItem(state, id) {
      state.selectedItemId = id
    }
  }
})
<template>
  <ul>
    <li 
      v-for="item in list" 
      :key="item.id"
      @click="$store.commit('setSelectedItem', item.id)"
      :class="{ 'active': $store.state.selectedItemId === item.id }"
    >
      {{ item.name }}
    </li>
  </ul>
</template>

注意事项

  • 确保为列表项添加唯一的key属性,帮助Vue高效更新DOM
  • 复杂项目建议将样式逻辑抽离到CSS文件中维护
  • 移动端项目可以考虑添加:active伪类提升点击反馈体验

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

相关文章

Vue实现lagger页面

Vue实现lagger页面

Vue实现懒加载页面的方法 懒加载(Lazy Loading)是一种优化技术,用于延迟加载非关键资源,从而提升页面初始加载速度。在Vue中可以通过以下方式实现懒加载: 路由懒加载 使用Vue Rou…

vue实现传输列表

vue实现传输列表

Vue 实现传输列表 在 Vue 中实现传输列表功能,通常涉及两个列表之间的数据传递,用户可以通过按钮或拖拽操作将项目从一个列表移动到另一个列表。以下是实现传输列表的几种方法。 使用组件间数据传递…

vue实现卡片列表

vue实现卡片列表

Vue 实现卡片列表 使用 v-for 动态渲染卡片列表 通过 Vue 的 v-for 指令可以动态渲染卡片列表,数据通常存储在组件的 data 或通过 API 获取。 <template&g…

vue实现目录列表

vue实现目录列表

实现目录列表的基本思路 在Vue中实现目录列表通常涉及动态渲染嵌套结构的数据,结合递归组件或v-for循环处理层级关系。以下是两种常见实现方式: 方法一:使用递归组件 递归组件适合处理未知深度的嵌套…

vue实现多选列表

vue实现多选列表

Vue 实现多选列表的方法 使用 v-model 绑定数组 在 Vue 中,可以利用 v-model 指令绑定一个数组来实现多选功能。当用户选择多个选项时,选中的值会自动添加到数组中。 <te…

vue实现列表轮播

vue实现列表轮播

实现列表轮播的基本思路 使用Vue实现列表轮播的核心在于动态更新列表数据,结合CSS动画或过渡效果实现平滑切换。通常有两种实现方式:基于CSS动画的无限循环轮播和基于Vue数据绑定的动态更新轮播。…