当前位置:首页 > VUE

vue实现鼠标拖动多选

2026-01-21 20:57:09VUE

Vue 实现鼠标拖动多选框

基本思路

通过监听鼠标事件(mousedownmousemovemouseup)实现拖动选区,结合动态计算选中元素的范围完成多选功能。核心是记录起始位置和当前鼠标位置,生成选区矩形。

核心代码实现

模板部分

<template>
  <div 
    class="container"
    @mousedown="handleMouseDown"
    @mousemove="handleMouseMove"
    @mouseup="handleMouseUp"
  >
    <!-- 可选元素列表 -->
    <div 
      v-for="(item, index) in items" 
      :key="index"
      class="selectable-item"
      :class="{ 'selected': selectedItems.includes(index) }"
      :style="getItemStyle(index)"
    >
      {{ item }}
    </div>

    <!-- 拖拽选区框 -->
    <div 
      v-if="isSelecting" 
      class="selection-box"
      :style="selectionBoxStyle"
    />
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
      selectedItems: [],
      isSelecting: false,
      startPos: { x: 0, y: 0 },
      currentPos: { x: 0, y: 0 }
    }
  },
  computed: {
    selectionBoxStyle() {
      const left = Math.min(this.startPos.x, this.currentPos.x)
      const top = Math.min(this.startPos.y, this.currentPos.y)
      const width = Math.abs(this.currentPos.x - this.startPos.x)
      const height = Math.abs(this.currentPos.y - this.startPos.y)
      return {
        left: `${left}px`,
        top: `${top}px`,
        width: `${width}px`,
        height: `${height}px`
      }
    }
  },
  methods: {
    handleMouseDown(e) {
      this.isSelecting = true
      this.startPos = { x: e.clientX, y: e.clientY }
      this.currentPos = { x: e.clientX, y: e.clientY }
      this.selectedItems = [] // 开始新选择时清空已选
    },
    handleMouseMove(e) {
      if (!this.isSelecting) return
      this.currentPos = { x: e.clientX, y: e.clientY }
      this.updateSelectedItems()
    },
    handleMouseUp() {
      this.isSelecting = false
    },
    getItemStyle(index) {
      // 返回元素位置信息(需根据实际布局实现)
      return {
        position: 'absolute',
        left: `${(index % 3) * 100 + 10}px`,
        top: `${Math.floor(index / 3) * 100 + 10}px`
      }
    },
    updateSelectedItems() {
      const items = document.querySelectorAll('.selectable-item')
      this.selectedItems = []
      items.forEach((item, index) => {
        const rect = item.getBoundingClientRect()
        if (this.isInSelection(rect)) {
          this.selectedItems.push(index)
        }
      })
    },
    isInSelection(rect) {
      const selectionLeft = Math.min(this.startPos.x, this.currentPos.x)
      const selectionRight = Math.max(this.startPos.x, this.currentPos.x)
      const selectionTop = Math.min(this.startPos.y, this.currentPos.y)
      const selectionBottom = Math.max(this.startPos.y, this.currentPos.y)

      return (
        rect.left < selectionRight &&
        rect.right > selectionLeft &&
        rect.top < selectionBottom &&
        rect.bottom > selectionTop
      )
    }
  }
}
</script>

样式部分

<style scoped>
.container {
  position: relative;
  width: 100%;
  height: 500px;
  border: 1px solid #ddd;
  overflow: hidden;
}

.selectable-item {
  width: 80px;
  height: 80px;
  border: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.selected {
  background-color: #b3d4ff;
  border-color: #4a90e2;
}

.selection-box {
  position: absolute;
  background-color: rgba(74, 144, 226, 0.2);
  border: 1px dashed #4a90e2;
  pointer-events: none;
}
</style>

功能扩展建议

添加Shift键多选

handleMouseDown(e) {
  if (!e.shiftKey) {
    this.selectedItems = []
  }
  // 其余逻辑不变
}

性能优化 对于大量元素,使用虚拟滚动技术(如vue-virtual-scroller)避免全量DOM检测。

vue实现鼠标拖动多选

边界处理 添加对容器边界的检测,防止选区超出可视区域。

标签: 鼠标拖动
分享给朋友:

相关文章

vue 实现拖动

vue 实现拖动

实现拖动的核心方法 在Vue中实现拖动功能可以通过HTML5的拖放API或第三方库如vuedraggable。以下是两种常见方案的实现细节。 使用HTML5原生拖放API HTML5提供了dragg…

vue实现拖动

vue实现拖动

Vue 实现拖动的几种方法 使用 HTML5 原生拖放 API HTML5 提供了原生拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dro…

vue实现图片拖动

vue实现图片拖动

Vue 实现图片拖动的步骤 使用 HTML5 拖放 API 在 Vue 中实现图片拖动可以利用 HTML5 的拖放 API。通过 draggable 属性设置元素可拖动,监听 dragstart、dr…

vue怎么实现拖动

vue怎么实现拖动

Vue 实现拖动的常用方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现基础拖动功能。适用于简单场景。 <temp…

vue实现滚动鼠标加载

vue实现滚动鼠标加载

vue实现滚动鼠标加载的实现方法 使用Intersection Observer API Intersection Observer API可以监听元素是否进入视口,适合实现滚动加载。在Vue中可以通…

react如何禁用鼠标

react如何禁用鼠标

禁用鼠标事件的实现方法 在React中禁用鼠标事件可以通过多种方式实现,以下是几种常见方法: 阻止默认行为和冒泡 通过阻止事件的默认行为和冒泡来禁用鼠标交互: function handleCli…