Я разработал приложение перетаскивания для записи статистики в футбольную игру, которая отлично работает на рабочем столе. Тем не менее, моя следующая проблема заключается в том, что мне нужно, чтобы он был совместим на мобильном телефоне / iPad. Поэтому мне нужно сопоставить события касания с событиями мыши, используя Javascript, однако я не уверен, как это сделать.
Этот код относится к событиям перетаскивания:
function drag_start(event) {
_('app_status').innerHTML = "Dragging the "+event.target.getAttribute('name');
event.dataTransfer.dropEffect = "move";
event.dataTransfer.setData("text", event.target.getAttribute('id') );
}
function drag_enter(event) {
_('app_status').innerHTML = "You are dragging over the "+event.target.getAttribute('name');
}
function drag_leave(event) {
_('app_status').innerHTML = "You left the "+event.target.getAttribute('name');
}
function drag_end(event) {
if(droppedIn == false){
_('app_status').innerHTML = "You let the "+event.target.getAttribute('name')+" go.";
}
droppedIn = false;
}
Я пытался добавить этот код на основе этого вопроса JavaScript отображает события касания на события мыши
function touchHandler(event)
{
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type)
{
case "touchstart": type = "mousedown"; break;
case "touchmove": type = "mousemove"; break;
case "touchend": type = "mouseup"; break;
default: return;
}
// initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, null);
first.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);
}
Любая помощь с благодарностью :)