VUE2的ctrl多选和shift多选

template:
           <div v-for="(item, index) in fileList" :id="'f_' + item.scanIndex" :key="item.scanIndex" :class="{'active': selectList.includes(item.scanIndex)}" style="margin-left: 20%;margin-bottom: 10px;margin-top: 10px;float: left;" @click="showBigPic(item)">
                <img :id="'img' + item.scanIndex" :src="item.filePath" border="0" width="80" height="100" style="border: solid 1px #ccc;">
        
              </div>

css:

  .active {
    border: 2px solid blue;
  }

多选

 data() {
    return {
      scanIndex: 1,
      fileList: [], // 文件列表
      selectList: [], // 左侧选择列表
      keyCtrl: false, // 是否按下ctrl键
      keyShift: false, // 是否按下Shift键
      shiftIndex: '', // shift索引
      activeKey: ''

    }
  },
  mounted() {
    // 监听键盘
    window.addEventListener('keydown', this.handleCtrlDown, true)
    window.addEventListener('keyup', this.handleCtrlUp, true)
  },
  methods: {
    // 初始化页面
    init(type) {
      this.selectList = [] // 选择列表
    // 和后台要fileList

    },
     handleCtrlDown(e) { // 是否按下ctrl键
      const key = e.key
      switch (key) {
        case 'Control' :
          if (!this.keyCtrl) {
            this.keyCtrl = true
          }
          break
        case 'Shift' :
          if (!this.keyShift) {
            this.keyShift = true
          }
          break
      }
    },
    handleCtrlUp(e) { // 是否抬起ctrl键
      const key = e.key
      switch (key) {
        case 'Control' :
          if (this.keyCtrl) {
            this.keyCtrl = false
          }
          break
        case 'Shift' :
          if (this.keyShift) {
            this.keyShift = false
          }
          break
      }
    },
    showBigPic(item) {
      if (this.keyCtrl && !this.keyShift) {
        let deleteIndex = null
        for (let i = 0; i < this.selectList.length; i++) {
          if (this.selectList[i] === item.scanIndex) {
            deleteIndex = i
          }
        }
        this.$delete(this.selectList, deleteIndex)
        if (deleteIndex == null) {
          this.selectList.push(item.scanIndex)
        }
      } else if (!this.keyShift) {
        this.selectList = []
        this.selectList.push(item.scanIndex)
      } else if (this.keyShift) {
        if (this.shiftIndex === '' && this.selectList.length === 0) {
          this.shiftIndex = item.scanIndex
          this.selectList.push(item.scanIndex)
        } else {
          // 如果使用shift进行多选
          this.selectList = []
          for (let i = this.shiftIndex > item.scanIndex ? item.scanIndex : this.shiftIndex; i <= (this.shiftIndex > item.scanIndex ? this.shiftIndex : item.scanIndex); i++) {
            this.selectList.push(i)
          }
        }
      }
      this.activeKey = item.scanIndex
      if (!this.keyShift) { // 如果没有按下shift 更新索引
        this.shiftIndex = this.activeKey
      }
    }


  }

按住shift选择开始和结束及中间的,按住ctrl选择之前和现在的,

scanIndex是fileList中对象的索引

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值