javascript检测桌面触摸和鼠标支持 [英] javascript detect desktop touch and mouse support

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

问题描述

应该如何区分纯触控设备和带有鼠标的设备?(就像今天的一些笔记本电脑)

How should touch-enabled devices be differentiates between pure touch and ones that also have a mouse? (like some of today's laptops)

需要在应用程序中优先考虑所有鼠标功能,如果运行应用程序的设备仅支持触摸,则需要更改功能.

There is a need to give all mouse functionality a priority in an application, and if the device running the app has only touch support, to change the functionality.

如果触摸设备也有鼠标指针,则逻辑建议应用应将该设备视为普通桌面,并可能添加触摸支持,但样式本身应响应鼠标事件.

If a touch-device also has a mouse pointer, logic suggests the app should consider that device as a normal desktop, and might add touch support as well, but the styling itself should respond to mouse events.

正常的触摸检测是这样实现的:文档中的'ontouchend';

normal touch detection is achieved like so: 'ontouchend' in document;

检测计算机是否为台式机的一种方法可能是检查屏幕分辨率:

One way to detect if the computer is desktop might be to check the screen resolution:

window.screen.width >= 1280 // desktop 

但这并不是很好,因为某些设备可能具有非常大的视口分辨率,而某些笔记本电脑的分辨率可能较低...

but that is not so good because some devices might have very large viewport resolution, and some laptops might have low resolution...

推荐答案

经过一番研究,我发现最好的解决方案是使用此代码:

After some research, I have found that the best solution was to use this code:

https://github.com/matthewhudson/device.js

所以,它的工作方式是给出代表设备和方向的 body 元素类名称,这样我几乎可以猜测用户是否有鼠标.(应用类desktop)

So, the way it works, is by giving the body element class names which represent the device and orientation, and with that I can pretty much guess if the user has a mouse. (class desktop is applied)

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

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