【问题标题】:Connecting a Draggable to Sortable causes helper element to jump将 Draggable 连接到 Sortable 会导致辅助元素跳转
【发布时间】:2011-05-03 22:49:16
【问题描述】:

我在页面上有一个 jQueryUI 可拖动对象和一个可排序对象。

将一个元素从可拖动元素拖到可排序元素会导致被拖动的元素跳到页面的左上角。

这是演示:http://jsfiddle.net/travisrussi/aBhDu/1/

复制:

  • 将“项目 5”从可拖动 div(顶部框)拖到可排序 div(底部框)

实际结果:


拖动的元素似乎从相对定位切换到绝对定位。拖拽的“li”切换自:

<li class="ui-state-default ui-draggable" style="position: relative; left: 52px; top: 9px;">Item 3</li>

到这里:

<li class="ui-state-default ui-draggable ui-sortable-helper" style="position: absolute; left: 67px; top: 91px; width: 80px; height: 20px;">Item 3</li>

当可拖动项被拖入可排序对象时。


这是来自 jQueryUI 1.8.12 的相关 sn-p(从第 3041 行开始):

//The element's absolute position on the page minus margins
this.offset = this.currentItem.offset();
this.offset = {
    top: this.offset.top - this.margins.top,
    left: this.offset.left - this.margins.left
};

// Only after we got the offset, we can change the helper's position to absolute
// TODO: Still need to figure out a way to make relative sorting possible
this.helper.css("position", "absolute");
this.cssPosition = this.helper.css("position");

$.extend(this.offset, {
    click: { //Where the click happened, relative to the element
        left: event.pageX - this.offset.left,
        top: event.pageY - this.offset.top
    },
    parent: this._getParentOffset(),
    relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
});

//Generate the original position
this.originalPosition = this._generatePosition(event);
this.originalPageX = event.pageX;
this.originalPageY = event.pageY;

是否有一些我没有使用的配置选项?

CSS 有问题吗?

或者这是 jqueryUI 中的错误?

【问题讨论】:

  • 你链接到正确的小提琴了吗?我在演示中看到的布局与您的屏幕截图中的不同。
  • @Andrew:感谢您指出这一点。链接已修复。
  • 看起来 JS 无法正常工作——仔细检查后,看起来脚本需要不同的 HTML?不确定。

标签: jquery jquery-ui drag-and-drop jquery-ui-sortable jquery-ui-draggable


【解决方案1】:

跳转的主要原因是可拖动的“助手”选项未设置为“克隆”。如果您使用克隆助手,跳跃问题就会消失。

如果您需要使用“原始”辅助设置,您仍然会遇到跳跃问题。一种可能的解决方案是使用自定义帮助程序选项,如下所示:jQueryUI Draggable Helper Option Help。这个想法是在创建助手时将位置从相对位置转换为绝对位置。

这是使用“克隆”助手的工作演示的链接:http://jsfiddle.net/travisrussi/aBhDu/

【讨论】:

    【解决方案2】:

    似乎一个自定义辅助函数解决了这个问题,像这样:

            $( ".draggable" ).draggable({
                connectToSortable: "#sortable",
                //helper: "original",
                revert: "invalid",
                helper: function() {
                    return $(this);
                }
            });
    

    【讨论】:

    • 此解决方案导致克隆在拖动时呈现为不可见。
    • 这对我的实例非常有效,有 2 个列表 - 一个克隆一个没有,都被拖到第三个可排序列表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多