当前位置:首页 > 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实现鼠标拖动多选

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

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

相关文章

jquery鼠标

jquery鼠标

jQuery 鼠标事件处理 jQuery 提供了丰富的鼠标事件处理方法,可以方便地响应用户的鼠标操作。以下是常见的鼠标事件及其用法: 鼠标点击事件 click(): 鼠标单击时触发 dblclic…

vue如何实现拖动

vue如何实现拖动

Vue 实现拖动的常用方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现拖动功能。 <template>…

jquery拖动

jquery拖动

jQuery 拖动实现方法 使用 jQuery 实现拖动功能可以通过原生 HTML5 的拖放 API 或借助 jQuery UI 的拖拽组件。以下是两种常见的方法: 使用 jQuery UI 的 D…

Vue实现鼠标悬浮

Vue实现鼠标悬浮

鼠标悬浮效果实现方法 在Vue中实现鼠标悬浮效果可以通过多种方式完成,以下是几种常见方法: 使用v-on指令绑定事件 通过@mouseenter和@mouseleave事件可以实现悬浮效果 <…

vue 实现拖动效果

vue 实现拖动效果

使用 Vue 实现拖动效果 Vue 提供了多种方式实现拖动效果,可以通过原生 HTML5 的拖放 API,也可以使用第三方库如 vuedraggable。以下是两种常见的方法: 使用 HTML5 拖…

vue实现鼠标框选

vue实现鼠标框选

Vue 实现鼠标框选功能 基本思路 鼠标框选功能通常需要监听鼠标的按下、移动和释放事件,记录起始点和当前位置,动态计算选区范围并高亮显示。在Vue中可以通过指令或组件封装实现。 核心实现步骤 模板部…