CSS属性 touch-action 用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作(比如浏览器自带的划动、缩放等)。

/* Keyword values */
touch-action: auto;
touch-action: none;
touch-action: pan-x;
touch-action: pan-left;
touch-action: pan-right;
touch-action: pan-y;
touch-action: pan-up;
touch-action: pan-down;
touch-action: pinch-zoom;
touch-action: manipulation;

/* Global values */
touch-action: inherit;
touch-action: initial;
touch-action: unset;

初始值auto
适用元素all elements except: non-replaced inline elements, table rows, row groups, table columns, and column groups
是否是继承属性
适用媒体visual
计算值as specified
Animation typediscrete
正规顺序the unique non-ambiguous order defined by the formal grammar

默认情况下,平移(滚动)和缩放手势由浏览器专门处理。 使用 Pointer_events 的应用程序将在浏览器开始处理触摸手势时收到一个 pointercancel 事件。 通过明确指定浏览器应该处理哪些手势,应用程序可以在 pointermove 和 pointerup 监听器中为其余的手势提供自己的行为。 使用 Touch_events 的应用程序通过调用 preventDefault() 禁用浏览器处理手势,但也应使用触摸操作确保浏览器在调用任何事件侦听器之前,了解应用程序的意图。

当手势开始时,浏览器与触摸的元素及其所有祖先的触摸动作值相交直到一个实现手势(换句话说,第一个包含滚动元素)的触摸动作值。 这意味着在实践中,触摸动作通常仅适用于具有某些自定义行为的单个元素,而无需在该元素的任何后代上明确指定触摸动作。 手势开始之后,触摸动作值的更改将不会对当前手势的行为产生任何影响。

语法

touch-action 属性可以被指定为:

auto
当触控事件发生在元素上时,由浏览器来决定进行哪些操作,比如对viewport进行平滑、缩放等。
none
当触控事件发生在元素上时,不进行任何操作。
pan-x
启用单指水平平移手势。可以与 pan-y 、pan-up、pan-down 和/或 pinch-zoom 组合使用。
pan-y
启用单指垂直平移手势。可以与 pan-x 、pan-left 、pan-right 和/或 pinch-zoom 组合使用。
manipulation
浏览器只允许进行滚动和持续缩放操作。任何其它被auto值支持的行为不被支持。启用平移和缩小缩放手势,但禁用其他非标准手势,例如双击以进行缩放。 禁用双击可缩放功能可减少浏览器在用户点击屏幕时延迟生成点击事件的需要。 这是“pan-x pan-y pinch-zoom”(为了兼容性本身仍然有效)的别名。
pan-leftpan-right,pan-up,pan-down 
启用以指定方向滚动开始的单指手势。 一旦滚动开始,方向可能仍然相反。 请注意,滚动“向上”(pan-up)意味着用户正在将其手指向下拖动到屏幕表面上,同样 pan-left 表示用户将其手指向右拖动。 多个方向可以组合,除非有更简单的表示(例如,“pan-left pan-right”无效,因为“pan-x”更简单,而“pan-left pan-down”有效)。
pinch-zoom
启用多手指平移和缩放页面。 这可以与任何平移值组合。

正式语法

auto | none | [ [ pan-x | pan-left | pan-right ] || [ pan-y | pan-up | pan-down ] || pinch-zoom ] | manipulation

示例

最常见的用法是禁用元素(及其不可滚动的后代)上的所有手势,以使用自己提供的拖放和缩放行为(如地图或游戏表面)。

#map {
  touch-action: none;
}

另一种常见的模式是使用指针事件处理水平平移的图像轮播,但不想干扰网页的垂直滚动或缩放。

.image-carousel {
  width: 100%;
  height: 150px;
  touch-action: pan-y pinch-zoom;
}

触摸动作也经常用于完全解决由支持双击缩放手势引起的点击事件的延迟。

html {
  touch-action: manipulation;
}

Specifications

Specification Status Comment
Compatibility Standard
touch-action
Living Standard Added pinch-zoom property value.
Pointer Events – Level 2
touch-action
Working Draft Added pan-leftpan-rightpan-uppan-downproperty values.
Pointer Events
touch-action
Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 36Edge Full support 12Firefox Full support 52
Notes
Full support 52
Notes
Notes Not applicable to Firefox platforms that support neither pointer nor touch events.
Full support 29
Notes Disabled
Notes Not applicable to Firefox platforms that support neither pointer nor touch events.
Disabled From version 29: this feature is behind the layout.css.touch_action.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
Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera Full support 23Safari No support No
Notes
No support No
Notes
Notes See WebKit bug 133112.
WebView Android Full support 37Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 52
Full support 52
Full support 29
Disabled
Disabled From version 29: this feature is behind the layout.css.touch_action.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support YesSafari iOS No support No
Notes
No support No
Notes
Notes See WebKit bug 133112.
Samsung Internet Android Full support Yes
manipulationChrome Full support 36Edge Full support 12Firefox Full support 52
Notes
Full support 52
Notes
Notes Not applicable to Firefox platforms that support neither pointer nor touch events.
Full support 29
Notes Disabled
Notes Not applicable to Firefox platforms that support neither pointer nor touch events.
Disabled From version 29: this feature is behind the layout.css.touch_action.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
Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera Full support 23Safari No support NoWebView Android Full support 37Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 52
Full support 52
Full support 29
Disabled
Disabled From version 29: this feature is behind the layout.css.touch_action.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support YesSafari iOS Full support 9.1Samsung Internet Android Full support Yes
double-tap-zoom
Non-standard
Chrome No support NoEdge Full support 12Firefox No support NoIE Full support 11
Full support 11
Full support 10
Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
pinch-zoomChrome Full support 56Edge Full support 12Firefox No support No
Notes
No support No
Notes
Notes See bug 1285685.
IE Full support 11
Full support 11
Full support 10
Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera Full support 43Safari No support NoWebView Android Full support 56Chrome Android Full support 56Edge Mobile Full support YesFirefox Android No support No
Notes
No support No
Notes
Notes See bug 1285685.
Opera Android Full support 43Safari iOS No support NoSamsung Internet Android Full support 6.0
pan-up, pan-down, pan-left and pan-rightChrome Full support 55Edge No support NoFirefox No support No
Notes
No support No
Notes
Notes See bug 1285685.
IE No support NoOpera Full support 42Safari No support NoWebView Android Full support 55Chrome Android Full support 55Edge Mobile No support NoFirefox Android No support No
Notes
No support No
Notes
Notes See bug 1285685.
Opera Android Full support 42Safari iOS No support NoSamsung Internet Android Full support 6.0

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

相关链接

文档标签和贡献者

此页面的贡献者: xyy94813, Katherina-Miao, Ende93, teoli, TooBug
最后编辑者: xyy94813,