2010-05-26 56 views
1

錯誤:jQuery的可拖動拋出時,「鼠標鬆開」如果你建立一個簡單的牽引被觸發

$(document).ready(function(){ 
    $('#tomove').draggable({ 
    axis: 'x', 
    drag: function(event, ui) { 
     mouseUp(); 
    } 
    }); 
}); 

,並嘗試以編程方式停止:

function mouseUp() { 
    if($('#tomove').offset().left > 400) { 
    $('#tomove').trigger('mouseup'); 
    } 
} 

您將得到錯誤控制檯此消息:

this.helper是空

有什麼辦法解決這個問題嗎? 感謝您的幫助。

+0

你有沒有得到這個成功解決?你還需要幫助嗎? – jcolebrand 2010-12-14 03:53:50

回答

0

看起來你只是想限制可拖動元素上的移動,這是正確的嗎?你見過此頁: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> 
+0

謝謝drachenstern,但這不是我想要實現的。 我只想在達到某個值時停止拖動的對象,但與約束無關。 謝謝你的建議。 – Moustard 2010-05-26 19:56:23

相關問題