当前位置:首页 > VUE

前端实现图片旋转vue

2026-02-23 09:53:03VUE

使用 CSS transform 实现图片旋转

在 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 动画关键帧实现无限旋转效果,通过类名切换控制动画启停。

<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 事件计算旋转角度,适合移动端场景。

前端实现图片旋转vue

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

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…