如何检测用户鼠标移动的距离? [英] How can I detect the distance that the user's mouse has moved?

查看:1041
本文介绍了如何检测用户鼠标移动的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测鼠标移动的距离(以像素为单位)。我目前正在使用:

I'm trying to detect the distance the mouse has moved, in pixels. I am currently using:

$(document).mousemove(function(event) {
    var startingTop = 10,
        startingLeft = 22,
        math = Math.abs(((startingTop - event.clientY) + (startingLeft - event.clientX)) + 14) + 'px';
    $('span').text('From your starting point(22x10) you moved:   ' + math);
});

但是,我觉得这不是正确的做法,或者是这样吗?感觉与我不一致。

However, I don't feel like this is the right way to do this, or is it? It doesn't feel to consistent to me.

这是一个关于它现在如何工作的演示: http://jsfiddle.net/Em4Xu/1/

Here is a demo of how it is working right now: http://jsfiddle.net/Em4Xu/1/

我实际上正在开发一种拖累和放大器。 drop plugin我想创建一个名为 distance 的功能,就像draggable一样,你需要在拖动它之前拉动鼠标一定数量的像素。我不是100%确定如何做到这一点,所以首先我需要获取鼠标从startingTop和startingLeft位置移动的像素。

I'm actually developing a drag & drop plugin and I want to create a feature called distance, like draggable has it, where you have to pull your mouse a certain amount of pixels before it drags. I'm not 100% sure how to do this, so first I need to get the pixels that the mouse has moved from the startingTop and startingLeft position.

有没有人有任何建议?

推荐答案

你得到了数学错误。以下是改进版: http://jsfiddle.net/stulentsev/Em4Xu/26/

You got your math wrong. Here's improved version: http://jsfiddle.net/stulentsev/Em4Xu/26/

$(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);
});

这篇关于如何检测用户鼠标移动的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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