在div上使用拖放和旋转

时间:2015-04-03 13:55:12

标签: javascript jquery html drag-and-drop rotation

你好我需要你放一个旋转以及拖放一个div ..我尝试了下面的代码,但只有当表单加载我能够拖放后才能被禁用。我可以在任何时候旋转,但无法拖放......请帮助!!

 <style>
  #mainTarget{
width:300px; 
height:200px; 
position:relative;
top:100px; 
left:25%
}

.mainTarget{position:absolute; width:300px; height:200px;}

#target{
position:absolute;
height:20px;
width:20px;
background:url(http://files.softicons.com/download/system-icons/human-o2-icons-by-oliver-scholtz/png/128x128/actions/object-rotate-left.png) no-repeat top center #ffffff;
background-size:100%;
cursor:pointer; 
z-index:1; 
top:0; 
right:0;    
}
 #target1{
position:absolute;
height:20px;
width:20px;
background:url(http://files.softicons.com/download/system-icons/human-o2-icons-by-oliver-scholtz/png/128x128/actions/object-rotate-left.png) no-repeat top center #ffffff;
background-size:100%;
cursor:pointer; 
z-index:1; 
bottom:0; 
left:0;    
}

  #mainTarget1{

width:320px; 
height:200px; 
position:relative;

}

  .mainTarget1{position:absolute; width:300px; height:200px;}
</style>
<!DOCTYPE html>
<html>
<head>

<meta charset=utf-8 />
<title>JS Bin</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">                            </script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

 <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
 <body>

    <p>degrees</p>
   <span> rotate</span>
  <input type="text" id="style" name="style" /> 
 <div id="mainTarget">  
 <div >
 <img src="http://myreaxns.com/wp-content/uploads/2013/03/priyanka-chopra-  image.jpg" width="300" class="mainTarget" />


<div id="target">&nbsp;</div></div>
<div id="mainTarget1">
<img src="http://myreaxns.com/wp-content/uploads/2013/03/priyanka-chopra-image.jpg" width="300" class="mainTarget1" />
<div id="target1">&nbsp;</div>

</div>

</div>

<script>
  var dragging = false;
  $(function() {
var target = $('#target');  
var target1 = $('#target1');
var mainTarget = $('#mainTarget');
var mainTarget1 = $('#mainTarget1');
var rad = mainTarget.width()/2;
var elOfs = mainTarget.offset();
var elPos = {
   x: elOfs.left,
   y: elOfs.top
};
target.mousedown(function() {
    $('#mainTarget').draggable({ disabled: true });
    dragging = true;
});
$(document).mouseup(function(a) {
var mPos = {
    x: a.pageX-elPos.x,
    y: a.pageY-elPos.y
  };
  var getAtan = Math.atan2(mPos.x-rad, mPos.y-rad);
  var getDeg = -getAtan/(Math.PI/180) + 135;  //135 = (180deg-45deg)


  $('span').text(getDeg);

    if (dragging) {
        mainTarget.css({transform: 'rotate(' + getDeg + 'deg)'});
    }
    $('#mainTarget').draggable({ disabled: true });
    dragging = false;
});

$(document).mousemove(function(e) {
  var mPos = {
    x: e.pageX-elPos.x,
    y: e.pageY-elPos.y
  };
  var getAtan = Math.atan2(mPos.x-rad, mPos.y-rad);
  var getDeg = -getAtan/(Math.PI/180) + 135;  //135 = (180deg-45deg)


  $('p').text(getDeg);
  var style_position = $("#mainTarget").attr("style");

        $("#style").val(style_position);
    if (dragging) {
        mainTarget.css({transform: 'rotate(' + getDeg + 'deg)'});
    }

});
$('#mainTarget').draggable();
$('#target1').mousemove(function() {
$('#mainTarget').draggable();
$("#mainTarget").attr("class","ui-draggable ui-draggable-handle ui-draggable-dragging");


});
$('#target1').mousedown(function() {

$('#mainTarget').draggable();
$("#mainTarget").attr("class","ui-draggable ui-draggable-handle ui-draggable-dragging");

});
$('#target1').mouseup(function() {

$("#mainTarget").attr("class","ui-draggable ui-draggable-handle ui-draggable-dragging");
$('#mainTarget').draggable();
 //dragging = true;
});

});

</script>

1 个答案:

答案 0 :(得分:1)

你最好重写js。重新开始并为图像定义两个容器 - 一个用于旋转,一个用于应用平移。还要确定你是否打算使用jQueryUI draggable,并坚持使用。 这是一个简单的示例(根本没有完善),只使用代码中的事件鼠标位置,没有jQuery UI,并结合了两个容器的想法:

 var dragging = false;
 var rotating = false;
 var target = $('#target');
 var target1 = $('#target1');
 var mainTarget = $('#mainTarget');
 var mainTarget1 = $('#mainTarget1');
 var rad = mainTarget.width() / 2;
 var elOfs = $('.mainTarget').offset();
 var elPos = {
      x: elOfs.left,
      y: elOfs.top
 }
 target.mousedown(function () {;
      dragging = false;
      rotating = true;
 });
 $(".mainTarget").mousedown(function (a) {
      dragging = true;
      rotating = false;
 });
 $(document).mouseup(function (a) {
      dragging = (dragging) ? false : dragging;
      rotating = (rotating) ? false : rotating;     
 });
 $(document).mousemove(function (e) {
      var mPos = {
          x: e.pageX - elPos.x,
          y: e.pageY - elPos.y
      };
      var getAtan = Math.atan2(mPos.x - rad, mPos.y - rad);
      var getDeg = -getAtan / (Math.PI / 180) + 135; //135 = (180deg-45deg)
      $('p').text(getDeg);
      var style_position = $("#mainTarget").attr("style");     
      $("#style").val(style_position);
      if (rotating) {
          $('#rotate').css({
              transform: 'rotate(' + getDeg + 'deg)'
          });
      }
      if (dragging) {
          mainTarget.css({
              transform: 'translate(' + mPos.x + 'px,' + mPos.y + 'px)'
          });
      }

 });

这是html:

<p>degrees</p> <span> rotate</span>

<input type="text" id="style" name="style" />
<div id="mainTarget">
    <div id="rotate">
        <img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg" width="300" class="mainTarget" />
        <div id="target">&nbsp;</div>
    </div>
</div>

和一个新的CSS规则:

#rotate{
    position:relative;
    width:300px;
    height:200px;
}

here is the fiddle