--- title: TouchEvent slug: Web/API/TouchEvent translation_of: Web/API/TouchEvent ---
{{ APIRef("Touch Events") }}
TouchEvent
介面表示了一個由觸控平面因觸碰而改變狀態所發出的事件。平面可以是觸控螢幕或是觸控板,TouchEvent
能描述一或多個觸碰點,並且支援偵測其移動、增加或減少觸碰點等等功能。
觸碰點是由 {{ domxref("Touch") }} 物件表示;每一個觸碰點描述了其位置、大小與形狀、壓力值以及目標元素。觸碰點列表由 {{ domxref("TouchList") }} 所表示。
TouchEvent
object.此介面也繼承了其父介面 {{domxref("UIEvent")}} 與 {{domxref("Event")}} 的屬性
觸控事件有多個種類可以代表觸碰狀態發生了改變。可以藉由 {{ domxref("event.type", "TouchEvent.type") }} 屬性來確認是哪一個種類的觸控事件。
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.
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.
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.
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.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):
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","preventDefault()")}} to keep the mouse event from being sent as well.
The exception to this is Chrome, starting with version 56 (desktop, Chrome for android, and android webview), where the default value for {{event("touchstart")}} and {{event("touchmove")}} is true
and calls to {{domxref("Event.preventDefault","preventDefault()")}} are not needed. To override this behavior, you simply set the passive
option to false
as shown in the example below. This change prevents the listener from blocking page rendering while a user is scrolling. A demo is available on the Google Developer site.
{{SeeCompatTable}}
See the example on the main Touch events article.
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. |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | {{CompatChrome("22.0")}} | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("18.0")}}[1] {{CompatGeckoDesktop("52.0")}}[2] |
{{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android Webview | Chrome for Android | Edge | 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}} |
[1] Touch events were implemented in Gecko 18.0, but removed again in 24.0 {{geckoRelease("24.0")}} on the desktop version of Firefox due to web compatibility issues ({{bug(888304)}}).
[2] As of Gecko 52.0, touch events support has been fixed and reenabled in Windows desktop platforms.