我正在尝试使用可拖动的jquery ui插件(http://jqueryui.com/demos/draggable/)向我的页面添加一个可拖动元素。目前我有这个:
$("#makeMeDraggable").draggable({ axis: "y"});
我已经尝试过:
$("#makeMeDraggable").draggable({ axis: "y", limit: {top: 10, bottom: 550}});
我需要的只是允许垂直拖动 - 但只能向上拖动(禁止向下拖动)。
答案 0 :(得分: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/