2009-07-09 44 views
1

我有一个奇怪的情况,我需要在用户开始拖动它时尽快修改可拖动元素的位置。所以,在可拖动元素的开始事件处理程序中,我试图设置位置。它不响应位置变化,除非 - 这很奇怪 - 我改变位置后会做一些javascript错误。下面是一个例子:如何在JQuery UI Draggable元素的启动事件处理程序中使css位置更改生效?


<html> 
<head> 
<title>Drag reposition test</title> 

<script type="text/javascript" src="js/css_browser_selector.js"></script> 
<script type="text/javascript" src="development-bundle/jquery-1.3.2.js"></script> 
<script type="text/javascript" src="development-bundle/ui/jquery-ui-1.7.1.custom.js"></script> <!-- Includes JQuery UI Draggable. --> 

<style type="text/css"> 
    #draggable { width: 150px; height: 150px; padding: 0.5em; } 
</style> 

<script type="text/javascript"> 

$(function() { 
    $("#initialdragger").draggable({  
     start: function(e, ui) { 
      $('#initialdragger').css('top', 400); 
      x = y; // Javascript error, which weirdly causes a redraw. 
     } 
    });  
}); 
</script> 

</head> 

<body> 

<div id="initialdragger" class="ui-widget-content" style="border-width: 1px; border-color: black; background-color: orange; width: 300px"> 
    <p>Drag me around</p>  
</div> 

</body> 
</html> 

我该如何合法地在这种情况下导致重绘? JQuery的hide()和show()不起作用,它们都不是these methods

回答

1

我想结合mouseDown事件会得到你想要

<script type="text/javascript"> 
    $(function() { 
     $("#initialdragger").draggable(); 
     $('#initialdragger').bind("mousedown", function(e) { 
      $(this).css('top', 400); 
     }); 
    }); 
</script> 
+0

这就是我需要什么。我也需要它只是第一次发生,所以在一次解绑呼叫中折腾了这一点。 – Jim 2009-07-09 19:29:29

相关问题