如何触发触摸事件?

10

让我们从一些事件监听器开始:

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

我需要以编程方式触摸位于位置{pageX: 0, pageY: 0}的文档,将其移动到{pageX: 0, pageY: 100}并结束触摸事件。
为此,我将构建一个辅助函数TouchEvent,它将在指定元素上触发触摸事件。
/**
 * @see https://gist.github.com/sstephenson/448808
 * @see https://developer.apple.com/library/iad/documentation/UserExperience/Reference/TouchEventClassReference/TouchEvent/TouchEvent.html
 * @see https://dev59.com/WmMl5IYBdhLWcg3w3aI-
 */
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);
};

我会确保文档已经加载并触发先前商定的场景中的触摸事件。
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);
});

期望的是触发了touchstarttouchmovetouchend事件。意外的是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 https://dev59.com/WmMl5IYBdhLWcg3w3aI-
 */
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>

我的设置缺少什么,以使浏览器将触摸事件解释为用户发出的那样? 实质上,我希望浏览器根据一系列通过编程触发的触摸开始、移动和结束事件滚动。


document.createTouch()已被弃用。 现在您必须使用Touch接口。但是似乎这个接口还没有在一些著名的浏览器中实现。 - ABeltramo
1个回答

1

我不确定我是否正确理解了你的问题,但是如果你从页面顶部开始触摸并向下拖动,你正在尝试将页面滚动到顶部,而它已经在那里,所以为什么应该触发滚动。也许从位置{pageX: 0, pageY: 100}开始,然后结束于{pageX: 0, pageY: 0},然后看看是否仍然无法工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接