简体   繁体   English

使用jsPlumb调整元素大小并拖动

[英]Resize and Drag an Element using jsPlumb

I am working with the jsPlumb library to implement a simple interface where an element can be dragged from the toolbox and dropped onto the container. 我正在使用jsPlumb库来实现一个简单的接口,在该接口中可以将元素从工具箱中拖放到容器上。 Here, I have an element('partitiondrop') that needs to be re-sizable and draggable at the same time. 在这里,我有一个element('partitiondrop'),它需要同时调整大小和拖动。 But, the following code doesn't permit the partition to be resized. 但是,以下代码不允许调整分区大小。 Without the jsPlumb.draggable , the resize function works but once the element is dropped it can't be dragged. 如果没有jsPlumb.draggable ,则resize函数可以工作,但是一旦删除元素,就不能将其拖动。

drop: function (e, ui) {
    var dropElem = ui.draggable.attr('class');
    droppedElement = ui.helper.clone();
    ui.helper.remove();
    $(droppedElement).removeAttr("class");
    $(droppedElement).draggable({containment: "container"});
    jsPlumb.repaint(ui.helper);

    var newAgent = $('<div>').attr('id', i).addClass('partitiondrop');
    $(droppedElement).draggable({containment: "container"});

    newAgent.css({
        'top': e.pageY,
         'left': e.pageX
    });

    $('#container').append(newAgent);

    jsPlumb.draggable(newAgent, {
         containment: 'parent'     
    });

    $(newAgent).resizable({
         resize : function(event, ui) {
         jsPlumb.repaint(ui.helper);
         }
    });
}

CSS for partitiondrop 用于partitiondrop的CSS

.partitiondrop {
    border: 1px solid #346789;
    resize: both;
    overflow-x: hidden;
    overflow-y: hidden;
    ...
    z-index: 20;
    position: absolute;
    ...
    box-sizing: border-box;
}

Suggestions in this regard will be highly appreciated. 在这方面的建议将受到高度赞赏。

You can use the interact.js Javascript library for this. 您可以为此使用interact.js Javascript库。 It provides an extensive function library specially methods to perform resizing and dragging at the same time. 它提供了广泛的函数库,特别是同时执行大小调整和拖动的方法。

interact('.resize-drag')
  .draggable({
    onmove: window.dragMoveListener
  })
  .resizable({
    preserveAspectRatio: true,
    edges: { left: true, right: true, bottom: true, top: true }
  })
  .on('resizemove', function (event) {
    var target = event.target,
        x = (parseFloat(target.getAttribute('data-x')) || 0),
        y = (parseFloat(target.getAttribute('data-y')) || 0);
// update the element's style
target.style.width  = event.rect.width + 'px';
target.style.height = event.rect.height + 'px';

// translate when resizing from top or left edges
x += event.deltaRect.left;
y += event.deltaRect.top;

target.style.webkitTransform = target.style.transform =
    'translate(' + x + 'px,' + y + 'px)';

target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
target.textContent = Math.round(event.rect.width) + '×' + Math.round(event.rect.height);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM