Tiny JavaScript Library for Touch and Mouse Swipe Detection – SwipeTracker
It can be used in your web apps to detect when a user swipes on a specific DOM element and dispatch custom events that you can listen for.
1. Install SwipeTracker and import it into your project.
# NPM $ npm install swipe-tracker
import SwipeTracker from 'swipe-tracker';
2. Or directly load the UMD version in your HTML document.
<script src="/dist/swipe-tracker.umd.min.js"></script>
3. Initialize SwipeTracker with options:
50.100.swipemove events begin to fire. This prevents event noise from minor pointer movements. The default is 5.true, enables swipe detection from mouse input in addition to touch. The default is false.'x', 'y', or 'both'. This restricts swipe detection to a specific axis, which is useful for horizontal sliders or vertical scrollers. The default is 'both'.const el = document.querySelector('.target-element');
const tracker = new SwipeTracker({
container: el
});
tracker.init(); 4. SwipeTracker dispatches custom events that you can listen for using standard addEventListener methods:
tracker.addEventListener('swipeleft', (e) => {
eventName.textContent = 'Swipe Left';
});
tracker.addEventListener('swiperight', (e) => {
eventName.textContent = 'Swipe Right';
});
tracker.addEventListener('swipeup', (e) => {
eventName.textContent = 'Swipe Up';
});
tracker.addEventListener('swipedown', (e) => {
eventName.textContent = 'Swipe Down';
}); Each event includes detailed information in the event.detail object containing coordinates, distances, direction, and pointer type.
5. You can also configure options through HTML data attributes:
<div class="target-element" data-swipe data-swipe-threshold="60" data-swipe-mouse="true" data-swipe-lock="x"> </div>
6. API methods.
new SwipeTracker(options): Creates a new instance.instance.init(): Attaches the event listeners.instance.destroy(): Cleans up and removes event listeners.SwipeTracker.getInstance(element): A static method to get the instance associated with an element.SwipeTracker.initAll(selector?): A handy static method to initialize all elements with data-swipe (or a custom selector).SwipeTracker.destroyAll(): Destroys all active instances on the page.The post Tiny JavaScript Library for Touch and Mouse Swipe Detection – SwipeTracker appeared first on CSS Script.
On March 26, a female store clerk at Pokémon Center Mega Tokyo in Japan was…
ABILENE, Texas (KTAB/KRBC) – Friends and family are remembering 19-year-old Juan Diego Mendez, an Abilene…
BIG COUNTRY, Texas (KTAB/KRBC) - Our local 2026 Nexstar Media Group "Remarkable Women" contest winner…
March 26, 2026 Every corner at 85th Street and Louise Avenue indicates development likely is…
MF.xyz – Cloudflare customer – (United States) Investment firms operating in blockchain and emerging technology…
A catchy tribute to mid-century Soviet hipsters popped up a few years back in a…
This website uses cookies.