当前位置:首页 > 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>

脚本部分

vue实现拖拽旋转

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

vue实现拖拽旋转

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中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来…