使用鼠标移动旋转div

8

我有以下代码来旋转一个div。通过在同一div的右上角的图像上按下鼠标事件。我希望div在鼠标抬起时旋转。逻辑上,我相信代码是正确的,但它只在点击后才能工作。而不是在鼠标抬起时停止旋转,当我点击其他项目时,旋转就会停止。我认为在鼠标按下后拖动时,浏览器试图拖动图像,但我需要帮助...提前感谢:)

fl_rotate: false,
rotdivs: function() {
    var pw;
    var oThis = this;
    $('.drop div img').mousedown(function(e) {
        oThis.destroyDragResize();
        oThis.fl_rotate = true;
        return;
    });
    $(document).mousemove(function(e) {
        if (oThis.fl_rotate) {
            var element = $(oThis.sDiv);
            oThis.rotateOnMouse(e, element);
        }
    });
    $(document).mouseup(function(e) {
        if (oThis.fl_rotate) {
            oThis.initDragResize();
            var ele = $(oThis.sDiv);
            ele.unbind('mousemove');
            ele.draggable({
                containment: 'parent'
            });
            ele = 0;
            oThis.fl_rotate = false;
        }
    });
},
rotateOnMouse: function(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)');
}​

2
你能否创建一个带有示例的 http://jsfiddle.net/? - Akarun
你是否已经发布了所有相关的代码? - polarblau
你能否使用console.log输出事件,以便确认它们是否在你预期的时候发生?根据你所写的内容,我理解第一个mousedown事件仅在单击后触发,随后第二次单击会激活mouseup事件,从而禁用旋转,因为fl_rotate被设置为true。然而,这听起来像是mouseup事件在mousedown事件之前触发了?! - Ekim
3个回答

2

2
我认为问题在于当本地拖动事件(拖动图像)触发时(在鼠标按下时),它阻止了鼠标松开事件的触发。因此,您只需要防止鼠标按下事件的默认操作即可。

这里有一个工作示例:

HTML:

<div class="drop">
    <div>
        <img src="http://www.belugerinstudios.com/image/picturethumbnail/FunnyCatFootballIcon.JPG"/>
    </div>
</div>​

Javascript:

$(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');
  });
});

演示:http://jsfiddle.net/rwBku/13/ 我使用了jQuery的命名空间事件,这样你可以解绑你想要的mousemove事件。
请注意,图片的旋转有些问题,但我真的没有仔细研究那个方法。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接