【问题标题】:jQuery: How Do I simulate Drag and Drop in Code?jQuery:如何在代码中模拟拖放?
【发布时间】:2010-11-13 01:37:42
【问题描述】:

编辑:这是一个向您展示我的示例代码的链接:http://www.singingeels.com/jqtest/

我有一个非常简单的页面,其中引用了 jquery-1.3.2.js、ui.core.js(最新版本)和 ui.draggable.js(也是最新版本)。

我有一个可以很容易拖动的 div(当然是使用鼠标):

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

然后在 JavaScript 中:

$("#myDiv").draggable();

这是完美的工作。但是,我需要能够单独使用代码来模拟“拖放”。 我大部分时间都在工作,但问题是触发的事件是占位符事件

如果你打开“ui.core.js”并滚动到底部......你会看到这个:

// These are placeholder methods, to be overriden by extending plugin
_mouseStart: function(event) { },
_mouseDrag: function(event) { },
_mouseStop: function(event) { },
_mouseCapture: function(event) { return true; }

为什么在我的模拟中事件没有被正确扩展,但是当你用鼠标点击时,它们是? - 关于如何强制 _mouseDrag: 属性服从“ui.draggable.js”中的覆盖扩展的任何想法?

解决这个问题将是巨大的 - 我计划稍后展示主要的好处。

谢谢, -蒂莫西

编辑:这是一个向您展示我的示例代码的链接:http://www.singingeels.com/jqtest/

编辑 2:单击上面的链接并查看源代码...你会看到我想要做什么。这是一个sn-p:

$(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() { }
    });
});

【问题讨论】:

  • 你能告诉我们你的代码吗?告诉我们什么不起作用以及您希望它如何起作用。
  • 你为什么不把所有的 JQueryUI 作为一个文件使用?
  • 我会尝试提供一个代码示例...分离 UI 的原因只是为了调试。
  • 如果您单击上面的示例链接并查看源代码,您就会明白我所说的“仅代码”是什么意思。
  • 你有没有得到这个排序?我正在尝试 mousedown() 以及 mousemove(eventData) 和 mouseup() 的循环,但效果不佳。

标签: jquery jquery-ui drag-and-drop draggable


【解决方案1】:

有一个question in the JQuery forum about it。在撰写本文时尚未解决,但将来可能会有更多信息。


编辑:论坛上已回答:

我建议您使用 jQuery UI 用于单元测试拖放的模拟插件:

https://github.com/jquery/jquery-ui/blob/master/external/jquery-simulate/jquery.simulate.js

您可以通过查看单元测试来查看使用示例

https://github.com/jquery/jquery-ui/blob/master/tests/unit/draggable/core.js

https://github.com/jquery/jquery-ui/blob/master/tests/unit/draggable/events.js

感谢 rdworth。

【讨论】:

    【解决方案2】:

    我找到了一个效果很好的解决方案。我让 drop 事件调用了一个名为 onDragAndDrop() 的函数。该函数有两个参数,draggable jQuery 对象和droppable jQuery 对象。

    $('.my-drop-target').droppable({
        drop: function(event, ui) {
            onDragAndDrop(ui.draggable, $(event.target));
        }
    });
    

    在我的测试中,我有一个直接调用 onDragAndDrop 的函数,但要确保使用鼠标的用户可以执行该操作。

        var simulateDragAndDrop = function(draggable, droppable) {
            if (!draggable.data('uiDraggable')) {
                throw new Error('Tried to drag and drop but the source element is not draggable!');
            }
            if (!droppable.data('uiDroppable')) {
                throw new Error('Tried to drag and drop but the target element is not droppable!');
            }
            onDragAndDrop(draggable, droppable);
        }
    

    我发现它是一个很好的、易于使用的单元测试解决方案。我可能最终也会将它用于键盘可访问的后备。

    【讨论】:

    • 这是onDragAndDrop的自定义函数?
    猜你喜欢
    • 2014-03-23
    • 1970-01-01
    • 2015-06-05
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    相关资源
    最近更新 更多