如何以编程方式调用jQuery UI Draggable拖动开始? [英] How to programmatically invoke jQuery UI Draggable drag start?

查看:58
本文介绍了如何以编程方式调用jQuery UI Draggable拖动开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在鼠标处于向下位置且释放之前,我创建了一个新的jQuery元素. (按下鼠标后).

I create a new jQuery element after the mouse is in a down position and before it is released. (After mousedown).

我想使用jQuery UI以编程方式在新元素上触发dragging,以便它会随着鼠标移动自动开始拖动.我不想释放,然后再次单击鼠标.

I would like to programmatically trigger dragging on the new element using jQuery UI, so that it will automatically begin dragging with my mouse movement. I don't want to have to release and then click the mouse again.

我尝试了以下方法...

I have tried the following...

var element = $("<div />");
element.appendTo("body").draggable().trigger("mousedown");

...但是,这不起作用.

...however this does not work.

有人对如何做到这一点有什么建议吗?

Does anyone have any suggestions on how to accomplish this?

更新:在搜索了

UPDATE: After some searching the poster of this question has the identical problem. However the suggested solution, which boils down to...

$("body").on("mousedown", function(e) { 
  $("<div />").draggable().appendTo("body").trigger(e);
});

...不再在最新版本的jQuery和jQuery-UI中运行,而是生成了超出最大调用堆栈"错误.

...no longer works in the latest versions jQuery and jQuery-UI, and instead generates a Maximum Call Stack Exceeded error.

推荐答案

这完全是黑客行为,但似乎可以解决问题:

This is totally a hack, but it seems to do the trick:

  var myDraggable = $('#mydraggable').draggable();

  // Yeah... we're going to hack the widget
  var widget = myDraggable.data('ui-draggable');
  var clickEvent = null;

  myDraggable.click(function(event){
      if(!clickEvent){
        widget._mouseStart(event);
        clickEvent = event;
      }
      else {
        widget._mouseUp(event);
        clickEvent = null;
      }
    });

  $(document).mousemove(function(event){
    console.log(event);
    if(clickEvent){
      // We need to set this to our own clickEvent, otherwise
      // it won't position correctly.
      widget._mouseDownEvent = clickEvent;
      widget._mouseMove(event);
    }
  });

这是pl人

我的示例使用的是一个已经存在的元素,而不是创建一个元素,但它的工作原理类似.

My example uses an element that already exists instead of creating one, but it should work similarly.

这篇关于如何以编程方式调用jQuery UI Draggable拖动开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