【发布时间】:2014-12-27 03:07:01
【问题描述】:
我试图弄清楚如何在移动浏览器中触发触摸事件以进行测试。我不是网络开发人员,所以这对我来说有点陌生。
此时我正在尝试捕获由我触摸屏幕创建的实际触摸事件,并重新触发它们以触发相同的响应。下面是我在移动 amazon.com 页面上所做的,期望轮播重复滑动,就像我用手指实际滑动它一样。但是,旋转木马不会滑动。似乎浏览器对待我重新触发的事件与原生事件不同。我在 Android 上的 Chrome 中运行它。
有没有人看到这种行为并知道我应该解决什么问题?
// EDITED after taking into consideration @Palpatim's suggestion
// stash away events on a carousel
var element = document.querySelector('.gw-carousel-viewport');
document.test_touch_move = []
element.addEventListener('touchstart', function(e){document.test_touch_start = e;}, false);
element.addEventListener('touchmove', function(e){document.test_touch_move.push(e);}, false);
element.addEventListener('touchend', function(e){document.test_touch_end = e;}, false);
//
// physically slide the carousel with my finder to trigger events
//
// store captured events from being overriden
var touch_start = document.test_touch_start;
var touch_move = document.test_touch_move;
var touch_end = document.test_touch_end;
//
// physically slide the carousel back to its initial position to make sure all event screen coordinates match
//
element.dispatchEvent(touch_start);
touch_move.forEach(function(e){element.dispatchEvent(e);});
element.dispatchEvent(touch_end);
// I do not see the same sliding behavior as I see when actually swiping with my finger
【问题讨论】:
标签: javascript google-chrome mobile