jQuery UI可拖动多个项目并转换:scale(0.5);

时间:2017-06-08 12:50:10

标签: jquery jquery-ui scale draggable

首先,我的母语不是英语。

所以有些事情很难理解。

我因此而花了两天时间。

我希望多个div可拖动"转换:缩放(0.5)"

这是正常的工作小提琴转换:比例(1) - > 点击here! JsFiddle1

但这不起作用小提琴变换:缩放(0.5) - > 点击here! JsFiddle2

var selectedObjs;
var pointerX;
var pointerY;

var draggableOptions = {
start: function(event, ui) {

     var iZoom = 0.5;
     pointerY = (event.pageY - $('#divCanvas').offset().top) / iZoom - parseInt($(event.target).css('top'));
     pointerX = (event.pageX - $('#divCanvas').offset().left) / iZoom - parseInt($(event.target).css('left'));

    //get all selected...
    if (ui.helper.hasClass('selected')) selectedObjs = $('div.selected');
    else {
        selectedObjs = $(ui.helper);
        $('div.selected').removeClass('selected')
    }
},
drag: function(event, ui) {
    var currentLoc = $(this).position();
    var prevLoc = $(this).data('prevLoc');
    if (!prevLoc) {
        prevLoc = ui.originalPosition;
    }

    var offsetLeft = currentLoc.left-prevLoc.left;
    var offsetTop = currentLoc.top-prevLoc.top;

    moveSelected(offsetLeft, offsetTop, event, ui);

    selectedObjs.each(function () {
        $(this).removeData('prevLoc');
    });

    $(this).data('prevLoc', currentLoc);

 }

};


$('.drag').draggable(draggableOptions).click(function(){
    $(this).toggleClass('selected')
 });


function moveSelected(ol, ot, evt, ui){
//console.log("moving to: " + ol + ":" + ot);
selectedObjs.each(function(){
    var iZoom = 0.5; //you can change this value
    $this =$(this);
    var p = $this.position();
    var l = p.left;
    var t = p.top;
    //console.log({id: $this.attr('id'), l: l, t: t});

    var canvasTop = $('#divCanvas').offset().top;
    var canvasLeft = $('#divCanvas').offset().left;

    if (selectedObjs.length == 1) {
          ui.position.left = Math.round((evt.pageX - canvasLeft) / iZoom - pointerX);
          ui.position.top = Math.round((evt.pageY - canvasTop) / iZoom - pointerY);
          ui.offset.left = Math.round($this.position().left + canvasLeft);
          ui.offset.top = Math.round($this.position().top + canvasTop);
    }
    else
    {
           $this.css('left', Math.round((l + ol) / iZoom));
         $this.css('top', Math.round((t + ot) / iZoom));
    }

})
}

可以修复吗?

否则,我希望你能以另一种方式给我。

0 个答案:

没有答案