The HTML ontouchmove event
The HTML ontouchmove attribute runs JavaScript when one or more touch points move along the touch surface. It is an inline handler for the touchmove event; in modern code prefer addEventListener('touchmove', …).
Overview
The ontouchmove event attribute runs JavaScript when a touch moves on the element. In JavaScript the event itself is named touchmove — drop the on prefix when you call addEventListener.
It is one of the Touch Events. The handler receives a TouchEvent whose touches list describes every active touch point, which is what makes multi-touch gestures possible. For most needs, though, the unified pointer events are now preferred over touch-specific handlers.
You can wire this up with the inline ontouchmove HTML attribute, but the modern, recommended approach is element.addEventListener('touchmove', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.
Syntax
<element ontouchmove="handler()">…</element>
element.addEventListener('touchmove', handler);
Best practices
- Prefer
element.addEventListener('touchmove', handler)over the inlineontouchmoveattribute — it separates behavior from markup and allows multiple handlers. - Prefer the unified pointer events unless you specifically need multi-touch details.
- Iterate
event.touches(orchangedTouches) to handle each touch point. - Call
preventDefault()when you need to stop the browser's default scrolling or zooming.
Frequently asked questions
What is the ontouchmove event?
touchmove.Should I use touch events or pointer events?
How do I access the touch points?
event.touches list (or event.changedTouches) from the TouchEvent.Should I use the ontouchmove attribute or addEventListener?
addEventListener('touchmove', …) in JavaScript. The inline ontouchmove attribute works but mixes behavior into the markup and allows only one handler per element.