vue多选功能

废话不多说,直接上代码!!!

html 复制代码
<template>
  <div class="duo-xuan-page">
    <li
      v-for="(item, index) in list"
      :key="index"
      @click="toggleSelection(item)"
      :class="{ active: selectedItems.includes(item) }"
    >
      {{ item.tit }}
    </li>
  </div>
</template>
 
<script>
  export default {
    name: 'duo-xuan',
    data () {
      return {
        list: [
          {
            tit: '点我选择111'
          },
          {
            tit: '点我选择222'
          },
          {
            tit: '点我选择333'
          },
          {
            tit: '点我选择444'
          },
          {
            tit: '点我选择555'
          },
          {
            tit: '点我选择666'
          },
          {
            tit: '点我选择777'
          },
          {
            tit: '点我选择888'
          },
          {
            tit: '点我选择999'
          },
          {
            tit: '点我选择1010'
          }
        ],
        selectedItems: [] // 用于存储已选中项的每一项
      }
    },
    methods: {
      toggleSelection(item) {
        // 检查已选中项数组中是否包含当前点击项
        const selectedIndex = this.selectedItems.indexOf(item);
        if (selectedIndex === -1) {
          // 如果当前项未被选中,则把点击项添加到数组中
          this.selectedItems.push(item);
        } else {
          // 如果当前项已被选中,则从已选中项数组中移除
          this.selectedItems.splice(selectedIndex, 1);
        }
      }
    }
  }
</script>
 
<style lang="scss" scoped>
.duo-xuan-page {
  width: 100vw;
  height: 100vh;
  padding: 0 10px;
  box-sizing: border-box;
  overflow: auto;
  li {
    line-height: 100px;
    text-align: center;
    background-color: #ddd;
    list-style: none;
    margin: 20px 0;
    border-radius: 10px;
    font-size: 18px;
    font-weight: bold;
    &.active {
      border: 3px solid #67c23a;
      position: relative;
      &::before {
        content: '';
        position: absolute;
        top: 0px;
        right: 10px;
        width: 6px;
        height: 12px;
        border-bottom: 3px solid #fff;
        border-right: 3px solid #fff;
        transform: rotate(40deg);
        -webkit-transform-origin: left bottom;
        transform-origin: left bottom;
        z-index: 2;
      }
      &::after {
        content: "";
        position: absolute;
        top: 0px;
        right: 0px;
        width: 0;
        height: 0;
        border-left: 45px solid transparent;
        border-top: 45px solid #67c23a;
        z-index: 1;
      }
    }
  }
}
</style>

效果图

相关推荐
x***r15115 分钟前
R语言 4.5.1安装教程 Windows版:解压+管理员运行+自定义路径+启动验证指南
windows
小金鱼Y17 分钟前
别再乱拷贝了!JS 浅拷贝 vs 深拷贝全解析
前端·javascript
kyriewen20 分钟前
Sass 进阶:当 CSS 学会了编程,变量函数循环全都安排上
前端·css·scss
海带先森22 分钟前
python 虚拟环境的创建
前端
lovemiss27 分钟前
解码本质:claude code是怎么运行的
前端
yuxi202035 分钟前
Cursor 的 7 个隐藏功能,90% 的人不知道
前端
Moment36 分钟前
MiniMax 发布 M2.7,Agent 开始走向自我进化
前端·后端·面试
发现一只大呆瓜38 分钟前
Vue-Vue Router核心原理+实战用法全解析
前端·vue.js·面试
m0_694845571 小时前
Oh My Zsh 使用指南:Zsh 终端配置与插件管理教程
服务器·前端·小程序·开源·github
英俊潇洒美少年1 小时前
React19 useActionState的注意事项
前端·javascript·react.js