2.4.1 • Published 3 years ago
vanilla-swipe v2.4.1
vanilla-swipe
Tiny vanilla JS library to detect swipe direction.
👉 Live demo.
Types
type ConstructorProps = {
element?: HTMLElement | null;
target?: HTMLElement | null;
delta?: number | 10;
directionDelta?: number | 0;
rotationAngle?: number | 0;
mouseTrackingEnabled?: boolean | false;
touchTrackingEnabled?: boolean | true;
preventDefaultTouchmoveEvent?: boolean | false;
preventTrackingOnMouseleave?: boolean | false;
onSwipeStart?: EventHandler;
onSwiping?: EventHandler;
onSwiped?: EventHandler;
onTap?: EventHandler;
};
type EventHandler = {
(e: Event, data: EventData): void;
};
type EventData = {
absX: number;
absY: number;
deltaX: number;
deltaY: number;
directionX: 'LEFT' | 'RIGHT' | 'NONE';
directionY: 'TOP' | 'BOTTOM' | 'NONE';
duration: number; // ms
velocity: number; // (px/ms)
};Props
element- target event triggertarget- additionally target event trigger, if specified with the element, will be used by all handlers to trigger the actiondelta- minimal distance inpxbefore a swipe startsdirectionDelta- minimum distance inpxrequired for the direction to be reversed while swiping.rotationAngle- rotation anglemouseTrackingEnabled- enable mouse trackingtouchTrackingEnabled- enable touch trackingpreventDefaultTouchmoveEvent- prevent default touch events while touchingpreventTrackingOnMouseleave- triggeredonSwipedcallback when the event loses the element's focusonSwipeStart- triggered on swipe start (if thedeltais passed)onSwiping- triggered during swipeonSwiped- triggered after swipeonTap- triggered when the swipe value is less than the minimum distance (delta)
Methods
init(): voidupdate(options: ConstructorProps): voiddestroy(): void- static
isTouchEventsSupported(): boolean
Install
npm install vanilla-swipeExamples
import VanillaSwipe from 'vanilla-swipe';
const isTouchEventsSupported = VanillaSwipe.isTouchEventsSupported();
const VS = new VanillaSwipe({
element: document.getElementById('some-id'),
onSwiping: handler,
onSwiped: handler,
mouseTrackingEnabled: true,
});
VS.init();
function handler() {
console.log(...arguments); // -> Event, EventData
}Run project
npm i
npm startTests
npm testCoverage
npm run test:coverage