5

我使用 JQuery 1.7 来处理事件。

function objToStr(obj) {
    return ...string representation of the object
}
$slider.mousedown(function(e) {
    console.log("$slider.mousedown:\n" + objToStr(e) );
});

$slider.mousemove( function(e) {
    console.log("$slider.mousemove:\n" + objToStr(e) );
});

结果如下:

$slider.mousedown: 
altKey = false
 attrChange = undefined 
attrName = undefined 
bubbles = true 
button = 0 
buttons = undefined 
cancelable = true 
clientX = 319 
clientY = 13 
ctrlKey = false 
currentTarget = object 
data = null 
delegateTarget = object 
eventPhase = 3 
fromElement = undefined 
handleObj = object
isDefaultPrevented = (function) 
isImmediatePropagationStopped = (function) 
isPropagationStopped = (function) 
jQuery17209754494933906718 = true 
metaKey = false 
offsetX = undefined 
offsetY = undefined 
originalEvent = object 
pageX = 319 
pageY = 13 
preventDefault = (function) 
relatedNode = undefined 
relatedTarget = null 
screenX = 2876 
screenY = 223 
shiftKey = false 
srcElement = undefined 
stopImmediatePropagation = (function) 
stopPropagation = (function) 
target = object 
timeStamp = 869699957 
toElement = undefined 
type = "mousedown" 
view = object 
which = 1


$slider.mousemove: 
currentTarget = object
data = null 
delegateTarget = object 
exclusive = undefined 
handleObj = object 
isDefaultPrevented = (function) 
isImmediatePropagationStopped = (function) 
isPropagationStopped = (function) 
isTrigger = true 
jQuery17209754494933906718 = true 
namespace = "" 
namespace_re = null 
preventDefault = (function) 
result = undefined 
stopImmediatePropagation = (function) 
stopPropagation = (function) 
target = object 
timeStamp = 1334151903323 
type = "mousemove"

我需要访问e.whichmousemove显然它没有传递给事件处理程序?

我不想使用jQuery: Detecting pressed mouse button during mousemove event 中建议的标志算法。因为如果用户将鼠标按钮拖到浏览器窗口之外并在那里释放,当鼠标回到浏览器窗口时,仍然认为它被按下了。然后我必须为鼠标离开窗口时添加处理程序并且它太多了。有没有更简单的方法可以知道在事件处理程序中按下了哪个鼠标按钮?mousemove()

4

2 回答 2

3

我可以想象,因为它是一个移动事件,所以找出当时按下的键并不被认为是最重要或有效的,特别是因为该事件被如此频繁地触发。因此,您需要为mouseupand举办一个活动mousedown

var leftMButtonDown = false;

$slider.mousedown(function(e) {
    if(e.which === 1) leftMButtonDown = true;
});

$(document).mouseup(function(e) {
    if(leftMButtonDown && e.which === 1) leftMButtonDown = false;
});

$slider.mousemove(function(e) {
    if(leftMButtonDown == true) {
        console.log('Left mouse button was down on mousemove event');
    }
});

而且您必须为鼠标右键等添加更多变量

于 2012-04-12T19:49:00.753 回答
1
<body>
<div id="div" style=" height: 100px; border:1px solid red"></div>

<script>
  document.mouseState=-1; //global check mouse state

  document.onmousedown=function(e){
    document.mouseState=e.button;
  }
  document.onmouseup=function(e){
    document.mouseState=-1;
  }

  var d=document.getElementById('div');
  d.onmousemove= function(e){
    d.innerHTML+='; '+document.mouseState;

  }

</script>
</body>
于 2016-06-15T06:52:10.260 回答