forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (127 loc) · 5.32 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title: GlobalEventHandlers.onpointerdown
slug: Web/API/GlobalEventHandlers/onpointerdown
tags:
- API
- Document
- Element
- GlobalEventHandlers
- HTML DOM
- Pointer Events
- Pointer Events API
- PointerEvent
- Property
- Reference
- Window
browser-compat: api.GlobalEventHandlers.onpointerdown
---
<div>{{APIRef("HTML DOM")}}</div>
<p>The {{domxref("GlobalEventHandlers")}} event handler
<strong><code>onpointerdown</code></strong> is used to specify the event handler for the
{{event("pointerdown")}} event, which is fired when the pointing device is initially
pressed. This event can be sent to {{domxref("Window")}}, {{domxref("Document")}}, and
{{domxref("Element")}} objects.</p>
<p>This is functionally equivalent to the {{event("mousedown")}} event when generated due
to user activity with a mouse or mouse-compatible device. If the
<code>pointerdown</code> event isn't canceled through a call to
{{domxref("Event.preventDefault", "preventDefault()")}}, most user agents will fire a
<code>mousedown</code> event, so that sites not using pointer events will work.</p>
<p>You can also use {{domxref("EventTarget.addEventListener", "addEventListener()")}} to
add a listener for <code>pointerdown</code> events.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="brush: js"><var>target</var>.onpointerdown = <var>downHandler</var>;
var <var>downHandler</var> = <var>target</var>.onpointerdown;
</pre>
<h3 id="Value">Value</h3>
<p>A {{jsxref("Function")}} to handle the <code>pointerdown</code> event for the
<code><var>target</var></code> {{domxref("Element")}}, {{domxref("Document")}}, or
{{domxref("Window")}}. It receives as input the {{domxref("PointerEvent")}} describing
the <code>pointerdown</code> event.</p>
<h2 id="Example">Example</h2>
<h3>Responding to pointer down events</h3>
<p>This example demonstrates how to watch for and act upon <code>pointerdown</code> events
using <code>onpointerdown</code>. You could also use <code>addEventListener()</code>, of
course.</p>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><div id="target">
Tap me, click me, or touch me!
</div></pre>
<h4 id="CSS">CSS</h4>
<p>The CSS sets up the appearance of the target, and doesn't affect its functionality at all.</p>
<pre class="brush: css">#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;
}</pre>
<h4 id="JavaScript">JavaScript</h4>
<pre class="brush: js">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.textContent = `Thanks for ${action} me!`;
evt.preventDefault();
}</pre>
<p>This uses <code>onpointerdown</code> to establish the function
<code>handleDown()</code> as the event handler for pointer down events.</p>
<p>The <code>handleDown()</code> function, in turn, looks at the value of
{{domxref("PointerEvent.pointerType", "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.</p>
<p>Then the event's {{domxref("Event.preventDefault", "preventDefault()")}} method is
called to ensure that the <code>mousedown</code> 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.</p>
<p>We also have a handler for {{event("pointerup")}} events:</p>
<pre class="brush: js">targetBox.onpointerup = handleUp;
function handleUp(evt) {
targetBox.textContent = "Tap me, click me, or touch me!";
evt.preventDefault();
}</pre>
<p>This code restores 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).</p>
<p>In addition, the event's {{domxref("Event.preventDefault", "preventDefault()")}} method
is called to ensure that the <code>mouseup</code> event isn't triggered unnecessarily.
</p>
<h4 id="Result">Result</h4>
<p>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.</p>
<p>{{EmbedLiveSample("Responding_to_pointer_down_events", 450, 50)}}</p>
<h2 id="Specifications">Specifications</h2>
{{Specifications}}
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Pointer_events">Pointer events</a></li>
<li><a href="/en-US/docs/Web/API/Pointer_events/Using_Pointer_Events">Using Pointer
Events</a></li>
<li><a href="/en-US/docs/Web/CSS/CSSOM_View/Coordinate_systems">Coordinate systems</a>
</li>
<li>
<code><a href="/en-US/docs/Web/API/Document/pointerdown_event">Document: pointerdown</a></code>
event</li>
<li>
<code><a href="/en-US/docs/Web/API/HTMLElement/pointerdown_event">HTMLElement: pointerdown</a></code>
event</li>
</ul>