2010-08-24 97 views

回答

6

最佳的是隻在應之和構成遏制領域的選擇發送,但因爲這是行不通的我用的替代解決方案撥弄。我不會說這是非常優化的,但我會放棄它。

查找你應該拖動拖動withing和設置這些點作爲拖動的遏制細胞的極值點(左,上,右,下)。在我的例子中,我給出了這些元素(表中的tdcontainmentTD。它沒有考慮到邊界,填充等因素,所以如果這很重要的話就需要進行調整。

//The TD elements that are to be included when 
//we find the dimentions of the containment 
var td_elements = $(".containmentTD"); 

//This will hold the extreme points of the containment 
var points = { 
    left: td_elements.eq(0).position().left, 
    top: td_elements.eq(0).position().top, 
    right: 0, 
    bottom: 0 
}; 

//Find the points of the containment 
td_elements.each(function() { 
    var t = $(this); 
    var p = t.position(); 
    var width = t.width(); 
    var height = t.height(); 

    points.left = Math.min(p.left, points.left); 
    points.top = Math.min(p.top , points.top); 
    points.right = Math.max(points.right, p.left + width); 
    points.bottom = Math.max(points.bottom, p.top + height); 
}); 

//This will only work "perfectly" when all draggables have 
//the same dimentions 
points.bottom -= $("div.draggable:first").height(); 
points.right -= $("div.draggable:first").width(); 

$("div.draggable").draggable({ 
    containment: [points.left, points.top, points.right, points.bottom] 
}); 

這裏有一個演示:http://jsfiddle.net/uTyTY/注意,紅場僅存在以可視化的遏制區域