Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 966111 of TouchEvent

  • Revision slug: Web/API/TouchEvent
  • Revision title: TouchEvent
  • Revision id: 966111
  • Created:
  • Creator: cvrebert
  • Is current revision? Yes
  • Comment add gesture event classes to See Also

Revision Content

{{ APIRef("Touch Events") }}

The TouchEvent interface represents an event sent when the state of contacts with a touch-sensitive surface changes. This surface can be a touch screen or trackpad, for example. The event can describe one or more points of contact with the screen and includes support for detecting movement, addition and removal of contact points, and so forth.

Touches are represented by the {{ domxref("Touch") }} object; each touch is described by a position, size and shape, amount of pressure, and target element. Lists of touches are represented by {{ domxref("TouchList") }} objects.

Constructor

{{domxref("TouchEvent.TouchEvent", "TouchEvent()")}}
Creates a TouchEvent object.

Properties

This interface inherits properties from its parent, {{domxref("UIEvent")}} and {{domxref("Event")}}.

{{ domxref("TouchEvent.altKey") }} {{readonlyInline}}
A Boolean value indicating whether or not the alt key was down when the touch event was fired.
{{ domxref("TouchEvent.changedTouches") }} {{readonlyInline}}
A {{ domxref("TouchList") }} of all the {{ domxref("Touch") }} objects representing individual points of contact whose states changed between the previous touch event and this one.
{{ domxref("TouchEvent.ctrlKey") }} {{readonlyInline}}
A Boolean value indicating whether or not the control key was down when the touch event was fired.
{{ domxref("TouchEvent.metaKey") }} {{readonlyInline}}
A Boolean value indicating whether or not the meta key was down when the touch event was fired.
{{ domxref("TouchEvent.shiftKey") }} {{readonlyInline}}
A Boolean value indicating whether or not the shift key was down when the touch event was fired.
{{ domxref("TouchEvent.targetTouches") }}{{readonlyInline}}
A {{ domxref("TouchList") }} of all the {{ domxref("Touch") }} objects that are both currently in contact with the touch surface and were also started on the same element that is the target of the event.
{{ domxref("TouchEvent.touches") }} {{readonlyInline}}
A {{ domxref("TouchList") }} of all the {{ domxref("Touch") }} objects representing all current points of contact with the surface, regardless of target or changed status.

Touch event types

There are several types of event that can be fired to indicate that touch-related changes have occurred. You can determine which of these has happened by looking at the event's {{ domxref("event.type", "TouchEvent.type") }} property.

Note: It's important to note that in many cases, both touch and mouse events get sent (in order to let non-touch-specific code still interact with the user). If you use touch events, you should call {{ domxref("event.preventDefault()") }} to keep the mouse event from being sent as well.

{{event("touchstart")}}

Sent when the user places a touch point on the touch surface. The event's target will be the {{ domxref("element") }} in which the touch occurred.

{{event("touchend")}}

Sent when the user removes a touch point from the surface (that is, when they lift a finger or stylus from the surface). This is also sent if the touch point moves off the edge of the surface; for example, if the user's finger slides off the edge of the screen.

The event's target is the same {{ domxref("element") }} that received the touchstart event corresponding to the touch point, even if the touch point has moved outside that element.

The touch point (or points) that were removed from the surface can be found in the {{ domxref("TouchList") }} specified by the changedTouches attribute.

{{event("touchmove")}}

Sent when the user moves a touch point along the surface. The event's target is the same {{ domxref("element") }} that received the touchstart event corresponding to the touch point, even if the touch point has moved outside that element.

This event is also sent if the values of the radius, rotation angle, or force attributes of a touch point change.

Note: The rate at which touchmove events is sent is browser-specific, and may also vary depending on the capability of the user's hardware. You must not rely on a specific granularity of these events.

{{event("touchcancel")}}

Sent when a touch point has been disrupted in some way. There are several possible reasons why this might happen (and the exact reasons will vary from device to device, as well as browser to browser):

  • An event of some kind occurred that canceled the touch; this might happen if a modal alert pops up during the interaction.
  • The touch point has left the document window and moved into the browser's UI area, a plug-in, or other external content.
  • The user has placed more touch points on the screen than can be supported, in which case the earliest {{ domxref("Touch") }} in the {{ domxref("TouchList") }} gets canceled.

