当前位置:首页 > VUE

vue实现拖拽旋转

2026-01-17 18:15:37VUE

Vue 实现拖拽旋转

核心思路

通过监听鼠标事件计算旋转角度,结合 CSS 的 transform: rotate() 实现旋转效果。需要处理 mousedownmousemovemouseup 事件,并计算元素中心点与鼠标位置的夹角变化。

实现步骤

模板部分

<template>
  <div class="container">
    <div 
      class="rotatable-element"
      ref="element"
      :style="{ transform: `rotate(${angle}deg)` }"
      @mousedown="startRotate"
    ></div>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      angle: 0,
      startAngle: 0,
      isRotating: false,
      center: { x: 0, y: 0 }
    }
  },
  methods: {
    getElementCenter(el) {
      const rect = el.getBoundingClientRect()
      return {
        x: rect.left + rect.width / 2,
        y: rect.top + rect.height / 2
      }
    },
    calculateAngle(center, mouseX, mouseY) {
      return Math.atan2(mouseY - center.y, mouseX - center.x) * 180 / Math.PI
    },
    startRotate(e) {
      this.center = this.getElementCenter(this.$refs.element)
      this.startAngle = this.calculateAngle(this.center, e.clientX, e.clientY) - this.angle
      this.isRotating = true
      document.addEventListener('mousemove', this.rotate)
      document.addEventListener('mouseup', this.stopRotate)
    },
    rotate(e) {
      if (!this.isRotating) return
      const currentAngle = this.calculateAngle(this.center, e.clientX, e.clientY)
      this.angle = currentAngle - this.startAngle
    },
    stopRotate() {
      this.isRotating = false
      document.removeEventListener('mousemove', this.rotate)
      document.removeEventListener('mouseup', this.stopRotate)
    }
  }
}
</script>

样式部分

<style>
.rotatable-element {
  width: 100px;
  height: 100px;
  background-color: #42b983;
  cursor: grab;
  user-select: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform-origin: center;
}
.container {
  position: relative;
  height: 300px;
  border: 1px dashed #ccc;
}
</style>

优化方向

边界处理 添加角度限制逻辑,例如限制旋转范围为 0-360 度:

rotate(e) {
  if (!this.isRotating) return
  let currentAngle = this.calculateAngle(this.center, e.clientX, e.clientY)
  this.angle = (currentAngle - this.startAngle + 360) % 360
}

性能优化 使用 requestAnimationFrame 替代直接的事件处理:

rotate(e) {
  if (!this.isRotating) return
  requestAnimationFrame(() => {
    const currentAngle = this.calculateAngle(this.center, e.clientX, e.clientY)
    this.angle = currentAngle - this.startAngle
  })
}

触摸屏支持 添加触摸事件处理:

<div 
  @mousedown="startRotate"
  @touchstart="handleTouchStart"
></div>
handleTouchStart(e) {
  const touch = e.touches[0]
  this.startRotate({
    clientX: touch.clientX,
    clientY: touch.clientY
  })
}

vue实现拖拽旋转

标签: 拖拽vue
分享给朋友:

相关文章

vue如何实现冒泡

vue如何实现冒泡

Vue 实现冒泡排序 在 Vue 中实现冒泡排序,可以通过数据绑定和响应式特性动态展示排序过程。以下是一个完整的实现示例,包含代码和详细说明。 数据准备 在 Vue 的 data 中定义一个数组作…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm insta…

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:用…

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、…