javascript - 如何触发触摸事件?

标签 javascript safari webkit

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

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

我将确保文档已加载并发送代表先前商定场景的触摸事件。

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 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>

为了让浏览器将触摸事件解释为最终用户发出的触摸事件,我缺少什么设置?本质上,我希望浏览器响应一系列以编程方式触发的触摸开始、移动和结束事件而滚动。

最佳答案

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

问候,KJ。

关于javascript - 如何触发触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26744603/

相关文章:

javascript - 无法在 Firefox 中为同一输入字段触发 ONKEYDOWN 和 ONCHANGE 事件

ios - iOS 上的 Safari 是否拒绝下载 `application/octet-stream` 内容?

css - SVG 背景图像在 Safari 中无法正确缩放

javascript - 从 JavaScript 以编程方式打开 Safari/Google Chrome 开发者工具

javascript - Javascript 中的事件处理程序、闭包和垃圾收集

javascript - innerHTML 无法将文本注入(inject)到标记中

javascript - 如何解析从 javascript 或 jquery 中的 php 脚本检索的 json 数据

javascript - Safari 和 Chrome 上的 Tinymce 换行问题

Python QWebView : Accessing Network Resources

html - 外部定义的 SVG 标记和过滤器在 Chrome 中不起作用