当前位置:首页 > VUE

vue实现鼠标拖动多选

2026-02-22 12:06:31VUE

vue实现鼠标拖动多选

实现鼠标拖动多选的核心思路

监听鼠标的mousedownmousemovemouseup事件,通过计算鼠标移动轨迹和位置,动态生成一个选择框。检测目标元素是否在选择框范围内,更新选中状态。

vue实现鼠标拖动多选

基础代码结构

<template>
  <div 
    class="container"
    @mousedown="startDrag"
    @mousemove="onDrag"
    @mouseup="endDrag"
  >
    <div 
      v-for="item in items" 
      :key="item.id"
      class="selectable-item"
      :class="{ selected: selectedItems.includes(item.id) }"
    >
      {{ item.name }}
    </div>
    <div 
      v-if="isDragging"
      class="selection-box"
      :style="selectionBoxStyle"
    />
  </div>
</template>

数据与事件处理

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        // 更多项目...
      ],
      selectedItems: [],
      isDragging: 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: {
    startDrag(e) {
      this.isDragging = true
      this.startPos = { x: e.clientX, y: e.clientY }
      this.currentPos = { x: e.clientX, y: e.clientY }
    },
    onDrag(e) {
      if (!this.isDragging) return
      this.currentPos = { x: e.clientX, y: e.clientY }
      this.updateSelection()
    },
    endDrag() {
      this.isDragging = false
    }
  }
}
</script>

碰撞检测实现

methods: {
  updateSelection() {
    const selectionBox = {
      left: Math.min(this.startPos.x, this.currentPos.x),
      top: Math.min(this.startPos.y, this.currentPos.y),
      right: Math.max(this.startPos.x, this.currentPos.x),
      bottom: Math.max(this.startPos.y, this.currentPos.y)
    }

    this.selectedItems = []
    const items = document.querySelectorAll('.selectable-item')

    items.forEach(item => {
      const rect = item.getBoundingClientRect()
      const isIntersecting = !(
        selectionBox.right < rect.left || 
        selectionBox.left > rect.right || 
        selectionBox.bottom < rect.top || 
        selectionBox.top > rect.bottom
      )

      if (isIntersecting) {
        const itemId = parseInt(item.dataset.id)
        this.selectedItems.push(itemId)
      }
    })
  }
}

样式优化

<style>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
}

.selectable-item {
  position: absolute;
  width: 100px;
  height: 100px;
  border: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.selectable-item.selected {
  background-color: rgba(0, 123, 255, 0.3);
  border-color: #007bff;
}

.selection-box {
  position: absolute;
  background-color: rgba(0, 123, 255, 0.1);
  border: 1px dashed #007bff;
  pointer-events: none;
}
</style>

性能优化建议

使用事件委托减少事件监听器数量,避免在mousemove中频繁操作DOM。可以添加防抖处理减少不必要的计算。

methods: {
  onDrag: _.throttle(function(e) {
    if (!this.isDragging) return
    this.currentPos = { x: e.clientX, y: e.clientY }
    this.updateSelection()
  }, 50)
}

完整组件示例

将上述代码整合为一个完整组件,确保所有功能协同工作。注意处理边界情况,如滚动容器、元素重叠等场景。

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

相关文章

vue怎么实现拖动组件

vue怎么实现拖动组件

Vue 拖动组件实现方法 使用 HTML5 原生拖放 API 在 Vue 中可以利用 HTML5 的拖放 API 实现基础拖动功能。通过 draggable 属性和相关事件(dragstart, dr…

js实现鼠标移动

js实现鼠标移动

监听鼠标移动事件 使用 mousemove 事件监听鼠标移动,通过回调函数获取鼠标坐标(clientX 和 clientY)。 document.addEventListener('mousem…

js实现拖动

js实现拖动

实现拖动的步骤 使用JavaScript实现拖动功能需要监听鼠标事件,并更新元素的位置。以下是实现的基本方法: 监听鼠标按下事件 为需要拖动的元素添加mousedown事件监听器。当鼠标按下时,记录…

js事件实现拖动

js事件实现拖动

使用原生JavaScript实现元素拖动 在JavaScript中实现元素拖动功能需要监听鼠标事件并更新元素位置。以下是实现的基本步骤: HTML结构 <div id="draggable"…

js实现拖动生成

js实现拖动生成

实现拖动生成的基本思路 拖动生成通常指通过鼠标拖拽操作动态创建或放置元素。在JavaScript中,可以通过监听鼠标事件(mousedown、mousemove、mouseup)结合DOM操作实现。…

jquery鼠标悬停

jquery鼠标悬停

jQuery鼠标悬停事件基础 jQuery提供了.hover()方法来处理鼠标悬停(hover)事件。该方法接受两个函数参数,分别对应鼠标进入和离开元素时的动作。 $(selector).hover…