检测鼠标光标类型
我想要 JavaScript 代码来检测鼠标光标类型。
例如,当光标悬停在
我将如何检测这个?
I want JavaScript code to detect the mouse cursor type.
For example when the cursor hovers in <textarea>
it changes from default to text.
How would I go about detecting this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以这样做,但它并不漂亮,而且可能会很慢,具体取决于页面上有多少元素。
You could do this, but its not pretty, and will probably be quite slow depending on how many elements you have on your page.
来检测光标类型
您可以使用JavaScript
,JavaScript 代码应该如下所示,
您也可以查看此 Jsfiddle for Js 光标检测
上面是编写的 Jquery 代码,您也可以使用 Vanilla JS,只需将其更改为
JavaScript 应该如下所示
You can detect the cursor type using JavaScript
like
and the JavaScript code should look something like this
you can also look at this Jsfiddle for Js Cursor Detection
the above is the Jquery code written , you can also use the Vanilla JS for that you just need to change it to
and the JavaScript should look something like this
我有一个很好的 jQuery 扩展,非常适合这种类型的事情,要点如下:
要使用它,只需执行以下操作:
还
应该提及,如果您为“position”和“isHover”提交多个元素,例如
$("#eleID1, .elementsWiththisClass")
,那么它将返回一个包含以下对象的Array
:I have a nice jQuery Extension perfect for this type of thing at this gist:
To use it just do something like:
also
should also mention, if you submit multiple elements like
$("#eleID1, .elementsWiththisClass")
for "position" and "isHover" then it will return anArray
containing objects like:正如此处所建议的,使用
getCompulatedStyle
对我有用。虽然当光标设置为 auto 时,这无助于检测确切的光标类型,但我们至少可以在光标设置为 auto 以外的其他值时使用它。
As suggested here, using
getComputedStyle
worked for me.Although this does not help detect the exact cursor type when cursor is set to auto, we can at least use it when cursor is set to something other than auto.
我认为您可以读取
cursor
css 属性,就像您可以设置它一样,但是您必须从特定元素执行此操作,因为据我所知,无法从窗口或文档对象中读取光标类型。按照这个逻辑,要获取当前光标类型,您必须找到鼠标所在的当前元素并读取其cursor
css。但是,您必须不断检查光标是否发生变化,这很慢并且容易出错(通常,您应该总是尝试将代码放入事件处理程序中以对某些内容做出反应,而不是不断检查是否发生变化它已经发生了,将你的代码放入该函数中会更逻辑、更高效、更健壮、更清晰。)但是检测光标类型的想法仍然让我着迷,如果有人知道我很想听听它。 :D
作为替代解决方案,您为什么不直接设置一个事件处理程序,以便在它进入会更改它的元素时,而不是读取光标类型?这会更不容易出错,而且可能更直接,因为我认为您不太关心光标,但如果鼠标已进入特定元素。
I think you can read the
cursor
css property just like you can set it, but you have to do this from a specific element because AFAIK there's no way to just read the cursor type from the window or document object. Following this logic, to get the current cursor type you would have to find the current element the mouse is over and read itscursor
css. However, you'd constantly have to check to see if the cursor changed, which is slow and error prone (As a rule you should almost always try to try to put your code in an event handler to react to something instead of constantly checking if its happened yet and putting your code in that function its more logical, efficient, robust, and clear.)But the idea of detecting the cursor type still fascinates me, and if anyone knows how I'd love to hear about it. :D
As an alternate solution, rather than reading the cursor type, why don't you just set an event handler for when it enters an element that would change it? This would be a lot less error prone and probably more direct, because I don't think you care so much about the cursor, but if the mouse has entered a specific element.