问题描述
根据下面的jQuery文档,代码可用于捕获mouseup和mouse down事件。但我的要求有点不同
As per the jQuery docs below code can be used to capture mouseup and mouse down events. But my requirement is bit different
$("#dic").mouseup(function () {
}).mousedown(function () {
});
但是如何计算mousedown位置与mouseup位置之间的鼠标移动坐标。请帮帮我。如何在mousedown和mouseup之间应用mousemove事件
But How can I calculate mouse moving co-ordinates between mousedown position to mouseup position. Please help me on this. How can I apply mousemove event between mousedown and mouseup
推荐答案
如果你需要捕获拖动过程中鼠标移动的所有点,绑定/取消绑定新的 mousemove
处理程序:
If you need to captue all points the mouse moves through during a drag, bind/unbind a new mousemove
handler:
var allPoints = [];
$("#dic").mouseup(function (e) {
$(this).unbind("mousemove", trackPoints);
}).mousedown(function (e) {
$(this).bind("mousemove", trackPoints);
});
function trackPoints(e) {
allPoints.push({ x: e.pageX, y: e.pageY });
}
这样, trackPoints
将在鼠标停止时开始触发并在其恢复时停止。
This way, trackPoints
will start firing when the mouse is down and stop when it goes back up.
您可能还想添加 if(e.which == 1)
到 mouseup
和 mousedown
处理程序的顶部,以便它们执行绑定
仅用于鼠标左键,而不是中键或右键。
You may also want to add a if(e.which == 1)
to the top of your mouseup
and mousedown
handlers so that they perform the bind
for a left mouse button only, not middle or right buttons.
这篇关于如何检索mousedown到mouseup事件之间的所有鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!