下面的代碼將始終與ID =「更新速度」跨度更新鼠標的垂直運動速度。代碼是不言自明的,它只是保存當前位置,前一個位置,當前時間和前一個時間,然後使用此公式計算速度(速度=(pos2 - pos1)/(time2 - time1))
HTML
<span id="update-speed">Update speed</span>
JS
var prev_time = new Date();
var prev_pos_y = 0;
$(document).mousemove(function(e) {
var now = new Date();
current_pos_y = e.pageY;
time_interval = now.getTime() - prev_time.getTime();
if(time_interval != 0)
{
speed = (Math.abs(current_pos_y - prev_pos_y)/time_interval);
}
else
speed = 0;
console.log(speed);
$('#update-speed').text(speed);
prev_time = now;
prev_pos_y = current_pos_y;
});
如何abt [this](https://stackoverflow.com/a/6417110/3551786) – Durga
鼠標移動速度 – ShibinRagh