An event listener for swiping gestures with pure javascript only, no dependency needed.
Touch devices only.
Also supports mouse now! -- 2016/09/23
Here's a simple plunker demo: https://plnkr.co/edit/FqCGSVcsXL3vFOruUDCL?p=preview
- include
dist/swipe-it.jsordist/swipe-it.min.jsinto your file. - then use it like this:
const mySwipeIt = new SwipeIt('your_element_selector_here' [,options]);
mySwipeIt
.on('swipeLeft', function(event) {
// your event handler here
const swipeStartPoint = event.swipe.start;
const swipeEndPoint = event.swipe.end;
const swipeDistance = event.swipe.distance; //the swipe distance (px)
console.log(`mySwipeIt is on swipeLeft with ${swipeDistance} px!`);
})
.on('swipeRight', function(event) {
// your event handler here
console.log('mySwipeIt is on swipeRight!');
});- That's it!
All options are optionable.
-
mouseEvent [boolean] |
trueWhether using mouse event or not.
-
minDistance [int] |
30The minimal distance (px) between the start point and end point for triggering swipe events.
const swipeWithOptions = new SwipeIt('#with-options', {
mouseEvent: false,
minDistance: 50
});- swipe (for all directions)
- swipeLeft
- swipeRight
- swipeUp
- swipeDown
Every swipe event (except swipe) contains a swipe data object which could be access by event.swipe.
-
event.swipe.distanceThe distance between the start point and end point
-
event.swipe.startThe start point position. For vertical swipe events, this would be the
event.clientYvalue; for horizontal swipe events, this would be theevent.clientXvalue of the start point. -
event.swipe.endJust like
event.swipe.startinstead of this is the end point position.
- 2017/03/17: Swipe events are now dispatched to it's listener instead of
event.target. - 2017/03/22: Add support to IE11.