简体   繁体   English

如何在拖动时将JQuery UI Sortable项目放置在鼠标光标处?

[英]How do I position JQuery UI Sortable item at mouse cursor while dragging?

I'm using Jquery sortable to reorder items. 我正在使用可排序的Jquery重新排序项目。 In my GUI I have a traschan icon where sort items can be thrown away. 在我的GUI中,我有一个traschan图标,可以将排序项扔掉。 The trashcan is a droppable area. 垃圾桶是可丢弃的区域。

Now, when the sort item helper hovers over the droppable area I change it's size to give the user the visual feedback that it's possible to throw it away. 现在,当排序项目帮助程序悬停在可放置区域上时,我更改了它的大小,以便为用户提供可以将其丢弃的视觉反馈。 But I would also like to reposition the sort item helper so it aligns with the mouse cursor. 但我也想重新放置排序项助手,使其与鼠标光标对齐。

I tried: 我试过了:

$(trashcan)
    .droppable(
    {
        over: function (event, ui)
        {
            ui
                .draggable
                .css("transform", "scale(0.2)")
                .sortable("option", "cursorAt",
                {
                    left: 5,
                    top: 5
                });
        }
    });

But I only get the following js error: Error: cannot call methods on sortable prior to initialization; 但是我只得到以下js错误: 错误:无法在初始化之前调用可排序的方法; attempted to call method 'option' 试图调用方法“选项”

You're calling the option method on an element before initializing it as sortable. 在将元素初始化为可排序之前,您要在元素上调用option方法。

try 尝试

element.draggable()
    .sortable({ cursorAt: { left: 5, top: 5 } }); // pass the options while initializing

or if it is not possible for some reason, try 或者,如果由于某种原因而无法执行,请尝试

element.draggable()
    .sortable(); //initialize first

element.sortable("option", "cursorAt", // then call option method
    {
        left: 5,
        top: 5
    });

You can add the helper: 'clone' option to your sortable instance. 您可以将helper: 'clone'选项添加到可排序的实例中。 If, for whatever reason you cannot use clone, then the only way to fully solve this problem, and have the mouse cursor always be where you click on the item (instead of the item floating away), is this: 如果由于某种原因而无法使用克隆,则完全解决此问题并使鼠标光标始终位于单击项目的位置(而不是浮动的项目)的唯一方法是:

element
    .sortable({ ... })
    .find('.handle').mousedown(function(event) {
        element.sortable('option', 'cursorAt', {left:event.offsetX, top:event.offsetY});
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM