最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pointer Events method of checking left- or right-click - Stack Overflow

programmeradmin1浏览0评论

I've configured Pointer Events through pointerevents-polyfill.

There's an issue I have where I cannot differentiate between left- and right-clicks where right-clicking a nav item will do the same action as left-clicking instead of opening the right-click menu.

The specific event I'm using is pointerup.

Is there a way with Pointer Events to check if the event is a left- or right-click?

I've configured Pointer Events through pointerevents-polyfill.

There's an issue I have where I cannot differentiate between left- and right-clicks where right-clicking a nav item will do the same action as left-clicking instead of opening the right-click menu.

The specific event I'm using is pointerup.

Is there a way with Pointer Events to check if the event is a left- or right-click?

Share Improve this question asked Feb 6, 2014 at 18:04 Kevin GhadyaniKevin Ghadyani 7,2978 gold badges47 silver badges66 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Looks like there is a property called button that has a value of 0 if it's the primary pointer (left mouse button).

I haven't used this library but from looking at the source and the W3C spec it would appear that way.

You can always print/debug the event and see what the property is.

The following code identifies “main button” type pointer events like left mouse click, touch.

if(e.pointerType !== 'mouse' || e.button === 0){
    //Not mouse pointer or mouse pointer with left button
}

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button

  • 0: Main button pressed, usually the left button or the un-initialized state
  • 1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
  • 2: Secondary button pressed, usually the right button
  • 3: Fourth button, typically the Browser Back button
  • 4: Fifth button, typically the Browser Forward button

I used event.type == 'click' (left) vs. event.type == 'contextmenu' (right).

发布评论

评论列表(0)

  1. 暂无评论