如何确定鼠标光标的起始点和停止点?
假设用户开始移动鼠标并停在浏览器上的某个位置。如何使用 Javascript/jQuery 确定开始点和停止点?
起始点很简单,我可以通过 mousemove 事件获取它,但是停止点呢?
我无法使用 mouseenter
或 mouseleave
事件,因为我的游乐场是窗口或文档本身。
Let's say the user starts to move mouse and stop at somewhere on the browser. How can I determine start and stop point with Javascript/jQuery?
Start point is easy, I can get it with mousemove
event but what about stop point?
I can't use mouseenter
or mouseleave
events because my playground is window or document itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用setTimeout 和clearTimeout 的组合。
如果用户在 x 秒内没有移动,则意味着他停止了。
我在这里给你写了一个例子:
using a combination of setTimeout and clearTimeout.
If the user doesn't move for x amount of seconds then it means that he stopped.
Here I wrote you an example:
起始位置是首次触发 mousemove 事件时的位置。它将持续发射,直到鼠标停止移动。因此,如果您将计时器(setTimeout/clearTimeout)添加到 mousemove 事件函数中,则可以说鼠标在经过一定时间后停止移动,而无需调用 mousemove 函数。
The start location is the location when the mousemove event is first fired. It will keep firing until the mouse stops moving. So, if you add a timer (setTimeout/clearTimeout) to your mousemove event function, you can say the mouse has stopped moving after a certain amount of time has passed without the mousemove function being called.