看起來你只是想限制可拖動元素上的移動,這是正確的嗎?你見過此頁:http://jqueryui.com/demos/draggable/#constrain-movement
編輯
這個怎麼樣,而不是那麼:(示例頁面,做你的要求 - 要留意的jQuery包含位置)
還要注意我改變了方法的名稱,使其更具針對性。這不會阻止用戶能夠拖回到左側。我不認爲如果他們達到400(或任何其他牆壁),他們就不想實際阻止他們。
如果你想這樣做,你只是$('#element').draggable('destroy')
<html>
<head>
<title>Draggable jQuery-UI Width Block</title>
<script src="jquery-1.4.2.min.js" ></script>
<script src="jquery-ui-1.8.1.custom.min.js" ></script>
</head>
<body>
<div id="tomove" style="width:100px;height:20px;background:silver;">
<span>some text</span>
</div>
<script>
$(document).ready(function() {
$('#tomove').draggable({
axis: 'x',
drag: function(event, ui) {
dragBlock(ui);
}
});
});
function dragBlock(ui) {
if(ui.position.left > 400) {
ui.position.left = '400px';
}
if(ui.position.left < 0) {
ui.position.left = '0px';
}
}
</script>
</body>
</html>
你有沒有得到這個成功解決?你還需要幫助嗎? – jcolebrand 2010-12-14 03:53:50