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>

效果图

相关推荐
前端无涯5 分钟前
react组件(4)---高阶使用及闭坑指南
前端·react.js
OliverH-yishuihan14 分钟前
在 Windows 上安装 Linux
linux·运维·windows
Gomiko17 分钟前
JavaScript DOM 原生部分(五):事件绑定
开发语言·前端·javascript
出来吧皮卡丘20 分钟前
A2UI:让 AI Agent 自主构建用户界面的新范式
前端·人工智能·aigc
Jeking21721 分钟前
进阶流程图绘制工具 Unione Flow Editor-- 击破样式痛点:全维度自定义解决方案
前端·流程图·workflow·unione flow·flow editor·unione cloud
晴转多云54321 分钟前
关于Vite后台项目的打包优化(首屏加载)
前端
阿苟26 分钟前
nginx部署踩坑
前端·后端
小林攻城狮28 分钟前
pdfmake 生成平铺式水印:核心方法与优化
前端
search731 分钟前
前端设计:CRG 2--CDC检查
前端·芯片设计
松涛和鸣33 分钟前
DAY33 Linux Thread Synchronization and Mutual Exclusion
linux·运维·服务器·前端·数据结构·哈希算法