当前位置:首页 > JavaScript

js实现图片旋转

2026-02-28 22:48:21JavaScript

使用CSS transform实现图片旋转

通过CSS的transform属性结合JavaScript动态修改旋转角度,实现图片旋转效果。以下是一个基础实现示例:

<img id="rotateImage" src="your-image.jpg" alt="Rotating Image">
<button onclick="rotateImage()">旋转图片</button>

<script>
  let currentRotation = 0;
  function rotateImage() {
    currentRotation += 90;
    document.getElementById('rotateImage').style.transform = `rotate(${currentRotation}deg)`;
  }
</script>

使用canvas实现精确旋转

对于需要更精确控制的旋转(如非90度倍数旋转),可以使用canvas API:

function rotateCanvasImage(imageElement, degrees) {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');

  // 调整canvas尺寸以适应旋转后的图像
  const rad = degrees * Math.PI / 180;
  canvas.width = Math.abs(imageElement.width * Math.cos(rad)) + 
                 Math.abs(imageElement.height * Math.sin(rad));
  canvas.height = Math.abs(imageElement.height * Math.cos(rad)) + 
                  Math.abs(imageElement.width * Math.sin(rad));

  // 执行旋转
  ctx.translate(canvas.width/2, canvas.height/2);
  ctx.rotate(rad);
  ctx.drawImage(imageElement, -imageElement.width/2, -imageElement.height/2);

  return canvas.toDataURL();
}

实现平滑动画旋转

使用CSS动画实现连续平滑旋转效果:

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.rotating {
  animation: spin 2s linear infinite;
}
// 添加/移除旋转类
document.getElementById('imageId').classList.toggle('rotating');

响应手势旋转(移动端)

通过触摸事件实现手势旋转:

let lastAngle = 0;
const image = document.getElementById('image');

image.addEventListener('touchmove', (e) => {
  if (e.touches.length === 2) {
    const touch1 = e.touches[0];
    const touch2 = e.touches[1];

    const currentAngle = Math.atan2(
      touch2.clientY - touch1.clientY,
      touch2.clientX - touch1.clientX
    ) * 180 / Math.PI;

    if (lastAngle !== 0) {
      const angleDiff = currentAngle - lastAngle;
      const currentRotation = parseInt(image.style.transform.replace(/[^0-9]/g, '') || '0');
      image.style.transform = `rotate(${currentRotation + angleDiff}deg)`;
    }

    lastAngle = currentAngle;
  }
});

image.addEventListener('touchend', () => {
  lastAngle = 0;
});

使用第三方库(如jQuery)

如果需要简化操作,可以使用jQuery实现:

js实现图片旋转

$('#rotateBtn').click(function() {
  let rotation = ($('#myImage').data('rotation') || 0) + 90;
  $('#myImage').css({
    'transform': 'rotate(' + rotation + 'deg)',
    '-webkit-transform': 'rotate(' + rotation + 'deg)',
    '-ms-transform': 'rotate(' + rotation + 'deg)'
  }).data('rotation', rotation);
});

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

相关文章

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScrip…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

vue图片实现多选

vue图片实现多选

Vue 图片多选实现方法 在 Vue 中实现图片多选功能可以通过以下几种方式完成,具体取决于项目需求和复杂度。 使用 v-model 和数组存储选中状态 通过 v-model 绑定一个数组来记录选中…