当前位置:首页 > jquery

jquery图片旋转

2026-03-16 10:24:57jquery

以下是使用jQuery实现图片旋转的几种方法,包括CSS动画和JavaScript控制:

使用CSS动画实现旋转

通过添加/移除CSS类实现旋转效果,性能较好:

jquery图片旋转

// CSS定义旋转动画
.rotate {
  animation: spin 2s linear infinite;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

// jQuery控制
$('#image').click(function() {
  $(this).toggleClass('rotate');
});

使用jQuery动画旋转

通过动态修改transform属性实现逐步旋转:

jquery图片旋转

let angle = 0;
$('#rotate-btn').click(function() {
  angle += 90;
  $('#image').css('transform', 'rotate(' + angle + 'deg)');
});

实现连续旋转动画

结合jQuery的animate()和step函数实现平滑旋转:

$('#image').hover(
  function() {
    $(this).stop().animate(
      { deg: 360 }, 
      {
        duration: 2000,
        step: function(now) {
          $(this).css('transform', 'rotate(' + now + 'deg)');
        }
      }
    );
  },
  function() {
    $(this).stop();
  }
);

拖拽旋转实现

结合mousemove事件实现拖拽旋转效果:

let isDragging = false;
let startAngle = 0;
let currentAngle = 0;

$('#image').mousedown(function(e) {
  isDragging = true;
  startAngle = getAngle(e.pageX, e.pageY);
});

$(document).mousemove(function(e) {
  if (!isDragging) return;
  const newAngle = getAngle(e.pageX, e.pageY);
  currentAngle += newAngle - startAngle;
  startAngle = newAngle;
  $('#image').css('transform', 'rotate(' + currentAngle + 'deg)');
});

$(document).mouseup(function() {
  isDragging = false;
});

function getAngle(x, y) {
  const rect = $('#image')[0].getBoundingClientRect();
  const centerX = rect.left + rect.width/2;
  const centerY = rect.top + rect.height/2;
  return Math.atan2(y - centerY, x - centerX) * 180 / Math.PI;
}

注意事项

  • 现代浏览器推荐使用CSS transforms代替jQuery animate以获得更好性能
  • 对于复杂旋转动画,建议使用GSAP等专业动画库
  • 旋转时注意设置transform-origin属性控制旋转中心点
  • 考虑添加will-change: transform提升动画性能

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

相关文章

jquery之家

jquery之家

jQuery之家资源推荐 jQuery之家(通常指提供jQuery相关资源的网站或社区)是开发者获取插件、教程和代码示例的重要平台。以下是几个与jQuery相关的优质资源网站: 1. jQuery官…

html jquery

html jquery

HTML 与 jQuery 基础 HTML 是超文本标记语言,用于构建网页的结构和内容。jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Aja…

jquery动画

jquery动画

jQuery动画基础 jQuery提供了多种动画方法,用于创建平滑的过渡效果。常用的动画方法包括animate()、fadeIn()、fadeOut()、slideUp()和slideDown()。…

jquery 库

jquery 库

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,适合快速开发前端…

jquery 高度

jquery 高度

jQuery 获取和设置元素高度的方法 使用 jQuery 可以方便地获取或设置元素的高度。以下是几种常用的方法: 获取元素高度 使用 height() 方法可以获取匹配元素中第一个元素的计算高度(…

H5图片实现地图

H5图片实现地图

实现H5图片地图的方法 在H5中实现图片地图功能,可以通过以下方式完成。图片地图允许用户在图片的特定区域点击,触发不同的交互效果。 使用HTML的<map>和<area>标签…