1

ユーザーが画像を回転できるようにしようとしていますが、何があってもうまくいかないようです。画像のドラッグ/ドロップ/サイズ変更が完全に機能していますが、回転は正しく機能したくありません。

ご覧になり、これを修正する方法を教えてください。ありがとう。

http://jsfiddle.net/JDzWD/

コード:

$(function(){  
 //Make every clone image unique.  
   var counts = [0];
    var resizeOpts = { 
      handles: "all" ,autoHide:true
    };    
   $(".dragImg").draggable({
                         helper: "clone",
                         //Create counter
                         start: function() { counts[0]++; }
                        });

$("#dropHere").droppable({
       drop: function(e, ui){
               if(ui.draggable.hasClass("dragImg")) {
     $(this).append($(ui.helper).clone());
   //Pointing to the dragImg class in dropHere and add new class.
         $("#dropHere .dragImg").addClass("item-"+counts[0]);
             $("#dropHere .img").addClass("imgSize-"+counts[0]);

  $("#dropHere .img").addClass("mainTarget-"+counts[0]);
   $("#dropHere #rotate").attr("id", "target-"+counts[0]);
    $("#dropHere .imgSize").attr("id", "mainTarget-"+counts[0]);


   //Remove the current class (ui-draggable and dragImg)
         $("#dropHere .item-"+counts[0]).removeClass("dragImg ui-draggable ui-draggable-dragging");

$(".item-"+counts[0]).dblclick(function() {
$(this).remove();
});     
    make_draggable($(".item-"+counts[0])); 
      $(".imgSize-"+counts[0]).resizable(resizeOpts);  
       }

       }


      });


var zIndex = 0;
function make_draggable(elements)
{   
    elements.draggable({
        containment:'parent',
        start:function(e,ui){ ui.helper.css('z-index',++zIndex); },
        stop:function(e,ui){
        }
    });
}    



   });


//Allow image to be rotated.
var dragging = false;

$(function() {    
    var target = $('[id^=target-]');
    var mainTarget = $('[id^=mainTarget-]');
    var offset = mainTarget.offset();
    target.mousedown(function() {
        dragging = true
    })
    $(document).mouseup(function() {
        dragging = false
    })
    $(document).mousemove(function(e) {
        if (dragging) {

            var center_x = (offset.left) + (mainTarget.width()/2);
            var center_y = (offset.top) + (mainTarget.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) + 120;    
            mainTarget.css('-moz-transform', 'rotate(' + degree + 'deg)');
            mainTarget.css('-moz-transform-origin', '50% 50%');
            mainTarget.css('-webkit-transform', 'rotate(' + degree + 'deg)');
            mainTarget.css('-webkit-transform-origin', '50% 50%');
            mainTarget.css('-o-transform', 'rotate(' + degree + 'deg)');
            mainTarget.css('-o-transform-origin', '50% 50%');
            mainTarget.css('-ms-transform', 'rotate(' + degree + 'deg)');
            mainTarget.css('-ms-transform-origin', '50% 50%');
        }
    })
});

HTML:

 <div class="dragImg"><img class="img" src="http://www.thumbshots.com/Portals/0/Images/Feature%20TS%201.jpg">
     <span id="rotate">Rotate</span></img>
 </div>

CSS:

#dropHere {
    width:400px;
    height: 400px;
    border: dotted 1px black;

}
4

2 に答える 2

1

働くフィドル

ドロップ可能なハンドラーを次のように変更します

