2011-10-30 62 views
0

我試圖使用可拖動的jquery ui插件(http://jqueryui.com/demos/draggable/)將可拖動元素添加到我的頁面。目前,我有這樣的:jquery垂直向上動畫

$("#makeMeDraggable").draggable({ axis: "y"}); 

我已經嘗試過:

$("#makeMeDraggable").draggable({ axis: "y", limit: {top: 10, bottom: 550}}); 

我需要的是隻允許拖動垂直 - 但只有向上(如果不允許拖着向下)。

+0

什麼是你想在這裏做什麼?你是否希望元素只能在當前窗口空間內拖動,或者什麼?我還假設這是一個插件。你能鏈接到插件的頁面,所以我們知道它是哪一個? – Bojangles

+0

JamWaffles我試圖做的事情,詹姆斯發佈,即,只允許「垂直向上」拖動。 – Snapper

回答

1

試試這個代碼:

$("#makeMeDraggable").draggable({ axis: "y"}); 

var lastYPosition = null; 

$("#makeMeDraggable").draggable({ 
    drag: function(event, ui) { 

     // set with the initial y position 
     if(lastYPosition === null) { 
      lastYPosition = ui.originalPosition.top;   
     } 

     // don't do the drag if the new y position is larger than the old one 
     if(ui.position.top > lastYPosition) { 
      return false; 
     } 

     // update the last y position with current value 
     // so we can check against it next time 
     lastYPosition = ui.position.top; 
    } 
}); 

這裏是一個工作示例(在Chrome和Safari測試) http://jsfiddle.net/jameszaghini/s4vfX/

+0

完美詹姆斯。非常感謝你 :) – Snapper