【问题标题】:CSS3 transform rotate using mouse positionCSS3变换使用鼠标位置旋转
【发布时间】:2013-07-23 13:28:16
【问题描述】:

我想使用 CSS3“transform:rotate”旋转一个 div 元素。 我想从鼠标位置获取度数,比如跟踪鼠标。

我怎样才能做到这一点?

谢谢!

【问题讨论】:

  • 使用 JavaScript 跟踪鼠标并更新样式。
  • 是的,但是如何将鼠标位置转换为旋转度数?

标签: javascript html css


【解决方案1】:

检查

http://jsfiddle.net/arunberti/fSgPf/1/

$(document).ready(function() {
  // the same as yours.
  function rotateOnMouse(e, pw) {
      var offset = pw.offset();
      var center_x = (offset.left) + ($(pw).width() / 2);
      var center_y = (offset.top) + ($(pw).height() / 2);
      var mouse_x = e.pageX;
      var mouse_y = e.pageY;
      var radians = Math.atan2(mouse_x - center_x, mouse_y - center_y);
      var degree = (radians * (180 / Math.PI) * -1) + 100;
      //            window.console.log("de="+degree+","+radians);
      $(pw).css('-moz-transform', 'rotate(' + degree + 'deg)');
      $(pw).css('-webkit-transform', 'rotate(' + degree + 'deg)');
      $(pw).css('-o-transform', 'rotate(' + degree + 'deg)');
      $(pw).css('-ms-transform', 'rotate(' + degree + 'deg)');
  }

  $('.drop div img').mousedown(function(e) {
    e.preventDefault(); // prevents the dragging of the image.
    $(document).bind('mousemove.rotateImg', function(e2) {
      rotateOnMouse(e2, $('.drop div img'));
    });
  });

  $(document).mouseup(function(e) {
    $(document).unbind('mousemove.rotateImg');
  });
});

【讨论】:

  • 我应该怎么做才能实现相同的效果,只需一次更改,在左下角的变换原点上旋转?
  • 也不需要前缀.css jQuery 开箱即用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
相关资源
最近更新 更多