$("#dropHere").droppable({
    drop: function (e, ui) {
        if (ui.draggable.hasClass("dragImg")) {
            var the_div  = $(ui.helper).clone()
            $(this).append(the_div);

            //Pointing to the dragImg class in dropHere and add new class.
            the_div.addClass("item-" + counts[0]);
            the_div.find('img').addClass("imgSize-" + counts[0]);

            the_div.find('img').addClass("mainTarget-" + counts[0]);
            the_div.find('span').attr("id", "target-" + counts[0]);
            the_div.find('img').attr("id", "mainTarget-" + counts[0]);

            //Remove the current class (ui-draggable and dragImg)
            the_div.find('img').removeClass("dragImg ui-draggable ui-draggable-dragging");

            $(".item-" + counts[0]).dblclick(function () {
                $(this).remove();
            });
            make_draggable($(".item-" + counts[0]));
            $(".imgSize-" + counts[0]).resizable(resizeOpts);
        }

        $("#target-" + counts[0]).mousedown(function (e) {
            var item_target = $('.item-' + $(this).attr('id').replace('target-', ''));
            item_target.draggable('disable');
            item_target.removeClass("dragImg ui-draggable ui-draggable-dragging ui-state-disabled");
        });


    }


});

回転中のドラッグを無効にするために、ドロップ ハンドラーに追加します。また、コードを$(document).ready()以下に変更します

//Allow image to be rotated.
var dragging = false;
console.log($('[id^=target-]'))
var target = $('[id^=target-]');
var mainTarget = $('[id^=mainTarget-]');
var rotation_target;

$(document).mousemove(function (e) {
    if (dragging) {

        var mainTarget = $('.mainTarget-' + rotation_target.attr('id').replace('target-', '')).parent();

        var offset = mainTarget.offset();
        var center_x = (offset.left) + (mainTarget.width() / 2);
        var center_y = (offset.top) + (mainTarget.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) + 120;
        mainTarget.css('-moz-transform', 'rotate(' + degree + 'deg)');
        mainTarget.css('-moz-transform-origin', '50% 50%');
        mainTarget.css('-webkit-transform', 'rotate(' + degree + 'deg)');
        mainTarget.css('-webkit-transform-origin', '50% 50%');
        mainTarget.css('-o-transform', 'rotate(' + degree + 'deg)');
        mainTarget.css('-o-transform-origin', '50% 50%');
        mainTarget.css('-ms-transform', 'rotate(' + degree + 'deg)');
        mainTarget.css('-ms-transform-origin', '50% 50%');
    }
});
$(document).mouseup(function () {
    dragging = false
    $("[class^=item]").draggable('enable');
})
$(document).on('mousedown', '[id^=target-]', function () {
    dragging = true
    rotation_target = $(this);
})

このコードは、マウスダウン時に回転のターゲットを識別し、回転する画像を認識します。また、マウスが戻った後に再びドラッグできるようになるため、回転が終了した後に画像を移動できます

また、次の css を追加します。

img, span{
    -webkit-user-select: none; /* Chrome/Safari */        
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ */

/* Rules below not implemented in browsers yet */
-o-user-select: none;
user-select: none;
}

回転しようとしたときにテキストの強調表示を停止するには

于 2013-08-29T10:31:02.797 に答える
0

君の

target.mousedown(function() {
    dragging = true
})

が呼び出されるのが早すぎるため、要素がドロップ領域に作成される前に実行されます。使用する必要があります.on

$(document).on("mousedown","[id^=target-]",function() {
    dragging = true;
});

mainTarget要素がまだ作成されていないため、何も保持しません。マウスの移動を次のように変更します。

$(document).mousemove(function(e) {
    if (dragging) {
        var mainTarget = $("[id^=mainTarget-]");
        var offset = mainTarget.offset();
        var center_x = (offset.left) + (mainTarget.width()/2);

あなたのmainTarget IDも設定されていません

$("#dropHere .imgSize").attr("id", "mainTarget-"+counts[0]);

する必要があります

$("#dropHere img[class*=imgSize]").attr("id", "mainTarget-"+counts[0]);

.imgSize実際のクラスは imgSize-(somenumber) であるため機能しないため、使用*=imgSizeすると、単語が含まれるクラスの要素が選択さimgSizeれます

targetまた、画像の回転は、マウスの移動に合わせて移動するのと同様に、スパンからのマウスのオフセットが変更される可能性があまりないため、あまり回転しません。1 つの例外は、窓の端に近づいたときです。他のコードを使用して、回転する角度を決定するか、回転中のドラッグを削除することをお勧めします。

JSFiddleデモ

于 2013-08-29T09:53:14.317 に答える