简体   繁体   English

jQuery选择和拖动具有相对和绝对位置的元素

[英]jQuery Select and Drag elements with relative and absolute position

I have a select and drag code that works well until in selection is added a position relative div. 我有一个选择和拖动代码,在选择中添加了一个相对位置的div之前,它的效果很好。

Here is the code and you can see a working demo at http://jsbin.com/azeli/2 To see the problem just mouse select span 1 , span 2 and the nested span 4 这是代码,您可以在http://jsbin.com/azeli/2上看到一个有效的演示。要查看该问题,只需鼠标选择span 1span 2和嵌套span 4

$(function() {

  var selected = $([]), offset = {top:0, left:0};
  $("#selectable1").selectable();

    $("#selectable1 span.drag").draggable({

        start: function(ev, ui) {

        selected = $(".ui-selected").each(function() {
        var el = $(this);
        el.data("offset", el.offset());

        });

        offset = $(this).offset();

      },

      drag: function(ev, ui) {

        var dt = ui.position.top - offset.top, dl = ui.position.left - offset.left;

        selected.not(this).each(function() {
          var el = $(this), off = el.data("offset");
          el.css({top: off.top + dt, left: off.left + dl});
          });

      },  

  });
  });

Please let me know if this can be fixed. 请让我知道是否可以解决。 Thank you. 谢谢。

Make the position absolute for all elements within the selection, restore the original position when the selection changes. 为所选内容中的所有元素设置绝对位置,更改所选内容时恢复原始位置。

outside the loop 循环外

var originalPositions = new Array();

inside the loop 循环内

// call the restorePositions() that goes through the array of 
// saved elements, restoring each position and remove the element from the array;
// if element in the loop has a position thats not relative, add to array
el.css({position: 'absolute', top: off.top + dt, left: off.left + dl});

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

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