使用JS跟踪鼠标速度 [英] Track mouse speed with JS

查看:104
本文介绍了使用JS跟踪鼠标速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用普通JS / JQuery跟踪鼠标速度的最佳方法是什么?我想跟踪用户在所有方向(向上/向下/向左/向右)移动鼠标的速度。

What's the best way to track the mouse speed with plain JS/JQuery? I'd like to track how fast a user moves the mouse in all directions (up/down/left/right).

推荐答案

Sparklines有一个很好的例子,可以跟踪鼠标移动并绘制图形。他们的代码可以在他们网站的源代码中找到,从第315行开始。

Sparklines has a nifty example of tracking mouse movement and graphing it. Their code is available in the source of their site starting at line 315.

简单有效。

这里是代码:

 var mrefreshinterval = 500; // update display every 500ms
 var lastmousex=-1; 
 var lastmousey=-1;
 var lastmousetime;
 var mousetravel = 0;
 $('html').mousemove(function(e) {
     var mousex = e.pageX;
     var mousey = e.pageY;
     if (lastmousex > -1)
         mousetravel += Math.max( Math.abs(mousex-lastmousex), Math.abs(mousey-lastmousey) );
     lastmousex = mousex;
     lastmousey = mousey;
 });

这篇关于使用JS跟踪鼠标速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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