虽然这一答案可能很晚,但我确信,它将帮助今后的人。 在寻找这一交叉浏览器特征时,我 st倒了这个问题,最初却忽视了这个问题。 我回头来回答那些步行走的人。
首先,一些知识。 我发现,这个网站非常有用,因为所有交叉浏览器问题(大多数)都得到了解决,并为您的判断作了规定(当我们的发展者需要绘制图表,说明浏览器如何工作时,我很困难)。
rel=“nofollow”http://unixpa.com/js/mouse.html
在这一页上,在底部附近,你会发现一个蓝色链接,它说“这里有各种 mo子进行测试”,比你看一看一刀。 这涉及你们的 mo或 mo。 如果你正确点击和查看这个地点的来源,你就会发现一个具有2个功能的文字标签,即上述链接,以及防止事件发生违约的固定功能,或者说,尽管并非在所有情况下都需要,但可以了解情况。
第二个知识来源于另一个网站,为我们提供了如何掌握摩擦和 events事件的见解。
rel=“nofollow”http://www.javascriptkit.com/javatutors/onmousewheel.shtml
为了把这一切放在一席之地,我们基本上有以下几点。
function GetMouseButton(e) {
// Normalize event variable
var e = window.event || e;
// Set button to initially not recognized (or false if you need to to be)
var button = Not Recognized ;
// Check if this is a button push event
if(e.type == mousedown || e.type == mouseup ) {
if (e.which == null) {
// Check if IE, if so, what e.button was pressed
button = (e.button < 2) ? "Left" :
((e.button == 4) ? "Middle" : "Right");
} else {
// All other browsers, what e.which was pressed
button = (e.which < 2) ? "Left" :
((e.which == 2) ? "Middle" : "Right");
}
} else {
// If this is not a button push event, then we get the direction
var direction = e.detail ? e.detail * (-120) : e.wheelDelta;
// And name the direction as a button
switch(direction) {
case 120: // Browsers use different variants of these
case 240:
case 360:
button = "Middle Scroll Up";
break;
case -120:
case -240:
case -360:
button = "Middle Scroll Down";
break;
}
}
alert(button);
}
/* Simple Bind function (like jQuery s for example) */
function Bind(elem, type, eventHandle) {
if (elem == null || elem == undefined) return;
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
} else {
elem["on"+type]=eventHandle;
}
}
/* Bind your mousedown / mouseup to the window, as well as the mousewheel */
Bind(window, mousedown , GetMouseButton);
Bind(window, mouseup , GetMouseButton);
/* One of FireFox s browser versions doesn t recognize mousewheel,
* we account for that in this line
*/
var MouseWheelEvent =
(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
Bind(window, MouseWheelEvent, GetMouseButton);
<><>>>> 为节省一段时间[和知道,如果你不关心研究这些联系],你可以在以下支部看到一个工作榜样:。
http://jsfiddle.net/BNefn/
EDIT
I should have also said, that since you need to know this on every mousemove event, you can simply store the resulting button name and event type (down or up), and then recall that variables information during your mousemove event. If you have a variable for each of these "buttons" you can then see which button is pressed and which isn t, and clear the variables that are pressed on mouseup.