javascript
69 lines · 10 steps
Building a swipe-to-dismiss gesture in JS
How touch events track a horizontal drag and either fling an element away or snap it back.
Explained by
highlit
1const SWIPE_THRESHOLD = 80;
2const MAX_TRANSLATE = 120;
3
4export function attachSwipeToDismiss(element, onDismiss) {
5 let startX = 0;
6 let startY = 0;
7 let currentX = 0;
8 let tracking = false;
9 let horizontal = false;
10
11 function onTouchStart(e) {
12 const touch = e.touches[0];
13 startX = touch.clientX;
14 startY = touch.clientY;
15 currentX = 0;
16 tracking = true;
17 horizontal = false;
18 element.style.transition = '';
19 }
20
21 function onTouchMove(e) {
22 if (!tracking) return;
23 const touch = e.touches[0];
24 const dx = touch.clientX - startX;
25 const dy = touch.clientY - startY;
26
27 if (!horizontal) {
28 if (Math.abs(dx) < 10 && Math.abs(dy) < 10) return;
29 horizontal = Math.abs(dx) > Math.abs(dy);
30 if (!horizontal) {
31 tracking = false;
32 return;
33 }
34 }
35
36 e.preventDefault();
37 currentX = Math.max(-MAX_TRANSLATE, Math.min(MAX_TRANSLATE, dx));
38 element.style.transform = `translateX(${currentX}px)`;
39 element.style.opacity = String(1 - Math.abs(currentX) / (MAX_TRANSLATE * 1.5));
40 }
41
42 function onTouchEnd() {
43 if (!tracking) return;
44 tracking = false;
45 element.style.transition = 'transform 0.25s ease, opacity 0.25s ease';
46
47 if (Math.abs(currentX) >= SWIPE_THRESHOLD) {
48 const direction = currentX > 0 ? 1 : -1;
49 element.style.transform = `translateX(${direction * window.innerWidth}px)`;
50 element.style.opacity = '0';
51 element.addEventListener('transitionend', () => onDismiss(element), { once: true });
52 } else {
53 element.style.transform = '';
54 element.style.opacity = '';
55 }
56 }
57
58 element.addEventListener('touchstart', onTouchStart, { passive: true });
59 element.addEventListener('touchmove', onTouchMove, { passive: false });
60 element.addEventListener('touchend', onTouchEnd);
61 element.addEventListener('touchcancel', onTouchEnd);
62
63 return () => {
64 element.removeEventListener('touchstart', onTouchStart);
65 element.removeEventListener('touchmove', onTouchMove);
66 element.removeEventListener('touchend', onTouchEnd);
67 element.removeEventListener('touchcancel', onTouchEnd);
68 };
69}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Detecting gesture direction early lets you claim horizontal swipes while leaving vertical scrolling untouched.
- 2Clamping the drag distance and toggling CSS transitions keeps the follow-the-finger feel separate from the snap-back animation.
- 3Returning a teardown function makes an event-heavy setup safe to unbind when the element goes away.
Related explainers
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
7 steps
javascript
function collapseConsecutiveLogs(lines, { keyFn = (l) => l.message } = {}) { const groups = []; for (const line of lines) {
Collapsing consecutive log lines in JavaScript
grouping
run-length-encoding
data-transformation
Intermediate
7 steps
javascript
const express = require('express'); const crypto = require('crypto'); const router = express.Router();
Optimistic locking with ETags in Express
optimistic-locking
etag
conditional-requests
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-swipe-to-dismiss-gesture-in-js-explained-javascript-fd6c/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.