可靠地检测用户是否在网页上使用鼠标? [英] Reliably detect if a user is using a mouse on a webpage?

查看:272
本文介绍了可靠地检测用户是否在网页上使用鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的页面上,某些iframe位于水平滚动的<div>中.如果用户使用的是鼠标,则可以使用滚动条进行滚动,我们希望他们能够在iframe中选择文本.但是,如果他们仅使用触摸,则滚动条很麻烦,我想在整个对象上覆盖一个透明元素,以使它们能够通过拖动轻松滚动,这当然会牺牲选择文本功能,但在这种情况.

On our page we have some iframes in a long horizontally-scrolling <div>. If the user is using a mouse they can scroll with the scrollbars and we would like them to be able to select text within the iframes. If they are using touch only however, the scrollbars are a hassle and I would like to overlay a transparent element over the whole thing to give them the ability to scroll easily by dragging, this of course sacrifices the select-text feature but makes sense in that scenario.

我的问题是什么,有没有一种方法可以可靠地检测用户是否正在通过鼠标与网页进行交互?

Which gets me to my question, is there a way to reliably detect if a user is interacting with a webpage via a mouse?

我在检测触摸或鼠标时所见到的一切都是,触摸会广播鼠标事件,因此很难检测到触摸或鼠标(更何况两者都可以).我的问题很简单-用户是否通过鼠标与页面进行了交互.

Everything I've seen on detecting touch or mouse is that touch will broadcast mouse events so it is very difficult to detect touch OR mouse (not to mention that you can have both). My problem is simpler - it is whether the user has interacted with the page via a mouse.

有人能想到一种可靠的方法吗?

Can anyone think of a way to check this reliably?

推荐答案

鼠标可以完成触摸设备无法完成的一件事-无需按下任何按钮即可移动.因此,我只在页面加载时安装一个onMouseMove事件,如果它触发了没有按下的按钮,则将该用户标记为鼠标用户.然后,您可以通过Cookie或LocalStorage保留该事件,因为该标志在同一环境中不会更改,因此请立即删除该事件.实施一次触发事件的确切方法取决于您使用的库(如果有的话),但是使用Mootools/JQuery文档应该很容易.

A mouse can do one single thing a touch device can never do - move without having any buttons pressed. So I'd just install an onMouseMove event on page load, and if it triggers without buttons pressed mark the user as a mouse user. You could then persist this through a cookie or LocalStorage since the flag will not change within the same environment, and remove the event right away. The precise way of implementing the single-fire event depends on which library you use (if any at all), but it should be easy with Mootools/JQuery docs in hand.

通常,在大多数情况下,我会建议仅检查触摸界面的简便方法:

In general I'd recommend the easier route of just checking for a touch interface in most cases :

if('ontouchstart'in window) 
{
    /* it's a touch device */ 
}

这篇关于可靠地检测用户是否在网页上使用鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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