当前位置:首页 > VUE

前端实现图片旋转vue

2026-02-23 09:53:03VUE

使用 CSS transform 实现图片旋转

在 Vue 中可以通过绑定样式实现图片旋转效果。定义一个数据属性存储旋转角度,通过点击事件修改角度值。

前端实现图片旋转vue

<template>
  <div>
    <img 
      :style="{ transform: `rotate(${rotation}deg)` }" 
      src="your-image.jpg" 
      @click="rotateImage"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      rotation: 0
    }
  },
  methods: {
    rotateImage() {
      this.rotation += 90
    }
  }
}
</script>

使用 CSS 动画实现连续旋转

添加 CSS 动画关键帧实现无限旋转效果,通过类名切换控制动画启停。

前端实现图片旋转vue

<template>
  <div>
    <img 
      :class="{ 'rotate-animation': isRotating }" 
      src="your-image.jpg" 
      @click="toggleRotation"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      isRotating: false
    }
  },
  methods: {
    toggleRotation() {
      this.isRotating = !this.isRotating
    }
  }
}
</script>

<style>
.rotate-animation {
  animation: rotation 2s infinite linear;
}

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>

使用第三方库实现复杂旋转

安装 vue-draggable-resizable 等库可实现带拖拽功能的旋转控制。

npm install vue-draggable-resizable
<template>
  <div>
    <vue-draggable-resizable 
      :rotatable="true" 
      :w="200" 
      :h="200"
    >
      <img src="your-image.jpg" />
    </vue-draggable-resizable>
  </div>
</template>

<script>
import VueDraggableResizable from 'vue-draggable-resizable'

export default {
  components: {
    VueDraggableResizable
  }
}
</script>

实现手势旋转功能

通过监听 touch 事件计算旋转角度,适合移动端场景。

<template>
  <div 
    @touchstart="handleTouchStart" 
    @touchmove="handleTouchMove" 
    @touchend="handleTouchEnd"
  >
    <img 
      :style="{ transform: `rotate(${rotation}deg)` }" 
      src="your-image.jpg"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      rotation: 0,
      startAngle: 0,
      isRotating: false
    }
  },
  methods: {
    calculateAngle(x, y) {
      return Math.atan2(y, x) * 180 / Math.PI
    },
    handleTouchStart(e) {
      const rect = e.target.getBoundingClientRect()
      const centerX = rect.left + rect.width / 2
      const centerY = rect.top + rect.height / 2
      const touchX = e.touches[0].clientX
      const touchY = e.touches[0].clientY
      this.startAngle = this.calculateAngle(touchX - centerX, touchY - centerY) - this.rotation
      this.isRotating = true
    },
    handleTouchMove(e) {
      if (!this.isRotating) return
      const rect = e.target.getBoundingClientRect()
      const centerX = rect.left + rect.width / 2
      const centerY = rect.top + rect.height / 2
      const touchX = e.touches[0].clientX
      const touchY = e.touches[0].clientY
      const angle = this.calculateAngle(touchX - centerX, touchY - centerY)
      this.rotation = angle - this.startAngle
    },
    handleTouchEnd() {
      this.isRotating = false
    }
  }
}
</script>

标签: 图片vue
分享给朋友:

相关文章

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue 实现pdf

vue 实现pdf

在Vue中实现PDF功能 在Vue项目中实现PDF功能通常涉及PDF生成、预览或下载。以下是几种常见实现方式: 使用vue-pdf库预览PDF 安装依赖: npm install vue-pdf…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…

vue实现分站

vue实现分站

Vue 实现分站的方案 在 Vue 中实现分站功能通常涉及路由配置、环境变量管理和动态加载资源。以下是几种常见的实现方式: 基于路由的分站实现 通过 Vue Router 配置多路由,区分不同站点的…