【问题标题】:How to Create a TouchEvent in Chrome?如何在 Chrome 中创建 TouchEvent?
【发布时间】:2015-06-26 17:34:46
【问题描述】:

W3C specification 声明 initTouchEvent 如下:

void initTouchEvent (in DOMString    type,
                     in boolean      canBubble,
                     in boolean      cancelable,
                     in AbstractView view,
                     in long         detail,
                     in boolean      ctrlKey,
                     in boolean      altKey,
                     in boolean      shiftKey,
                     in boolean      metaKey,
                     in TouchList    touches,
                     in TouchList    targetTouches,
                     in TouchList    changedTouches);

但是,当我在 Chrome 44 中尝试时:

var e = document.createEvent('TouchEvent');
e.initTouchEvent("touchstart", true, true, window, 1,
                 false, false, false, false, touches, null, null);

其中touches 是有效的TouchList,这是 Chrome 创建的:

> TouchEvent {}
    altKey: false
    bubbles: true
    cancelBubble: false
    cancelable: true
    changedTouches: null
    charCode: 0
    ctrlKey: true
    currentTarget: null
    defaultPrevented: false
    detail: 0
    eventPhase: 0
    keyCode: 0
    layerX: 0
    layerY: 0
    metaKey: false
    pageX: 0
    pageY: 0
    path: Array[0]
    returnValue: true
    shiftKey: false
    srcElement: null
    target: null
    targetTouches: null
    timeStamp: 1435339572699
    touches: null
    type: "[object Window]"
    view: null
    which: 0

仔细查看type 字段。似乎 Chrome 没有遵循将第 4 个参数转换为类型而不是第一个参数的规范。

这给我们带来了一个问题,即我如何在 Chrome 中实际创建一个 TouchEvent,因为它不符合规范?

【问题讨论】:

  • initTouchEvent() 没有记录在 MDN 或(据我所知)W3C 上,因此很难找出这里实际发生的情况。
  • 您是否尝试过使用 JQuery 或类似的东西?
  • 它记录在 W3C 上(链接在问题中)。此外,jQuery 无法帮助我,因为 jQuery 调度“假”内部事件。

标签: javascript google-chrome touch dom-events


【解决方案1】:

通过查看Chromium sourceQiita (in Japanese),它的参数似乎是这样排列的:

initTouchEvent (TouchList touches,
                TouchList targetTouches,
                TouchList changedTouches,
                String type,
                Window view,
                number screenX,
                number screenY,
                number clientX,
                number clientY,
                boolean ctrlKey, 
                boolean altKey,
                boolean shiftKey,
                boolean metaKey);

注意 Chrome 不遵循 W3C 规范。


Chromium 源码中的相关部分:

TouchEvent.cpp第63行:

void TouchEvent::initTouchEvent(ScriptState* scriptState, TouchList* touches, TouchList* targetTouches,
        TouchList* changedTouches, const AtomicString& type,
        PassRefPtrWillBeRawPtr<AbstractView> view,
        int, int, int, int,
        bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
{
    if (dispatched())
        return;

    if (scriptState->world().isIsolatedWorld())
        UIEventWithKeyState::didCreateEventInIsolatedWorld(ctrlKey, altKey, shiftKey, metaKey);

    bool cancelable = true;
    if (type == EventTypeNames::touchcancel)
        cancelable = false;

    initUIEvent(type, true, cancelable, view, 0);

    m_touches = touches;
    m_targetTouches = targetTouches;
    m_changedTouches = changedTouches;
    m_ctrlKey = ctrlKey;
    m_altKey = altKey;
    m_shiftKey = shiftKey;
    m_metaKey = metaKey;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2016-06-12
    • 2021-04-24
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多