您的代码没有考虑拖动开始的位置。 e.pageX
只会给你页面坐标,而不是差异。您需要检查移动距离的变化。
这篇文章非常相关。
这是基本代码:
$(document).mousemove(function(event) {
var startingTop = 10,
startingLeft = 22,
math = Math.round(Math.sqrt(Math.pow(startingTop - event.clientY, 2) +
Math.pow(startingLeft - event.clientX, 2))) + 'px';
$('span').text('From your starting point(22x10) you moved: ' + math);
});
编辑:现在我想我明白了 OP 在说什么。我用上面的代码想出了这个小提琴。它跟踪您当前相对于屏幕左上角的位置,并检查您的差异是否大于 5 个像素。
新脚本:
var oldMath = 0;
$(document).mousemove(function(event) {
var startingTop = 10,
startingLeft = 22,
math = Math.round(Math.sqrt(Math.pow(startingTop - event.clientY, 2) +Math.pow(startingLeft - event.clientX, 2))) + 'px';
$('#currentPos').text('you are at :' + math);
if(Math.abs(parseInt(math) - oldMath) > 5){
//you have moved 5 pixles, put your stuff in here
$('#logPos').append('5');
//keep track of your position to compare against next time
oldMath = parseInt(math);
}
});