修复多个Div的可拖动位置

发布于 2025-02-10 07:49:10 字数 1089 浏览 2 评论 0原文

在我的代码中,我想要Divs和Draggables元素动态,我的问题是滚动时,可拖动的元素正在沿Div的底部移动,当我将它们拖动到Div上时,我需要在滚动页面时不会移动。

代码 jsfiddle

  $(".signatureImage").on("mousedown mouseup", function(e) {

    }).draggable({
      containment: ".documents",
      cursor: "all-scroll",
      scroll: false, 
      //helper: "clone",
      grid: [5, 5],
      zIndex: 1000,
      drag: function(event,ui){
        var offset = $(this).offset();
        var xPos = offset.left;
        var yPos = offset.top;
      },
        stop: function(e, ui) {

          var finalxPos = ui.position.left;
          var finalyPos = ui.position.top;

          console.log('Stop: '+finalxPos + ' - '+finalyPos);
          jQuery.post("ajaxs/",
          {

            finalxPos:  finalxPos,
            finalyPos:  finalyPos,
          }, function(data)
          {});
        }
    }).each(function(i, item){    
    });

在DIV内拖动元素,然后使用containerdocuments div,scroll div,然后您会看到可拖动的元素随滚动而移动

in my code i want divs and draggables elements dynamically, my problem is that the draggable elements are moving along the bottom of the div when scrolling, and I need that when I drag them over the div they don't move when scrolling the page.

The code jsFiddle

  $(".signatureImage").on("mousedown mouseup", function(e) {

    }).draggable({
      containment: ".documents",
      cursor: "all-scroll",
      scroll: false, 
      //helper: "clone",
      grid: [5, 5],
      zIndex: 1000,
      drag: function(event,ui){
        var offset = $(this).offset();
        var xPos = offset.left;
        var yPos = offset.top;
      },
        stop: function(e, ui) {

          var finalxPos = ui.position.left;
          var finalyPos = ui.position.top;

          console.log('Stop: '+finalxPos + ' - '+finalyPos);
          jQuery.post("ajaxs/",
          {

            finalxPos:  finalxPos,
            finalyPos:  finalyPos,
          }, function(data)
          {});
        }
    }).each(function(i, item){    
    });

Drag the element inside the div and then use the scroll of the containerDocuments div, you will see that the draggable elements are moving along with the scroll

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿与i 2025-02-17 07:49:10

考虑以下示例。

小提琴:

” JavaScript

$(function() {
  $(".signatureImage").draggable({
    containment: ".documents",
    cursor: "all-scroll",
    scroll: false,
    grid: [5, 5],
    zIndex: 1000,
    stop: function(e, ui) {
      var finalxPos = ui.position.left;
      var finalyPos = ui.position.top;
      console.log('Stop: ' + finalxPos + ' - ' + finalyPos);
    }
  });

  $("#containerDocuments").droppable({
    drop: function(e, ui) {
      var el = ui.helper.detach();
      el.css({
        top: "",
        left: "",
        position: "absolute",
        "z-index": 1000
      }).appendTo(this).position({
        my: "center",
        at: "center",
        of: e
      });
    }
  });
});

使用可滴管,您可以分离可拖动元素并将其连接到Div。那是定位它的问题。您可以以多种不同的方式进行此操作,但我选择将其基于活动。

最初,使用您的代码,当您将其拖动并停止时,它得到了顶部&根据其父母的相对位置的左位置。当您删除它时,新位置必须相对于新的目标元素。您可以使用.offset()来完成此操作,但是它很复杂。

这使用户可以将元素从右侧拖到图像上,附加,现在在用户滚动时,图像将与DIV的其余部分一起滚动。

请参阅更多:

Consider the following example.

Fiddle: https://jsfiddle.net/Twisty/n5yrtou1/16/

JavaScript

$(function() {
  $(".signatureImage").draggable({
    containment: ".documents",
    cursor: "all-scroll",
    scroll: false,
    grid: [5, 5],
    zIndex: 1000,
    stop: function(e, ui) {
      var finalxPos = ui.position.left;
      var finalyPos = ui.position.top;
      console.log('Stop: ' + finalxPos + ' - ' + finalyPos);
    }
  });

  $("#containerDocuments").droppable({
    drop: function(e, ui) {
      var el = ui.helper.detach();
      el.css({
        top: "",
        left: "",
        position: "absolute",
        "z-index": 1000
      }).appendTo(this).position({
        my: "center",
        at: "center",
        of: e
      });
    }
  });
});

Using Droppable, you can detach the Draggable element and attach it to your DIV. It is then a matter of positioning it. You can do this in many different ways, yet I opted to base it on the Event.

Initially, with your code, when you dragged it and stopped, it was given a Top & Left position based on the relative position to it's parent. When you drop it, the new position has to be relative to the new target element. You can do this with .offset(), yet it's complicated.

This allows the user to drag an element from the right onto the images, it attaches, and now when the user scrolls, the image scrolls with the rest of the DIV.

See More:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文