简体   繁体   English

如何触发触摸事件?

[英]How to trigger a touch event?

Lets start with some event listeners:让我们从一些事件监听器开始:

window.addEventListener('scroll', function (e) {
    console.log('scroll', e);
});
window.addEventListener('touchstart', function (e) {
    console.log('touchstart', e);
});
window.addEventListener('touchmove', function (e) {
    console.log('touchmove', e);
});
window.addEventListener('touchend', function (e) {
    console.log('touchend', e);
});

I need to programmatically touch the document in position {pageX: 0, pageY: 0} , move it to {pageX: 0, pageY: 100} and end the touch event.我需要以编程方式触摸位置{pageX: 0, pageY: 0}的文档,将其移动到{pageX: 0, pageY: 100}并结束触摸事件。

To do this, I am going to build a helper function TouchEvent that will trigger the touch event on the specified element.为此,我将构建一个辅助函数TouchEvent ,它将触发指定元素上的触摸事件。

/**
 * @see https://gist.github.com/sstephenson/448808
 * @see https://developer.apple.com/library/iad/documentation/UserExperience/Reference/TouchEventClassReference/TouchEvent/TouchEvent.html
 * @see http://stackoverflow.com/questions/18059860/manually-trigger-touch-event
 */
function touchEvent (element, type, identifier, pageX, pageY) {
    var e,
        touch,
        touches,
        targetTouches,
        changedTouches;

    touch = document.createTouch(window, element, identifier, pageX, pageY, pageX, pageY);

    if (type == 'touchend') {
        touches = document.createTouchList();
        targetTouches = document.createTouchList();
        changedTouches = document.createTouchList(touch);
    } else {
        touches = document.createTouchList(touch);
        targetTouches = document.createTouchList(touch);
        changedTouches = document.createTouchList(touch);
    }

    e = document.createEvent('TouchEvent');
    e.initTouchEvent(type, true, true, window, null, 0, 0, 0, 0, false, false, false, false, touches, targetTouches, changedTouches, 1, 0);

    window.dispatchEvent(e);
};

I am going to make sure that the document is loaded and dispatch the touch events representing earlier agreed scenario.我将确保加载文档并分派代表早先商定方案的触摸事件。

document.addEventListener('DOMContentLoaded', function() {
    var identifier = new Date().getTime(),
        element = document,
        i = 0;

    touchEvent(element, 'touchstart', identifier, 0, 0);
    while (i++ < 100) {
        touchEvent(element, 'touchmove', identifier, 0, i);
    }
    touchEvent(element, 'touchend', identifier, 0, i);
});

The expected is that touchstart , touchmove and touchend events have been triggered.预期是touchstarttouchmovetouchend事件已被触发。 The unexpected is that scroll event has not been triggered and the actual "touching" of the document is not reflected in the current document.意料之外的是, scroll事件没有触发,文档的实际“触摸”并没有反映在当前文档中。

 window.addEventListener('scroll', function (e) { console.log('scroll', e); }); window.addEventListener('resize', function (e) { console.log('resize', e); }); window.addEventListener('touchstart', function (e) { console.log('touchstart', e); }); window.addEventListener('touchmove', function (e) { console.log('touchmove', e); }); window.addEventListener('touchend', function (e) { console.log('touchend', e); }); /** * @see https://gist.github.com/sstephenson/448808 * @see https://developer.apple.com/library/iad/documentation/UserExperience/Reference/TouchEventClassReference/TouchEvent/TouchEvent.html * @see http://stackoverflow.com/questions/18059860/manually-trigger-touch-event */ function touchEvent (element, type, identifier, pageX, pageY) { var e, touch, touches, targetTouches, changedTouches; if (!document.createTouch) { throw new Error('This will work only in Safari browser.'); } touch = document.createTouch(window, element, identifier, pageX, pageY, pageX, pageY); if (type == 'touchend') { touches = document.createTouchList(); targetTouches = document.createTouchList(); changedTouches = document.createTouchList(touch); } else { touches = document.createTouchList(touch); targetTouches = document.createTouchList(touch); changedTouches = document.createTouchList(touch); } e = document.createEvent('TouchEvent'); e.initTouchEvent(type, true, true, window, null, 0, 0, 0, 0, false, false, false, false, touches, targetTouches, changedTouches, 1, 0); window.dispatchEvent(e); }; document.addEventListener('DOMContentLoaded', function() { var identifier = new Date().getTime(), element = document, i = 0; touchEvent(element, 'touchstart', identifier, 0, 0); while (i++ < 100) { touchEvent(element, 'touchmove', identifier, 0, i); } touchEvent(element, 'touchend', identifier, 0, i); });
 #playground { background: #999; height: 5000px; }
 <div id="playground"></div>

What is my setup missing to make the browser interpret the touch events as if those were issued by the end user?我的设置缺少什么才能使浏览器将触摸事件解释为由最终用户发出? In essence, I am expecting the browser to scroll in response to the series of programmatically triggered touch start, move and end events.本质上,我希望浏览器滚动以响应一系列以编程方式触发的触摸开始、移动和结束事件。

I'm not sure if I understood your question correctly, but if you start touch at the top of page and drag down you're trying to scroll page to top and it's already there so why should the scroll trigger.我不确定我是否正确理解了您的问题,但是如果您开始触摸页面顶部并向下拖动,则您正在尝试将页面滚动到顶部并且它已经在那里,所以为什么要触发滚动。 Perhaps start at position {pageX: 0, pageY: 100} and end at {pageX: 0, pageY: 0} and then see if it's still not working.也许从位置{pageX: 0, pageY: 100}并在{pageX: 0, pageY: 0} ,然后看看它是否仍然不起作用。

Regards, KJ.问候,KJ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM