如何使用Jquery / Javascript在运行Windows的PC上实现触摸功能?
作者:互联网
我已经使用这些代码在我的网站上实现了触摸功能,它适用于iPad / iPhone.
// registering touch events
function initTouchEvents() {
// if a device is a touch device
if (window.Touch) {
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
}
// making items sortable in touch screen devices
function touchHandler(event) {
var touches = event.changedTouches,
first = touches[0],
type = "";
switch (event.type) {
case "touchstart": type = "mousedown"; break;
case "touchmove": type = "mousemove"; break;
case "touchend": type = "mouseup"; break;
default: return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY,
first.clientX, first.clientY, false, false, false, false, 0/*left*/, null);
first.target.dispatchEvent(simulatedEvent);
if (event.type == "touchmove") {
event.preventDefault();
}
}
但是当我在触摸屏电脑上测试我的网站时,它没有运行,我发现了window.Touch只适用于iPhone / iPad.我还尝试了各种其他事件,如typeof(window.ontouchstart!=’undefined’)和(‘ontouchstart’在窗口|| typeof TouchEvent!=“undefined”)但它没有检测到它是触摸屏而不是注册活动以进行触摸移动.
我在这里问的是,javascript中的一个事件可以检测所有触摸设备(IOS / Android / Windows / OSX)并运行注册事件.
解决方法:
Phantom Limb使桌面浏览器模拟触摸事件
https://github.com/brian-c/phantom-limb
标签:javascript,jquery,touch,touchscreen 来源: https://codeday.me/bug/20190902/1789578.html