I'm currently working on a mobile app/game. I am having a difficult time getting an inventory system working properly on a phone. the drag and drop work perfectly fine with a mouse on the pc, but will not work on phone.
JavaScript code:
document.addEventListener("DOMContentLoaded", function(event) {
$(document).ready(function(){
var videoPath = "videos/lg/";
var draggedItem = null;
$('.segmentListItem').each(function(index){
$(this).on("dragstart", handleDragStart);
$(this).on("drop", handleDrop);
$(this).on("dragover", handleDragOver);
});
function handleDragStart(e){
//console.log('start');
draggedItem=this;
e.originalEvent.dataTransfer.effectAllowed = 'move';
//e.dataTransfer.dropEffect = 'move'; //MH - do we need both of these?
e.originalEvent.dataTransfer.setData('text/html', this.innerHTML);
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
e.originalEvent.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
return false;
}
function handleDrop(e){
if (e.stopPropagation) {
e.stopPropagation(); // Stops some browsers from redirecting.
}
if (draggedItem != this) { //MH - swap if we're not dragging the item onto itself
var copy=$(this).clone(true,true);
var slot1 = $(this).attr("id");
var slot1Temp = slot1 + "temp";
var slot2 = $(draggedItem).attr("id");
$(this).replaceWith($(draggedItem).clone(true,true));
$(draggedItem).replaceWith($(copy).clone(true,true));
slotID = "slotID-" + slot1;
document.getElementById(slot1).id = slot1Temp;
document.getElementById(slot2).id = slot1;
document.getElementById(slot1Temp).id = slot2;
$.ajax({
type: "GET",
url: '/inventorySwap.php',
data: {'slot1': slot1,'slot2': slot2},
success: function(data) {
alert(data);
}
});
}
return false;
}
});
});
HTML/PHP
code:
<div class='inventorySlot". $obj->slot_id ."'>
<img class='segmentListItem' draggable='true' id=slotID-". $obj->slot_id ." src='{$itemImage}' />
</div>
Sorry if this question is set up a bit weird, I generally research and learn on my own. I don't like asking questions, I'm just completely stumped on this one.
What I need (and works perfect with mouse/on pc) Drag element from 1 div to another. In code posted, it also changed the id of swapped elements to match their div inventory slot number. this is to prevent errors when updating database to swap within slot record.
thank you for any and all help given.