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>

效果图

相关推荐
网络安全-老纪30 分钟前
网络安全事件响应--应急响应(windows)
windows·安全·web安全
answerball1 小时前
🔥 Vue3响应式源码深度解剖:从Proxy魔法到依赖收集,手把手教你造轮子!🚀
前端·响应式设计·响应式编程
Slow菜鸟2 小时前
ES5 vs ES6:JavaScript 演进之路
前端·javascript·es6
小冯的编程学习之路2 小时前
【前端基础】:HTML
前端·css·前端框架·html·postman
Jiaberrr3 小时前
Vue 3 中搭建菜单权限配置界面的详细指南
前端·javascript·vue.js·elementui
懒大王95273 小时前
uniapp+Vue3 组件之间的传值方法
前端·javascript·uni-app
烛阴4 小时前
秒懂 JSON:JavaScript JSON 方法详解,让你轻松驾驭数据交互!
前端·javascript
拉不动的猪4 小时前
刷刷题31(vue实际项目问题)
前端·javascript·面试
zeijiershuai4 小时前
Ajax-入门、axios请求方式、async、await、Vue生命周期
前端·javascript·ajax
恋猫de小郭5 小时前
Flutter 小技巧之通过 MediaQuery 优化 App 性能
android·前端·flutter