当前位置:首页 > 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 中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法: 监听滚动事件并阻止默认行为 通过 @scroll 或 @wh…

vue实现视频开场

vue实现视频开场

使用 Vue 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依…

vue实现放大效果

vue实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

vue实现tab换行

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示与隐…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPa…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…