简体   繁体   English

jQuery Sortable 拖动时得到 cursor 坐标

[英]jQuery Sortable get cursor coordinates while dragging

Is it possible to get the cursor coordinates while dragging with jQuery Sortable?是否可以在使用 jQuery Sortable 拖动时获得 cursor 坐标?

I have a scrollable div that has exact interval srolling.我有一个具有精确间隔滚动的可滚动 div。 I would like to scroll exactly 80px further down when the user is dragging an element close to the bottom of the scrolling div, or up if close to the top.当用户将元素拖动到靠近滚动 div 底部的位置时,我想向下滚动 80px,或者如果靠近顶部则向上滚动。

$(allIds).sortable({
    connectWith: ".allLists",
    zIndex: 2,
    cursorAt: function(e) {
        if(e.top < 80) {
            myScrollFunction('up');
        }
        if(e.top > 500) {
            myScrollFunction('down');
        }
    }
}).disableSelection();

The above code should hopefully give you an idea of what I'm trying to achieve.上面的代码应该能让您了解我正在努力实现的目标。 Maybe what I'm trying to achieve will conflict with the built in sortable scroll functionality?也许我想要实现的目标会与内置的可排序滚动功能发生冲突?

EDIT: I think cursorAt is the wrong option.编辑:我认为 cursorAt 是错误的选择。 I need a 'move' option to add a function to.我需要一个“移动”选项来添加 function。 Or something like this...或者像这样的东西......

$(allIds).sortable({
    connectWith: ".allLists",
    zIndex: 2
}, function(e) {
    if(e.position().top < 80) {
        myScrollFunction('up');
    }
    if(e.position().top > 500) {
        myScrollFunction('down');
    }
}).disableSelection();

However, if that worked it would probably only fire after the sortable event completes which is useless.但是,如果这样做有效,它可能只会在可排序事件完成触发,这是无用的。

To answer my own question, this is how I did it..要回答我自己的问题,我就是这样做的。

Created a mousemove event listener on the scrollable div separate to the sortable function在与可排序 function 分开的可滚动 div 上创建了一个 mousemove 事件侦听器

In the sortable function I used start: and stop: to toggle true or false of a global var在可排序的 function 中,我使用 start: 和 stop: 来切换全局变量的 true 或 false

Then whenever the mouse moves inside the scrollable div, it will check if dragging is true which triggers myScrollFunction().然后,每当鼠标在可滚动的 div 内移动时,它将检查拖动是否为真,从而触发 myScrollFunction()。

var dragging = false;

var dragScroll = function() {
    $("#scrollableDiv").mousemove(function(e) {
        if(dragging) {
            if((e.pageY-$(this).offset().top)<80) {
                myScrollFunction('up');
            }
            if((e.pageY-$(this).offset().top)>500) {
                myScrollFunction('down');
            }
        }
    });
};

$(allIds).sortable({
    start: function() { dragging = true; },
    stop: function() { dragging = false; },
    connectWith: ".allLists",
    zIndex: 2
}).disableSelection();

This fixes my issue however, now after the div scrolls, I can't drop the item in the other lists that come into view.但是,这解决了我的问题,现在在 div 滚动后,我无法将项目放到其他可见的列表中。 Anyway I'll check that out as a separate issue.无论如何,我会把它作为一个单独的问题来检查。

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

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