简体   繁体   English

Javascript,检测触摸设备

[英]Javascript, detect touch devices

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);
};

Got this function from here: What's the best way to detect a 'touch screen' device using JavaScript? 从这里获得此功能: 使用JavaScript检测“触摸屏”设备的最佳方法是什么?

But since Chrome 25 (25.0.1364) it returns true on my desktop which isn't a touch device. 但是从Chrome 25(25.0.1364)开始,它在我的桌面上返回true,这不是触控设备。 Also I've updated IE9 to IE10 and it returns true in IE! 我还将IE9更新为IE10,它在IE中返回true!

Searched around but couldn't find anything useful to fix this except using a something like this: http://detectmobilebrowsers.com/ 搜索周围但找不到任何有用的解决方法除了使用这样的东西: http//detectmobilebrowsers.com/

What do you recommend? 您有什么推荐的吗?

I'm looking forward to your responses! 我期待着你的回复!

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
   }
}

这似乎有效:

function isTouchEnabled() { return !!document.createTouch; }

You could use Modernizr to detect touch capability. 您可以使用Modernizr来检测触摸功能。

This question is very similar to What's the best way to detect a 'touch screen' device using JavaScript? 这个问题非常类似于使用JavaScript检测“触摸屏”设备的最佳方法什么? and should perhaps be considered a duplicate. 也许应该被视为重复。

i was experiencing a false positive IE10 touch issue as well when I was visiting the site i've been working on from my laptop. 当我访问我从笔记本电脑上工作的网站时,我也遇到了误报IE10触摸问题。 The original is_touch_device method, i used basically the same thing. 原来的is_touch_device方法,我用的基本相同。 I modified the latter half of that statement to be the following: 我修改了该语句的后半部分如下:

function is_touch_device()
{
    return !!('ontouchstart' in window) || (!!('onmsgesturechange' in window) && !!window.navigator.maxTouchPoints);
}

window.navigator.maxTouchPoints seems to be something specific in IE10 based on this post: http://msdn.microsoft.com/en-us/library/hh772144(v=vs.85).aspx window.navigator.maxTouchPoints似乎是基于这篇文章在IE10中特有的东西: http//msdn.microsoft.com/en-us/library/hh772144( v = vs。85) .aspx

You can use 51dergees.mobi for detection on the server appropriately changing page view. 您可以使用51dergees.mobi在服务器上进行适当更改页面视图的检测。 It has HasTouchScreen, IsSmartPhone, IsTablet properties etc. 它具有HasTouchScreen,IsSmartPhone,IsTablet属性等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM