vanilla-swipe
Tiny vanilla JS library to detect swipe direction.
Types
type ConstructorProps = {
element?: HTMLElement | null;
target?: HTMLElement | null;
delta?: number | 10;
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 = {
deltaX: number;
deltaY: number;
absX: number;
absY: number;
duration: number;
velocity: number;
};
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 in px
before a swipe startsrotationAngle
- rotation anglemouseTrackingEnabled
- enable mouse trackingtouchTrackingEnabled
- enable touch trackingpreventDefaultTouchmoveEvent
- prevent default touch events while touchingpreventTrackingOnMouseleave
- triggered onSwiped
callback when the event loses the element's focusonSwipeStart
- triggered on swipe start (if the delta
is passed)onSwiping
- triggered during swipeonSwiped
- triggered after swipeonTap
- triggered when the swipe value is less than the minimum distance (delta
)
Methods
init(): void
update(options: ConstructorProps): void
destroy(): void
Install
npm install vanilla-swipe
Examples
import VanillaSwipe from 'vanilla-swipe';
const VS = new VanillaSwipe({
element: document.getElementById('some-id'),
onSwiping: handler,
onSwiped: handler,
mouseTrackingEnabled: true,
});
VS.init();
function handler() {
console.log(...arguments);
}
Run project
npm i
npm start
Tests
npm test
Coverage
npm run test:coverage