当前位置:首页 > VUE

vue实现鼠标框选

2026-01-18 15:17:19VUE

Vue 实现鼠标框选功能

基本思路

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

核心实现步骤

模板部分

<template>
  <div 
    class="container"
    @mousedown="handleMouseDown"
    @mousemove="handleMouseMove"
    @mouseup="handleMouseUp"
  >
    <div 
      v-if="isSelecting"
      class="selection-box"
      :style="{
        left: selectionBox.left + 'px',
        top: selectionBox.top + 'px',
        width: selectionBox.width + 'px',
        height: selectionBox.height + 'px'
      }"
    ></div>

    <!-- 可选中的元素 -->
    <div 
      v-for="item in items" 
      :key="item.id"
      class="selectable-item"
      :class="{ selected: isItemSelected(item) }"
    >
      {{ item.name }}
    </div>
  </div>
</template>

脚本部分

export default {
  data() {
    return {
      isSelecting: false,
      startPos: { x: 0, y: 0 },
      currentPos: { x: 0, y: 0 },
      selectionBox: { left: 0, top: 0, width: 0, height: 0 },
      items: [
        { id: 1, name: 'Item 1', x: 10, y: 10, width: 100, height: 50 },
        // 更多元素...
      ],
      selectedItems: []
    }
  },
  methods: {
    handleMouseDown(e) {
      this.isSelecting = true
      this.startPos = { x: e.clientX, y: e.clientY }
      this.currentPos = { ...this.startPos }
      this.updateSelectionBox()
    },

    handleMouseMove(e) {
      if (!this.isSelecting) return
      this.currentPos = { x: e.clientX, y: e.clientY }
      this.updateSelectionBox()
      this.checkSelection()
    },

    handleMouseUp() {
      this.isSelecting = false
    },

    updateSelectionBox() {
      this.selectionBox = {
        left: Math.min(this.startPos.x, this.currentPos.x),
        top: Math.min(this.startPos.y, this.currentPos.y),
        width: Math.abs(this.currentPos.x - this.startPos.x),
        height: Math.abs(this.currentPos.y - this.startPos.y)
      }
    },

    checkSelection() {
      this.selectedItems = this.items.filter(item => {
        const itemRect = {
          left: item.x,
          right: item.x + item.width,
          top: item.y,
          bottom: item.y + item.height
        }

        const selectionRect = {
          left: this.selectionBox.left,
          right: this.selectionBox.left + this.selectionBox.width,
          top: this.selectionBox.top,
          bottom: this.selectionBox.top + this.selectionBox.height
        }

        return !(
          itemRect.right < selectionRect.left ||
          itemRect.left > selectionRect.right ||
          itemRect.bottom < selectionRect.top ||
          itemRect.top > selectionRect.bottom
        )
      })
    },

    isItemSelected(item) {
      return this.selectedItems.some(selected => selected.id === item.id)
    }
  }
}

样式部分

.container {
  position: relative;
  width: 100%;
  height: 100vh;
  background: #f5f5f5;
}

.selection-box {
  position: absolute;
  background: rgba(0, 120, 255, 0.2);
  border: 1px solid rgba(0, 120, 255, 0.8);
}

.selectable-item {
  position: absolute;
  border: 1px solid #ccc;
  padding: 10px;
  background: white;
}

.selectable-item.selected {
  background: #d4e6ff;
  border-color: #0078ff;
}

进阶优化方向

性能优化 对于大量元素场景,可采用空间分区算法(如四叉树)优化碰撞检测性能。实现虚拟滚动减少DOM数量。

交互增强 添加Shift键多选支持。实现拖拽选择后的批量操作。添加选择框的缩放控制点。

边界处理 考虑滚动容器的情况,需将鼠标坐标转换为容器相对坐标。处理元素重叠时的z-index问题。

指令封装 将核心逻辑封装为Vue指令,便于复用:

Vue.directive('selectable', {
  bind(el, binding) {
    // 实现选择逻辑
  }
})

以上实现提供了完整的鼠标框选功能基础,可根据实际需求进行调整和扩展。

vue实现鼠标框选

标签: 鼠标vue
分享给朋友:

相关文章

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return {…