找到你要的答案

Q:chrome iOS touchmove event not working

Q:Chrome iOS touchmove事件不工作

I have the following working in safari on iOS, but in chrome on iOS it doesnt seem to be working. In fact the browser scrolls around so I don't think e.preventDefault is doing it's job.

canvas.addEventListener("touchmove", trackPositionTouch, true);

function trackPositionTouch(e) {
    e.preventDefault();
    mouse.x = e.targetTouches[0].pageX - canvas.offsetLeft;
    mouse.y = e.targetTouches[0].pageY - canvas.offsetTop;
}

我有以下在Safari在iOS上的工作,但在Chrome在iOS它似乎不工作。事实上在浏览器卷轴,所以我不认为e.preventDefault是做它的工作。

canvas.addEventListener("touchmove", trackPositionTouch, true);

function trackPositionTouch(e) {
    e.preventDefault();
    mouse.x = e.targetTouches[0].pageX - canvas.offsetLeft;
    mouse.y = e.targetTouches[0].pageY - canvas.offsetTop;
}
answer1: 回答1:

It's a better idea to check for both event types. This is how I did it in a project. I am using jQuery to select DOM elements.

var touch = e.originalEvent.touches[0] || e.originalEvent.targetTouches[0];

var parentOffset = $(this).parent().offset();

var relX = (touch.pageX - parentOffset.left);
var relY = (touch.pageY - parentOffset.top);

这是一个更好的主意,检查两个事件类型。这就是我在一个项目中做的。我用jQuery选择DOM元素。

var touch = e.originalEvent.touches[0] || e.originalEvent.targetTouches[0];

var parentOffset = $(this).parent().offset();

var relX = (touch.pageX - parentOffset.left);
var relY = (touch.pageY - parentOffset.top);
javascript  ios  google-chrome  canvas