この回答は少し遅れているかもしれませんが、将来的に役立つと確信しています。このクロスブラウザ機能を探しているときにこの質問に出くわしましたが、最初は無視していました。私は私の足跡をたどる人々に私の答えを提供するために戻ってきました.
まずは、ある程度の知識。このサイトは非常に役に立ちました。クロス ブラウザーの問題 (ほとんど) が解決され、あなたの娯楽のためにレイアウトされているからです (私たち開発者がブラウザーの仕組みを示すチャートや図を作成する必要があるとき、私は笑います..)
http://unixpapa.com/js/mouse.html
このページの下部近くに、「さまざまなマウス ボタンでここをクリックしてテストする」という青いリンクがあり、その上にコード スニペットが表示されます。これは、マウスダウンまたはマウスアップに入ります。右クリックしてこの場所のソースを表示すると、このリンクの上にある 2 つの関数と、イベントがデフォルトを実行するのを防ぐ「dont」関数、またはフォールスルーを防ぐ「dont」関数を保持するスクリプト タグが見つかります。すべての場合に必要であり、知っておくと便利です。
2 番目の知識は、別の Web サイトから得たもので、マウス ホイールの上下のイベントをキャプチャする方法についての洞察を提供してくれます。
http://www.javascriptkit.com/javatutors/onmousewheel.shtml
これをすべて 1 か所にまとめると、基本的に次のようになります。
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);
時間を節約するために [およびこれらのリンクを見たくない場合は知識を得る]、次の jsfiddle で実際の例を表示できます。
http://jsfiddle.net/BNefn/
EDIT
また、すべての mousemove イベントでこれを知る必要があるため、結果のボタンの「名前」とイベントの種類 (ダウンまたはアップ) を保存し、mousemove イベント中にその変数情報を呼び出すことができるとも言いました。これらの「ボタン」ごとに変数がある場合、どのボタンが押されていてどのボタンが押されていないかを確認し、マウスアップで押された変数をクリアできます。