2016-04-26 17 views
0

タッチイベントをマウスイベントに変換するには、次のスクリプトを使用しています。それはかなりうまくいくが、2つのタッチイベント(例えば、図のデータをパンする)の間に2番目のイベントはトリガーされない。次に、次のイベントがトリガーされ、次のイベントはトリガーされません。タッチイベントをマウスイベントに変換するのは毎回のみです

だからこれは私の関数であるので、この動作はそのようである理由here

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);  
} 

誰もが知っている見つけますか?

更新: 'first.target'は最初に 'div.dragcover'であり、2回目には 'rect'、もう一度 'div.dragcover'などと試してみました。なぜこれがそうだと誰でも知っていますか?

+0

touchcancelイベントをmouseeventに変換しているようではありません。それは意図的なのでしょうか? –

+0

私はtouchcancelイベントが本当に必要ない&私はそれにバインドする必要がどのマウスイベント、任意のアイデアを知りませんか? – threxx

答えて

0

ほとんどの場合、マークアップに問題があるようです。ターゲットは常に指が実際に触れた要素でなければなりません。

+0

それでは、最初のタッチがすべて同じで、それから別のものが同じであるのはなぜですか?同じ – threxx

+1

これはおそらく役に立ちますか? 'document.elementFromPoint(event.clientX、event.clientY);' http://stackoverflow.com/questions/3918842/how-to-find-out-the-actual-event-target-of-touchmove -javascript-event – meditari

関連する問題