当前位置:首页 > VUE

vue实现鼠标拖动多选

2026-01-21 20:57:09VUE

Vue 实现鼠标拖动多选框

基本思路

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

核心代码实现

模板部分

vue实现鼠标拖动多选

<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>

脚本部分

vue实现鼠标拖动多选

<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 实现拖动的几种方法 使用 HTML5 原生拖放 API HTML5 提供了原生拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dro…

js实现图片拖动

js实现图片拖动

实现图片拖动的步骤 使用JavaScript实现图片拖动功能,可以通过HTML5的拖放API结合事件监听实现。以下是具体方法: 设置HTML结构 为图片添加draggable属性,并设置唯一ID以便…

jquery鼠标事件

jquery鼠标事件

jQuery鼠标事件概述 jQuery提供了一系列鼠标事件处理方法,用于响应用户的鼠标操作,如点击、悬停、移动等。这些事件通过简洁的语法绑定到DOM元素,实现交互功能。 常用鼠标事件方法 cl…

Vue实现鼠标拖拽滚动

Vue实现鼠标拖拽滚动

Vue 实现鼠标拖拽滚动的方法 使用原生事件监听实现基础拖拽滚动 在Vue组件中,通过@mousedown、@mousemove和@mouseup事件监听实现拖拽滚动效果。需要记录鼠标按下时的初始位置…

vue实现拖动放大缩小

vue实现拖动放大缩小

实现拖动放大缩小的基本思路 在Vue中实现元素的拖动和放大缩小功能,通常需要结合鼠标事件和CSS变换。通过监听鼠标的按下、移动和释放事件,计算元素的位移和缩放比例,最终应用到元素的transform属…

vue实现轮播图拖动

vue实现轮播图拖动

Vue 实现轮播图拖动功能 实现轮播图拖动功能需要结合 Vue 的响应式数据和触摸事件(或鼠标事件)。以下是具体实现方法: 基础轮播图结构 使用 Vue 的 v-for 渲染轮播项,并通过 CSS…