简体   繁体   English

如果触摸,否则单击,否则

[英]If touch, else if click, else

Any way to differentiate between touch and click? 有什么方法可以区分触摸和点击吗? "Why on earth would you want to?" “为什么要在地球上?” Always someone that thinks they know every possible use case, so to you: 总是有人以为他们知道每种可能的用例,所以对您来说:

We have a muti-tiered drop down nav menu. 我们有一个多层下拉式导航菜单。 If a user is on a regular laptop with a mouse, then menus drop down on: onmouseover. 如果用户使用鼠标在普通笔记本电脑上,则菜单下拉菜单为:onmouseover。 However if they click a first-tier item, we'd like them to go direct to a page. 但是,如果他们单击第一层项目,我们希望他们直接转到页面。 So the problems comes in when the user in on a laptop with touch capability. 因此,当用户使用具有触摸功能的笔记本电脑时,就会出现问题。 In this case, the touch is considered both a click and a mouseover. 在这种情况下,触摸被视为单击和鼠标悬停。 So the user would see the menus drop down, before being taken direct to the page hooked up in the first tier, or menu headers. 因此,用户在直接进入挂钩在第一层的页面或菜单标题之前,将看到菜单下拉菜单。 They'd never be able to select a 2nd or 3rd tier menu item. 他们将永远无法选择第二层或第三层菜单项。

What you can do then, is to detect if the user is on a tablet/smartphone or on a laptop before declaring your events and afterwhat, you add the right event regarding on the device. 然后,您可以做的是在声明事件之前检测用户是在平板电脑/智能手机上还是在笔记本电脑上,然后在设备上添加正确的事件。

You can detect the devise by using a regex on the userAgent string witch contain data on the user browser and can be reached from the navigator API => navigator.userAgent 您可以通过在userAgent上使用正则表达式来检测设备,字符串可以包含用户浏览器上的数据,并且可以通过navigator API => navigator.userAgent

See this web site to get regex you need: http://detectmobilebrowsers.com/ 请访问此网站获取所需的正则表达式: http : //detectmobilebrowsers.com/

Or you can also use an external library like mobile-detect.js. 或者,您也可以使用外部库,例如mobile-detect.js。

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

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