2012-10-21 28 views
0

以此爲例。如何鎖定/凍結/臨時禁用jQuery-ui可拖動事件?

http://jsbin.com/ufarif/6/

我想從被拖動到左側,如果左側位置凍結元素小於80。但是,如果我返回false,那麼用戶無法在所有拖動元素(甚至到了右) 。

handle.draggable('disable');被完全忽略,直到stop事件被觸發。

returnreturn true不停止拖動。

請注意,我知道containment選項,但是,我的條件是動態的,並且containment不會將函數作爲參數。

回答

0

您可以覆蓋被拖動元素的位置。

這小提琴是位置壓倒一切的例子:http://jsfiddle.net/QvRjL/74/

這小提琴是你如何可以做檢查,如果拖動的元素是靠近你的容器的邊界的例子:http://jsfiddle.net/pPn3v/22/

$(document).mousemove(function(e){ 
    window.mouseXPos = e.pageX; 
    window.mouseYPos = e.pageY; 
}); 

$('[id^="drag-"]').each(function() { 
    $(this).draggable({ 
     opacity: 0.7, 
     cursorAt: { top: 15, left: 50 },   
     scroll: true, 
     stop: function(){}, 
     drag : function(e,ui){    
      //Force the helper position 
      ui.position.left = window.mouseXPos - $(this).draggable('option','cursorAt').left; 
      ui.position.top = window.mouseYPos- $(this).draggable('option','cursorAt').top; 
     }); 
}); 
+0

你是對的。我做了更多的現場演示,他們可能需要相同的功能,http://jsbin.com/ufarif/7/edit。 – Gajus