I am trying to design an application using Phonegap, JQuery UI , JQuery mobile and I want to drag and drop but I don't know why my code doesn't work on mobile. It works fine on browser but when I run it on mobile it's not working.
Here is my code
Html code :
<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>
I need to drop those in this table :
now I use darg and drop by class here is javascript code : I use jquery ui
$(function () {
$(".cartridge").draggable({
cursor: 'move',
helper: 'clone',
revert: 'invalid',
});
$(".y").droppable({
accept: '.cartridge',
drop: function (event, ui) {
$(ui.draggable).appendTo(this);
checkwinner();
}
});
});
It works on browser but on mobile. Thanks
jQuery Mobile framework takes the "write less, do more" mantra to the next level: Instead of writing unique applications for each mobile device or OS, the jQuery mobile framework allows you to design a single highly-branded responsive web site or application that will work on all popular smartphone, tablet, and desktop ...
jQuery UI wasn't originally written to handle mobile devices. It won't even play nicely with jQuery Mobile. some parts of it may still work well in mobile devices, but anything that has anything to do with dragging will fail.
You have one of these problems: Your jQuery or jQuery UI Javascript path files are wrong. Your jQuery UI does not include draggable. Your jQuery or jQuery UI Javascript files are corrupted.
Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port. If the value of this option is set to false, it will prevent the DOM elements to be dragged .
The library I recommend is https://github.com/yeco/jquery-ui-touch-punch , with this your drag and drop from Jquery UI should work on touch devises
you can use this code which I am using, it also converts mouse events into touch and it works like magic.
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);
}
And in your document.ready just call the init() function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With