关于 javascript:jquery ui 拖放在移动设备上不起作用 | 珊瑚贝

jquery ui drag and drop not work on mobile


我正在尝试使用 Phonegap、JQuery UI、JQuery mobile 设计一个应用程序,我想拖放,但我不知道为什么我的代码不能在移动设备上运行。它在浏览器上运行良好,但当我在移动设备上运行时,它无法正常工作。

这是我的代码

HTML 代码:

1
2
3
4
5
 <span id=“a1” class=“cartridge”><img src=“img/app2.png”/></span>
   <span  id=“a2” class=“cartridge”><img src=“img/app2.png” /></span>
     <span  id=“a3” class=“cartridge”><img src=“img/app2.png” /></span>
       <span  id=“a4” class=“cartridge”><img src=“img/app2.png” /></span>
        <span  id=“a5” class=“cartridge”><img src=“img/app2.png” /></span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 <table width=“100%” border=“1” cellspacing=“0” cellpadding=“5”>
<tr bgcolor=“#F2F2F2”>
<td width=“50%”  align=“center” class=“x”>
<p>coulm1</p>
</td>
<td width=“50%”  align=“center” class=“x”>
 <p>coulm2</p>
 </td>
  </tr>
  <tr>
  <td width=“50%” align=“center” id=“Td1”  class=“y” background=“img/1.png”>    
  </td>
  <td width=“50%”  align=“center” id=“Td2”  class=“y” background=“img/4.png”>
  </td>
   </tr>
  </  table>

我需要把这些放在这个表中:

现在我使用 darg 并按类删除这里是 javascript 代码:
我使用 jquery ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  $(function () {
        $(“.cartridge”).draggable({
            cursor: ‘move’,
            helper: ‘clone’,
            revert: ‘invalid’,

        });

        $(“.y”).droppable({
            accept: ‘.cartridge’,
            drop: function (event, ui) {
                 $(ui.draggable).appendTo(this);
                 checkwinner();
            }
        });

    });

它适用于浏览器,但适用于移动设备。
谢谢


我推荐的库是 https://github.com/yeco/jquery-ui-touch-punch ,这样你从 Jquery UI 中的拖放应该可以在触摸设备上工作

您可以使用我正在使用的这段代码,它还将鼠标事件转换为触摸,它就像魔术一样工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
}

然后在你的 document.ready 中调用 init() 函数。

  • 这适用于可排序但现在简单的”点击”触摸事件不会注册。有什么想法吗?
  • 上面的脚本在我的代码中不起作用。我已经实现并测试了它,但仍然无法使用可拖动功能。
  • 不是最好的解决方案:如果要拖动的元素占据整个视口,使用 jQuery UI Touch Punch 会阻止文档滚动。甚至可能是您希望仅水平拖动元素且容器隐藏溢出的问题,作为可拖动的轮播。在这种情况下,轮播会阻止用户上下滚动文档。我更喜欢 jQuery Touch (github.com/ajlkn/jquery.touch),它使得可拖动可能仅在移动设备上触摸(鼠标手势可以被过滤)并且不会阻止触摸事件传播(因此,要滚动的文档)。
  • 当 UI Touch Punch 不起作用时,这对我有用,老实说,这似乎是一些黑暗魔法。
  • Touch Punch 弃用了,上次更新 9 年前,网站挂了
  • MouseEvent.initMouseEvent() 已弃用。在 \\’22 及以后使用此代码不是一个好主意。


我喜欢这个开源项目 https://interactjs.io/ 并防止在 css 中使用页面滚动

1
2
3
.draggable {
    touchaction: none
}

移动浏览器未列在 JQuery UI 浏览器支持列表中。看看这个 SO question,它可能会帮助您在移动设备上实现拖放操作。

  • 是的,我认为问题在于功能没有被解雇,谢谢你的帮助


来源:https://www.codenong.com/23595081/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8802.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?