- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
CSS touch-action Customizing Touch Interactions
Discover available options like auto, none, pan-x, and pan-y.
Understanding the touch-action Property
The touch-action property in CSS controls how elements respond to touch gestures on touchscreen devices. It allows developers to customize touch behaviors, making web applications more interactive and responsive.
Why is it Important?
By using the touch-action property, you can ensure that your web application provides a consistent user experience across different browsers and devices. This property is crucial for managing touch gestures, whether through the browser’s default behavior or custom JavaScript code.
Standards and Specifications
-
Compatibility Standard:
- Guidelines for implementing
touch-actionto ensure compatibility with various browsers and devices. - Compatibility Standard - touch-action
- Guidelines for implementing
-
Pointer Events:
- Outlines how to use
touch-actionwith Pointer Events to manage touch interactions. - Pointer Events - The touch-action CSS property
- Outlines how to use
Browser Compatibility
When using touch-action, consider browser compatibility to ensure a consistent experience across different platforms.
Supported Browsers
- Google Chrome: Supported since version 36.
- Microsoft Edge: Supported since version 12.
- Firefox: Supported since version 52.
- Opera: Supported since version 23.
- Safari: Supported since version 13.
Compatibility Notes
- Internet Explorer: Not supported. Use polyfills or alternative methods.
- Mobile Browsers: Generally well-supported. Test on various devices and browsers.
- Future-Proofing: Stay updated with the latest browser releases and specifications. Test regularly across different browsers and versions.
Example of Browser Compatibility Check
To ensure your web application handles touch interactions correctly, you can use a simple compatibility check.
HTML
<div id="compatibility-check">Check Browser Compatibility</div>CSS
#compatibility-check { width: 300px; height: 300px; background-color: lightgray; touch-action: none; /* Disable default browser handling */}JavaScript
const compatibilityCheck = document.getElementById('compatibility-check');
if ('touchAction' in document.documentElement.style) { console.log('touch-action property is supported.');} else { console.log('touch-action property is not supported.');}
compatibilityCheck.addEventListener('touchstart', (event) => { console.log('Touch start:', event);});
compatibilityCheck.addEventListener('touchmove', (event) => { console.log('Touch move:', event);});
compatibilityCheck.addEventListener('touchend', (event) => { console.log('Touch end:', event);});
compatibilityCheck.addEventListener('touchcancel', (event) => { console.log('Touch cancel:', event);});Practical Examples
Here are some practical examples of how to use the touch-action property in different scenarios.
Disabling All Gestures
Disabling all touch gestures is common for elements that provide their own dragging and zooming behavior, such as a map or game surface.
HTML
<div id="map">Map Area</div>CSS
#map { height: 150vh; width: 70vw; background: linear-gradient(blue, green); touch-action: none;}Result
By setting touch-action: none, you disable all browser-managed touch gestures on the element. This allows you to handle all touch interactions with custom JavaScript code.
Enabling Horizontal Panning
To enable only horizontal panning gestures on an element, use the pan-x value.
HTML
<div id="horizontal-scroll">Scroll Horizontally</div>CSS
#horizontal-scroll { width: 300px; height: 300px; background-color: lightblue; overflow: auto; touch-action: pan-x;}Result
By setting touch-action: pan-x, you enable horizontal panning gestures on the element while disabling vertical panning and pinch-zooming.
Enabling Vertical Panning
To enable only vertical panning gestures, use the pan-y value.
HTML
<div id="vertical-scroll">Scroll Vertically</div>CSS
#vertical-scroll { width: 300px; height: 300px; background-color: lightgreen; overflow: auto; touch-action: pan-y;}Result
By setting touch-action: pan-y, you enable vertical panning gestures on the element while disabling horizontal panning and pinch-zooming.
Enabling Pinch-Zoom
To enable pinch-zoom gestures on an element, use the pinch-zoom value.
HTML
<div id="pinch-zoom-area">Pinch to Zoom</div>CSS
#pinch-zoom-area { width: 300px; height: 300px; background-color: lightcoral; touch-action: pinch-zoom;}Result
By setting touch-action: pinch-zoom, you enable multi-finger panning and zooming gestures on the element while disabling single-finger panning.
Combining Gestures
You can combine multiple gestures by using a combination of values. For example, you can enable both horizontal and vertical panning as well as pinch-zooming.
HTML
<div id="combined-gestures">Combined Gestures</div>CSS
#combined-gestures { width: 300px; height: 300px; background-color: lightyellow; touch-action: pan-x pan-y pinch-zoom;}Result
By setting touch-action: pan-x pan-y pinch-zoom, you enable horizontal and vertical panning as well as pinch-zooming on the element.
Using manipulation
The manipulation value is an alias for pan-x pan-y pinch-zoom and is useful for reducing the delay in generating click events by disabling additional non-standard gestures such as double-tap to zoom.
HTML
<div id="manipulation-area">Manipulation Area</div>CSS
#manipulation-area { width: 300px; height: 300px; background-color: lightseagreen; touch-action: manipulation;}Result
By setting touch-action: manipulation, you enable panning and pinch-zoom gestures while disabling additional non-standard gestures.
Summary
The touch-action property is a powerful tool for customizing touch interactions on web pages. While it is widely supported by modern browsers, it’s essential to consider browser compatibility to ensure a consistent user experience. By testing your implementation on various browsers and devices, you can identify and address any compatibility issues that may arise.
See Also
For further exploration and understanding of touch interactions and related CSS properties, consider the following resources:
-
[
pointer-events]WebsiteUrl- Learn about the
pointer-eventsCSS property, which controls when an element can be the target of a pointer event.
- Learn about the
-
[Pointer Events]WebsiteUrl
- Dive deeper into Pointer Events, a unified way to handle input from various devices like touchscreens, mice, and styluses.
-
WebKit Blog: More Responsive Tapping on iOS
- Discover insights and tips on improving the responsiveness of touch interactions on iOS devices.
-
Google Developers Blog: Making touch scrolling fast by default
- Explore how Google is working to make touch scrolling faster and more responsive by default.
-
[Scroll Snap]WebsiteUrl
- Learn about the CSS Scroll Snap module, which allows you to create smooth scrolling experiences with precise snap points.
By using these resources, you can gain a comprehensive understanding of touch interactions, pointer events, and related CSS properties, helping you create more responsive and engaging web experiences.
สร้างเว็บไซต์ 1 เว็บ ต้องใช้งบเท่าไหร่? เจาะลึกทุกองค์ประกอบ website development cost อยากสร้างเว็บไซต์แต่ไม่มั่นใจในเรื่องของงบประมาณ อ่านสรุปเจาะลึกตั้งแต่ดีไซน์, ฟังก์ชัน และการดูแล พร้อมตัวอย่างงบจริงจาก Till it’s done ที่แผนชัด งบไม่บานปลายแน่นอน
Next.js สอน 14 ขั้นตอนเบื้องต้น: สร้างโปรเจกต์แรกใน 30 นาที เริ่มต้นกับ Next.js ใน 14 ขั้นตอนเพียงแค่ 30 นาที พร้อม SSR/SSG และ API Routes ด้วยตัวอย่างโค้ดง่าย ๆ อ่านต่อเพื่อสร้างโปรเจ็กต์แรกได้ทันทีที่นี่
วิธีสมัคร Apple Developer Account เพื่อนำแอปขึ้น App Store ทีละขั้นตอน อยากปล่อยแอปบน App Store ระดับโลก มาอ่านคู่มือสมัคร Apple Developer Account พร้อมเคล็ดลับ TestFlight และวิธีอัปโหลดที่ง่ายในบทความเดียวนี้ได้เลย
TypeScript Interface คืออะไร? อธิบายพร้อมวิธีใช้และข้อแตกต่างจาก Type เรียนรู้วิธีใช้ TypeScript Interface เพื่อสร้างโครงสร้างข้อมูลที่ปลอดภัยและเข้าใจง่าย พร้อมเปรียบเทียบข้อดีข้อแตกต่างกับ Type ที่คุณต้องรู้ ถูกรวมเอาไว้ในบทความนี้แล้ว
Material-UI (MUI) คืออะไร อยากสร้าง UI สวยงามและเป็นมืออาชีพในเวลาอันรวดเร็วใช่ไหม มาทำความรู้จักกับ Material-UI (MUI) ที่ช่วยให้คุณพัฒนาแอปพลิเคชันบน React ได้ง่ายและดูดีในทุกอุปกรณ์
เปรียบเทียบ 3 วิธีติดตั้ง install node js บน Ubuntu: NVM vs NodeSource vs Official Repo แบบไหนดีที่สุด? เรียนรู้วิธีติดตั้ง Node.js บน Ubuntu ด้วย NVM, NodeSource หรือ Official Repo เลือกวิธีที่เหมาะกับความต้องการของคุณ พร้อมเปรียบเทียบ เพื่อการพัฒนาที่มีประสิทธิภาพ! พูดคุยกับซีอีโอ
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.