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.
The tranche of Jeffrey Epstein emails and files released on January 30th tie the infamous…
The post Imagine’s Steve Reynolds Discusses Impact Of Pixel Power Acquisition appeared first on TV…
Anil Bhardwaj Broadcast standards association ATSC has named Indian broadcasting executive Anil Bhardwaj as director of…
Telestream is expanding practical AI enhancements across its Vantage, Vantage Cloud, EDC, Stanza and Qualify product lines to unify operations across on-premises,…
Riedel Communications today announced that Fondazione Teatro alla Scala has deployed a comprehensive wireless intercom…
At the 2026 NAB Show in Las Vegas, April 18-22, Netgear will highlight its new…
This website uses cookies.