2016-04-26 45 views
0

我正在使用以下腳本將觸摸事件轉換爲鼠標事件。它工作得很好,但是在兩個觸摸事件之間(例如通過平移圖的數據)第二個事件不會被觸發。然後觸發下一個事件,接下來不是,依此類推。將觸摸事件轉換爲鼠標事件僅適用於每兩秒

所以這是我的功能,所以發現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',在第二個嘗試它''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事件 – meditari

相關問題