在jquery中模拟鼠标拖动

use*_*100 2 jquery jquery-ui

我有一小段代码,我想模拟div从一个点到另一个点的拖动..我使用jquery.simulate.js脚本,但我在控制台中得到错误说..."$ .ui.mouse ._mouseDown不是一个函数"

<html>
<head>

<style>
.box {
    background-color:#b0c4de;
}   

</style>

<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript" src="jquery.simulate.js"></script>

<script>

$(document).ready(function() {
    var myDiv = $("#myDiv");

    myDiv.draggable();

    // This will set enough properties to simulate valid mouse options.
    $.ui.mouse.options = $.ui.mouse.defaults;

    var divOffset = myDiv.offset();

    // This will simulate clicking down on the div - works mostly.
    $.ui.mouse._mouseDown({
            target: myDiv,
            pageX: divOffset.left,
            pageY: divOffset.top,
            which: 1,

            preventDefault: function() { }
    });
});

</script>


</head>

<body>
<div id="myDiv">hello</div> 

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

Ign*_*tor 9

如果你真的想模拟一个拖放(&drop),请使用jQuery simulate扩展插件:http:
//j-ulrich.github.com/jquery-simulate-ext

然后你可以使用

$('#myDiv').simulate("drag-n-drop", {dx: 50});
Run Code Online (Sandbox Code Playgroud)

将div 50px向右拖动.

免责声明:我是该插件的作者.

  • @Nakilon查看[插件文档](https://github.com/j-ulrich/jquery-simulate-ext/blob/master/doc/drag-n-drop.md):`$('# myDiv').模拟("drag-n-drop",{dragTarget:otherDiv});` (2认同)