GlobalEventHandlers.onpointerdown

我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。

The GlobalEventHandlers event handler onpointerdown is used to specify the event handler for the pointerdown event, which is fired when the pointing device is initially pressed. This event can be sent to Window, Document, and Element objects.

This is functionally equivalent to the mousedown event when generated due to user activity with a mouse or mouse-compatible device. If the pointerdown event isn't canceled through a call to preventDefault(), most user agents will fire a mousedown event, so that sites not using pointer events will work.

You can also use addEventListener() to add a listener for pointerdown events.

Syntax

target.onpointerdown = downHandler;

var downHandler = target.onpointerdown;

Value

A Function to handle the pointerdown event for the target Element, Document, or Window. It receives as input the PointerEvent describing the pointerdown event.

Example

This example demonstrates how to watch for and act upon pointerdown events using onpointerdown. You could also use addEventListener(), of course.

JavaScript

First, let's look at the JavaScript code that handles the pointerdown event.

var targetBox = document.getElementById("target");

targetBox.onpointerdown = handleDown;

function handleDown(evt) {
  var action;
 
  switch(evt.pointerType) {
    case "mouse":
      action = "clicking";
      break;
    case "pen":
      action = "tapping";
      break;
    case "touch":
      action = "touching";
      break;
    default:
      action = "interacting with";
      break;
  }
 
  targetBox.innerHTML = "<strong>Thanks for " + action + " me!</strong>";
  evt.preventDefault();
}

This simply uses onpointerdown to establish the function handleDown() as the event handler for pointer down events.

The handleDown() function, in turn, looks at the value of pointerType to determine what kind of pointing device was used, then uses that information to customize a string to replace the contents of the target box.

Then the event's preventDefault() method is called to ensure that the mousedown event isn't triggered, potentially causing events to be handled twice if we had a handler for those events in case Pointer Event support is missing.

We also have a handler for pointerup events:

targetBox.onpointerup = handleUp;

function handleUp(evt) {
  targetBox.innerHTML = "Tap me, click me, or touch me!";
  evt.preventDefault();
}

This code's job is to just restore the original text into the target box after the user's interaction with the element ends (for example, when they release the mouse button, or when they lift the stylus or finger from the screen).

In addition, the event's preventDefault() method is called to ensure that the mouseup event isn't triggered unnecessarily.

HTML

The HTML is extremely simple:

<div id="target">
  Tap me, click me, or touch me!
</div>

CSS

The CSS simply sets up the appearance of the target, and doesn't affect its functionality at all.

#target {
  width: 400px;
  height: 30px;
  text-align: center;
  font: 16px "Open Sans", "Helvetica", sans-serif;
  color: white;
  background-color: blue;
  border: 2px solid darkblue;
  cursor: pointer;
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}

Result

The resulting output is shown below. Try tapping, clicking, or touching the box and see what happens. For full effect, try it with a variety of pointer types.

Specifications

Specification Status Comment
Pointer Events – Level 2
The definition of 'onpointerdown' in that specification.
Working Draft Non-stable version
Pointer Events
The definition of 'onpointerdown' in that specification.
Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 35Edge Full support YesFirefox Full support 59
Full support 59
Full support 29
Disabled
Disabled From version 29: this feature is behind the dom.w3c_pointer_events.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 11
Full support 11
Full support 10
Alternate Name
Alternate Name Uses the non-standard name: onmspointerdown
Opera ? Safari No support NoWebView Android Full support 37Chrome Android Full support 35Edge Mobile Full support YesFirefox Android Full support 29
Disabled
Full support 29
Disabled
Disabled From version 29: this feature is behind the dom.w3c_pointer_events.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android ? Safari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

See also