4

我在頁面上有一個jQueryUI可拖動對象和一個可排序對象。將Draggable連接到Sortable會導致幫助器元素跳轉

將元素從可拖動元素拖到可排序元素會導致拖動元素跳轉到頁面左上角

這裏的演示:http://jsfiddle.net/travisrussi/aBhDu/1/

重現:

  • 將 '項目5' 從可拖動DIV(機頂盒)的排序DIV(底盒)

實際結果:


似乎從相對於拖元件切換到絕對定位。拖動「禮」切換來自:

<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相關片段(開始於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中的錯誤?

+1

你鏈接到正確的小提琴?我在演示中看到了與截屏中不同的佈局。 – 2011-05-03 23:01:30

+0

@Andrew:謝謝你指出。鏈接已被修復。 – 2011-05-03 23:16:11

+1

看起來像JS不工作 - 仔細觀察它看起來像腳本期待不同的HTML?不確定。 – 2011-05-04 00:20:57

回答

4

跳轉的主要原因是可拖動的'helper'選項未設置爲'clone'。如果你使用克隆助手,跳躍問題就會消失。

如果您需要使用「原始」輔助設置,您仍然會遇到跳躍問題。一種可能的解決方案可能是使用自定義幫助程序選項,如下所示:jQueryUI Draggable Helper Option Help。這個想法是在助手創建時將位置從相對位置轉換爲絕對位置。

下面是一個使用「克隆」幫手的工作演示的鏈接:http://jsfiddle.net/travisrussi/aBhDu/

3

這似乎是一個自定義的輔助功能解決了這個問題,像這樣:

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

此解決方案會導致克隆在拖動時呈現不可見狀態。 – 2012-03-06 22:05:25

+0

這對我的實例非常有用,有2個列表 - 一個被克隆,一個不被列入第三個可排序列表。 – 2012-10-17 16:18:48