如何在JQuery UI Draggable元素的start事件处理程序中使css位置更改生效?

时间:2009-07-09 16:48:49

标签: javascript jquery events draggable redraw

我有一个奇怪的情况,我需要在用户开始拖动它时立即修改可拖动元素的位置。因此,在draggable元素的启动事件处理程序期间,我正在尝试设置位置。它没有响应位置变化,除非 - 这很奇怪 - 我做了一些事情,在我改变位置后导致javascript错误。这是一个例子:


<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 个答案:

答案 0 :(得分:1)

我认为绑定一个mousedown事件会得到你想要的东西

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