我想知道,根据应用程序使用的设备,如何使用mousemove或touchmove。我想让触控设备听触移动,而桌面设备使用mousemove代替。我还想使用移动设备上似乎缺少的offsetX和offsetY =/
发布于 2014-03-15 12:38:07
您可以将事件包装在一个对象中,并根据is_touch_supported设置它们:
var body = document.querySelector('body'),
is_touch_supported = ('ontouchstart' in window) ? true : false,
EVENTS = {
POINTER_DOWN : is_touch_supported ? 'touchstart' : 'mousedown',
POINTER_UP : is_touch_supported ? 'touchend' : 'mouseup',
POINTER_MOVE : is_touch_supported ? 'touchmove' : 'mousemove'
};现在,您可以使用这样的事件:
body.addEventListener(EVENTS.POINTER_MOVE, function (e) {
e.preventDefault();
// normalize event so you can use e.offset X/Y
if(is_touch_supported){
e = e.changedTouches[0];
e.offsetX = e.pageX - e.target.offsetLeft;
e.offsetY = e.pageY - e.target.offsetTop;
}
// ...
});https://stackoverflow.com/questions/22423869
复制相似问题