当前位置:首页 > 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 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现popup

vue实现popup

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

vue分页实现

vue分页实现

实现Vue分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…

vue实现修改

vue实现修改

Vue 实现修改功能的方法 在 Vue 中实现修改功能通常涉及表单绑定、状态管理和 API 调用。以下是几种常见实现方式: 双向数据绑定实现表单修改 使用 v-model 指令绑定表单元素与组件数据…

vue实现回顶部

vue实现回顶部

实现方法一:使用 window.scrollTo 在Vue组件中,可以通过调用 window.scrollTo 方法平滑滚动到页面顶部。该方法接受一个配置对象,指定滚动行为为平滑(smooth)。…