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>

效果图

相关推荐
跟着珅聪学java8 分钟前
NPM镜像切换教程
vue.js
_码力全开_14 分钟前
第一章 html5 第一节 HTML5入门基础
前端·javascript·css·html·css3·html5
KLW7518 分钟前
vue 绑定动态样式
前端·javascript·vue.js
love530love20 分钟前
【笔记】ComfyUI 启动时端口被占用(PermissionError [winerror 10013])解决方案
人工智能·windows·笔记·stable diffusion·aigc·端口·comfyui
幼稚鬼_Blog29 分钟前
可拖拽悬浮按钮组件支持vue3/vue2
前端·javascript·html
Biehmltym32 分钟前
【AI】02实现AI Agent全栈:十分钟,跑通Python调用 Gemini(大模型)的小型Web项目
人工智能·windows·python
无限进步_35 分钟前
C++ Vector 全解析:从使用到深入理解
开发语言·c++·ide·windows·git·github·visual studio
前端小怪兽zmy40 分钟前
Vue3实现纯前端语音输入成文字显示
前端·javascript·elementui
ohyeah43 分钟前
原子化 CSS 与 Fragment:现代前端开发的高效实践
前端
鱼鱼块1 小时前
告别重复传参!用柯里化提升代码优雅度
前端·javascript·面试