Javascript,检测触摸设备 [英] Javascript, detect touch devices

查看:65
本文介绍了Javascript,检测触摸设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此功能来检测设备是否为触控设备:

I'm using this function to detect if the device is a touch device:

function is_touch_device()
{
    return !!('ontouchstart' in window) || !!('onmsgesturechange' in window);
};

从这里得到这个功能:使用JavaScript检测'触摸屏'设备的最佳方法是什么?

Got this function from here: What's the best way to detect a 'touch screen' device using JavaScript?

但是自Chrome 25(25.0.1364)起,它在我的桌面上返回true,这不是触控设备。
此外我已经将IE9更新为IE10并且它在IE中返回true!

But since Chrome 25 (25.0.1364) it returns true on my desktop which isn't a touch device. Also I've updated IE9 to IE10 and it returns true in IE!

搜索到但是找不到任何有用的东西来解决这个问题,除非使用了某些东西像这样: http://detectmobilebrowsers.com/

Searched around but couldn't find anything useful to fix this except using a something like this: http://detectmobilebrowsers.com/

你推荐什么?

我期待你的回复!

推荐答案

代码工作得很好,浏览器能够理解触摸事件,只是因为你的屏幕不可触摸并不意味着浏览器不支持该功能。您要测试的是硬件功能,这是不可测试的。您可以使用不同的方式,但是在触摸一次之后查看用户是否实际使用触摸界面,例如这篇文章描述了大型图书馆使用的许多其他文章,例如Modernizer。

The code works just fine, the browser is able to understand touch events, just because your screen isn't touchable doesn't mean that the browser doesn't support the functionality. What you are looking to test is a hardware capability which isn't really testable. You can always use different ways though of seeing if the user is actual using a touch interface after touching it once, such as this article describes, or many others that larger libraries use such as Modernizer.

作为参考,实际使用的代码是上面的文章是:

As a reference the code actually used in the article above is:

function isTouchDevice() {
   var el = document.createElement('div');
   el.setAttribute('ongesturestart', 'return;');
   if(typeof el.ongesturestart == "function"){
      return true;
   }else {
      return false
   }
}

这篇关于Javascript,检测触摸设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