Course contentsShow
← Web Development
Lesson 735 of 2,98213. DOM EventsFree lesson

Touch Events: touchstart, touchmove, touchend

Handle touch interactions on mobile devices with the Touch Events API and multiple touch points.

Touch Events: touchstart, touchmove, touchend

You'll learn how to detect and respond to touch interactions on mobile devices using specialized touch events.

What Are Touch Events?

Touch events are specialized DOM events designed specifically for touch-enabled devices like smartphones and tablets. Unlike mouse events that track a single cursor, touch events can handle multiple simultaneous touch points (multi-touch), making them essential for modern mobile web experiences.

The three core touch events mirror the lifecycle of a touch interaction:

  • touchstart: Fires when a finger first touches the screen
  • touchmove: Fires repeatedly as a finger moves across the screen
  • touchend: Fires when a finger lifts off the screen

How Touch Events Work

When you add a touch event listener, you receive a TouchEvent object that contains information about all active touch points through its touches, targetTouches, and changedTouches properties. Each touch point includes coordinates (clientX, clientY) and an identifier.

const box = document.getElementById('myBox');

box.addEventListener('touchstart', function(event) {
  console.log('Touch started!');
  // Access first touch point
  const touch = event.touches[0];
  console.log(`Position: ${touch.clientX}, ${touch.clientY}`);
});

box.addEventListener('touchmove', function(event) {
  // Often want to prevent scrolling
  event.preventDefault();
  console.log('Touch moving...');
});

box.addEventListener('touchend', function(event) {
  console.log('Touch ended!');
});

Think of It Like This

Imagine tracking footprints on a beach. touchstart is when someone steps onto the sand, touchmove is their path as they walk, and touchend is when they lift their foot. Unlike a single set of footprints (mouse), multiple people can walk simultaneously (multi-touch).

Key Takeaway: Touch events (touchstart, touchmove, touchend) enable responsive mobile interactions by detecting finger touches and movements, supporting multiple simultaneous touch points for gestures like pinch-to-zoom.