GlobalEventHandlers

{{SeeCompatTable}}

{{ domxref("GlobalEventHandlers.ontouchstart") }} {{experimental_inline}}
A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchstart")}} event.
{{ domxref("GlobalEventHandlers.ontouchend") }} {{experimental_inline}}
A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchend")}} event.
{{ domxref("GlobalEventHandlers.ontouchmove") }} {{experimental_inline}}
A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchmove")}} event.
{{ domxref("GlobalEventHandlers.ontouchcancel") }} {{experimental_inline}}
A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchcancel")}} event.

Example

See the example on the main Touch events article.

Specifications

Specification Status Comment
{{SpecName('Touch Events 2','#touchevent-interface', 'TouchEvent')}} {{Spec2('Touch Events 2')}} Added ontouchstart, ontouchend, ontouchmove, ontouchend global attribute handlers
{{SpecName('Touch Events', '#touchevent-interface', 'TouchEvent')}} {{Spec2('Touch Events')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatChrome("22.0")}} {{CompatGeckoDesktop("18.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Android Webview Chrome for Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("6.0")}} {{CompatVersionUnknown}} 11 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
TouchEvent() {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

  • {{domxref("Touch_events","Touch Events Overview")}}
  • {{domxref("GestureEvent")}}
  • {{domxref("MSGestureEvent")}}

Revision Source

<p>{{ APIRef("Touch Events") }}</p>

<p>The <strong><code>TouchEvent</code></strong> interface represents an event sent when the state of contacts with a touch-sensitive surface changes. This surface can be a touch screen or trackpad, for example. The event can describe one or more points of contact with the screen and includes support for detecting movement, addition and removal of contact points, and so forth.</p>

<p>Touches are represented by the {{ domxref("Touch") }}&nbsp;object; each touch is described by a position, size and shape, amount of pressure, and target element. Lists of touches are represented by {{ domxref("TouchList") }} objects.</p>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{domxref("TouchEvent.TouchEvent", "TouchEvent()")}}</dt>
 <dd>Creates a Touch<code>Event</code> object.</dd>
</dl>

<h2 id="Properties">Properties</h2>

<p><em>This interface inherits properties from its parent, {{domxref("UIEvent")}} and {{domxref("Event")}}.</em></p>

<dl>
 <dt>{{ domxref("TouchEvent.altKey") }} {{readonlyInline}}</dt>
 <dd>A Boolean value indicating whether or not the alt key was down when the touch event was fired.</dd>
 <dt>{{ domxref("TouchEvent.changedTouches") }} {{readonlyInline}}</dt>
 <dd>A {{ domxref("TouchList") }} of all the {{ domxref("Touch") }} objects representing individual points of contact whose states changed between the previous touch event and this one.</dd>
 <dt>{{ domxref("TouchEvent.ctrlKey") }} {{readonlyInline}}</dt>
 <dd>A Boolean value indicating whether or not the control key was down when the touch event was fired.</dd>
 <dt>{{ domxref("TouchEvent.metaKey") }} {{readonlyInline}}</dt>
 <dd>A Boolean value indicating whether or not the meta key was down when the touch event was fired.</dd>
 <dt>{{ domxref("TouchEvent.shiftKey") }} {{readonlyInline}}</dt>
 <dd>A Boolean value indicating whether or not the shift key was down when the touch event was fired.</dd>
 <dt>{{ domxref("TouchEvent.targetTouches") }}{{readonlyInline}}</dt>
 <dd>A {{ domxref("TouchList") }} of all the {{ domxref("Touch") }}&nbsp;objects that are both currently in contact with the touch surface <strong>and</strong> were also started on the same element that is the target of the event.</dd>
 <dt>{{ domxref("TouchEvent.touches") }} {{readonlyInline}}</dt>
 <dd>A {{ domxref("TouchList") }} of all the {{ domxref("Touch") }}&nbsp;objects representing all current points of contact with the surface, regardless of target or changed status.</dd>
</dl>

<h2 id="Touch_event_types">Touch event types</h2>

<p>There are several types of event that can be fired to indicate that touch-related changes have occurred. You can determine which of these has happened by looking at the event's {{ domxref("event.type", "TouchEvent.type") }} property.</p>

<div class="note"><strong>Note:</strong> It's important to note that in many cases, both touch and mouse events get sent (in order to let non-touch-specific code still interact with the user). If you use touch events, you should call {{ domxref("event.preventDefault()") }} to keep the mouse event from being sent as well.</div>

<h3 id="event(touchstart)">{{event("touchstart")}}</h3>

<p>Sent when the user places a touch point on the touch surface. The event's target will be the {{ domxref("element") }} in which the touch occurred.</p>

<h3 id="event(touchend)">{{event("touchend")}}</h3>

<p>Sent when the user removes a touch point from the surface (that is, when they lift a finger or stylus from the surface). This is also sent if the touch point moves off the edge of the surface; for example, if the user's finger slides off the edge of the screen.</p>

<p>The event's target is the same {{ domxref("element") }}&nbsp;that received the <code>touchstart</code> event corresponding to the touch point, even if the touch point has moved outside that element.</p>

<p>The touch point (or points)&nbsp;that were removed from the surface can be found in the {{ domxref("TouchList") }} specified by the <code>changedTouches</code> attribute.</p>

<h3 id="event(touchmove)">{{event("touchmove")}}</h3>

<p>Sent when the user moves a touch point along the surface.&nbsp;The event's target is the same {{ domxref("element") }}&nbsp;that received the <code>touchstart</code> event corresponding to the touch point, even if the touch point has moved outside that element.</p>

<p>This event is also sent if the values of the radius, rotation angle, or force attributes of a touch point change.</p>

<div class="note"><strong>Note:</strong> The rate at which <code>touchmove</code> events is sent is browser-specific, and may also vary depending on the capability of the user's hardware. You must not rely on a specific granularity of these events.</div>

<h3 id="event(touchcancel)">{{event("touchcancel")}}</h3>

<p>Sent when a touch point has been disrupted in some way. There are several possible reasons why this might happen (and the exact reasons will vary from device to device, as well as browser to browser):</p>

<ul>
 <li>An event of some kind occurred that canceled the touch; this might happen if a modal alert pops up during the interaction.</li>
 <li>The touch point has left the document window and moved into the browser's UI area, a plug-in, or other external content.</li>
 <li>The user has placed more touch points on the screen than can be supported, in which case the earliest {{ domxref("Touch") }} in the {{ domxref("TouchList") }}&nbsp;gets canceled.</li>
</ul>

<h2 id="GlobalEventHandlers">GlobalEventHandlers</h2>

<p>{{SeeCompatTable}}</p>

<dl>
 <dt>{{ domxref("GlobalEventHandlers.ontouchstart") }} {{experimental_inline}}</dt>
 <dd>A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchstart")}} event.</dd>
 <dt>{{ domxref("GlobalEventHandlers.ontouchend") }} {{experimental_inline}}</dt>
 <dd>A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchend")}} event.</dd>
 <dt>{{ domxref("GlobalEventHandlers.ontouchmove") }} {{experimental_inline}}</dt>
 <dd>A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchmove")}} event.</dd>
 <dt>{{ domxref("GlobalEventHandlers.ontouchcancel") }} {{experimental_inline}}</dt>
 <dd>A {{domxref("GlobalEventHandlers","global event handler")}} for the {{event("touchcancel")}} event.</dd>
</dl>

<h2 id="Example">Example</h2>

<p>See the <a href="/en/DOM/Touch_events#Example" title="en/DOM/Touch events#Example">example on the main Touch events article</a>.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Touch Events 2','#touchevent-interface', 'TouchEvent')}}</td>
   <td>{{Spec2('Touch Events 2')}}</td>
   <td>Added <code>ontouchstart</code>, <code>ontouchend</code>, <code>ontouchmove</code>, <code>ontouchend</code> global attribute handlers</td>
  </tr>
  <tr>
   <td>{{SpecName('Touch Events', '#touchevent-interface', 'TouchEvent')}}</td>
   <td>{{Spec2('Touch Events')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("22.0")}}</td>
   <td>{{CompatGeckoDesktop("18.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("6.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>11</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>TouchEvent()</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("Touch_events","Touch Events Overview")}}</li>
 <li>{{domxref("GestureEvent")}}</li>
 <li>{{domxref("MSGestureEvent")}}</li>
</ul>
Revert to this revision