简体   繁体   English

jQuery UI Sortable - 当页面可滚动时,Div不可拖动

[英]jQuery UI Sortable — Div is not draggable when the page is scrollable

In advance thanks for taking a look at my question. 提前谢谢你看看我的问题。

I am creating a website where i have a list of divs which are sortable on the Y-axis using jQuery UIs sortable. 我正在创建一个网站,其中我有一个div列表,可以在Y轴上使用jQuery UI进行排序。
Since i want this to run on mobile devices using touch i had to add a little hack to make sure that jQuery UI is usable( since it currently does not support touch events.). 由于我希望使用触摸在移动设备上运行,因此我必须添加一些黑客以确保jQuery UI可用(因为它目前不支持触摸事件。)。
The hack is called jQuery UI touch punch. 这个hack被称为jQuery UI touch punch。
Website: jQuery UI touch punch . 网站: jQuery UI触摸打孔
GitHub: jQuery UI touch punch . GitHub: jQuery UI触摸打孔

Now comes my problem. 现在来了我的问题。 Sometimes the list gets so big that the website will get scrollable and when the site is scrollable i cannot properly drag the items since when i try to drag a div it just scrolls the page. 有时列表变得如此之大,以至于网站将可滚动,当网站可滚动时,我无法正确拖动项目,因为当我尝试拖动div时,它只是滚动页面。 The only way i can drag it is when i double tap the item and then drag it. 我可以拖动它的唯一方法是当我双击该项目然后拖动它。

But this is really not what i want since it is really tedious to use and unnatural. 但这真的不是我想要的,因为使用和不自然真的很乏味。

The question now is, is there a way to disable the scrolling when trying to drag one of the items from the draggable set. 现在的问题是,有没有办法在尝试从可拖动集中拖动其中一个项时禁用滚动。 I tried adding overflow-y: hidden on tap or adding touch-action : none . 我尝试添加overflow-y: hidden点击或添加touch-action : none Unfortunately this didn't seem to work. 不幸的是,这似乎不起作用。

SUMMARY 摘要
What i have: I can currently drag and sort a List of Divs with a touch device using jQuery UI and jquery UI touch punch. 我拥有:我现在可以使用jQuery UI和jquery UI触摸打孔器,通过触摸设备拖动和排序Div of List。
The Problem: The list will get so big that the site is scrollable which disables the dragging with a single tap i need to double tap to drag the item. 问题:列表将变得如此之大,以至于网站可以滚动,只需点按一下即可禁用拖动,我需要双击拖动项目。
What i want: I want to be able to drag the items(without double tapping) even when i have a scrollbar. 我想要的是:即使我有滚动条,我也希望能够拖动项目(不需要双击)。

How could i realize such behaviour / with what should i start? 我怎么能意识到这种行为/我应该从什么开始? Any tips and solutions are appreciated. 任何提示和解决方案表示赞赏。


Last but not least here is my FIDDLE . 最后但并非最不重要的是这是我的FIDDLE

EDIT: 编辑:

I am using: 我在用:
IE 11 IE 11
jQuery version 1.11.1 jQuery版本1.11.1
jQuery-ui version 1.11.4 jQuery-ui版本1.11.4

Try either replacing (recommended) touch punch library (or in addition to it) with the following JS snippet and calling init() function on $(document).ready(); 尝试使用以下JS片段替换(推荐)触摸打孔库(或者除此之外)并在$(document).ready();上调用init()函数$(document).ready(); :

  • Note you can comment styles on #wrapper , they were set just for overflow testing. 请注意,您可以在#wrapper上注释样式,它们仅用于溢出测试。
  • Ideally you would leave some space out of draggable items in order to scroll without undesired dragging. 理想情况下,您可以从可拖动的项目中留出一些空间,以便滚动而不会产生不希望的拖动。

Snippet below: 下面的代码段:

$(document).ready(function() {
    init();
    $("#myContainer").sortable({
        //your code
    })
});

function touchHandler(event) {
    var touch = event.changedTouches[0];

    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent({
            touchstart: "mousedown",
            touchmove: "mousemove",
            touchend: "mouseup"
        }[event.type], true, true, window, 1,
        touch.screenX, touch.screenY,
        touch.clientX, touch.clientY, false,
        false, false, false, 0, null);

    touch.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

function init() {
    document.addEventListener("touchstart", touchHandler, true);
    document.addEventListener("touchmove", touchHandler, true);
    document.addEventListener("touchend", touchHandler, true);
    document.addEventListener("touchcancel", touchHandler, true);
}

---> Fiddle with snippet replacing touch punch <--- --->用拨片代替触摸打孔器<---

---> Fiddle with snippet + touch punch <--- --->小提琴片+触摸打孔器<---

(Both tested in mobile safari & chrome, drag is achieved on 1st tap) (两者均在手机游戏和镀铬测试中,在第一次点击时实现拖动)

Try adding these lines to your touch punch library js file. 尝试将这些行添加到触摸打卡库js文件中。

function simulateMouseEvent(event, simulatedType) {   
// Ignore multi-touch events


> if (event.originalEvent.touches.length > 1) {
>         return;
>     }
> 
>     var touch = event.originalEvent.changedTouches[0],
>         simulatedEvent = document.createEvent('MouseEvents');
> 
>     if ($(touch.target).is("select")) {
>         event.stopPropagation();
>     } else {
>         event.preventDefault();
>     }
`

Include the above lines. 包括以上几行。

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

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