编辑:“这不能在 Angular UI 模式中完成”是一个有效的答案,如果确实如此的话。
这是我从触摸事件中获得的返回数据。明显缺少任何有用的触摸 X/Y 坐标 (https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches)。这是一个无可救药的通用问题,但是,有什么想法吗?传递给触摸时执行的函数的“事件”对象:
{
"originalEvent": {
"isTrusted": true
},
"type": "touchstart",
"timeStamp": 1450388006795,
"jQuery203026962137850932777": true,
"which": 0,
"view": "$WINDOW",
"target": {},
"shiftKey": false,
"metaKey": false,
"eventPhase": 3,
"currentTarget": {},
"ctrlKey": false,
"cancelable": true,
"bubbles": true,
"altKey": false,
"delegateTarget": {},
"handleObj": {
"type": "touchstart",
"origType": "touchstart",
"data": null,
"guid": 2026,
"namespace": ""
},
"data": null
}
现在,这是在 Canvas 中的 Angular UI 模式中,但鼠标事件工作正常。顺便说一句,这是我的元素:
link: function(scope, element, attrs, model){
//scope.canvasElem = element[0].children[0].children[0];
scope.canvasElem = angular.element($('.touchScreen'))[0];
scope.ctx = scope.canvasElem.getContext('2d');
这是我如何绑定(bind)的示例:
element.bind('touchstart', scope.touchStart);
编辑,这里有一个mousedown事件对象用于对比:
{
"originalEvent": {
"isTrusted": true
},
"type": "mousedown",
"timeStamp": 1450389131400,
"jQuery20309114612976554781": true,
"toElement": {},
"screenY": 436,
"screenX": 726,
"pageY": 375,
"pageX": 726,
"offsetY": 81,
"offsetX": 41,
"clientY": 375,
"clientX": 726,
"buttons": 1,
"button": 0,
"which": 1,
"view": "$WINDOW",
"target": {},
"shiftKey": false,
"relatedTarget": null,
"metaKey": false,
"eventPhase": 3,
"currentTarget": {},
"ctrlKey": false,
"cancelable": true,
"bubbles": true,
"altKey": false,
"delegateTarget": {},
"handleObj": {
"type": "mousedown",
"origType": "mousedown",
"data": null,
"guid": 2025,
"namespace": ""
},
"data": null
}
最佳答案
例如你可以使用指令绑定(bind)触摸事件,在指令中可以有这样的代码,
element.on('touchstart', function (event) {
event = event.originalEvent || event;
//actions
event.stopPropagation(); //if you need stop the propagation of the event
});
还需要根据您的目标注意 4 种类型的触摸事件(touchstart、touchend、touchmove、touchcancel)的行为。 对于捕获光标使用的位置,
element.on('touchmove', function (event) {
event.preventDefault();
if (event.targetTouches.length == 1) { //when only has one target in touch
var touch = event.targetTouches[0];
// Place element where the finger is
var x = touch.pageX + 'px';
var y = touch.pageY + 'px';
}
event.stopPropagation();
});
关于javascript - 触摸事件不返回触摸数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978128/