我正在尝试使用 Phonegap、JQuery UI、JQuery mobile 设计一个应用程序,我想要拖放,但我不知道为什么我的代码不能在移动设备上运行。它在浏览器上运行良好,但当我在移动设备上运行它时,它不起作用。
这是我的代码
HTML 代码:
<div class="mathheader" align="center" id="container">
<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>
<table width="100%" border="1" cellspacing="0" cellpadding="5">
<tr bgcolor="#F2F2F2">
<td width="50%" align="center" class="x" >
<p><b>coulm1</b></p>
</td>
<td width="50%" align="center" class="x">
<p><b>coulm2</b></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 用户界面
$(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 的拖放操作应该可以在触摸设备上运行
你可以使用我正在使用的这段代码,它还将鼠标事件转换为触摸,它的工作原理就像魔术一样。
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() 函数即可。
关于javascript - jquery ui 拖放功能在移动设备上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595081/