Swiper—a really basic left/right swiping event handler that tries to be helpful and not interfere with up/down scrolling.
Using Swiper is really simple: call the swiper
function, passing an element and a callback.
When the element is swiped your callback will be passed the target
and the direction.
var elem = document.body;
swiper(elem, function (e) {
if (e.direction === 'left') {
// Do something when swiped in the leftwards direction
} else {
// Do something when swiped in the rightwards direction
}
});
-
swiper(elem, callback)
— The only available function, will listen for horizontal swipes on the element and execute the callback.Callback signature:
callback(e)
— Only a single argument is passed, like event listeners, it contains two pieces of information:e.target
— the thing that was swipede.direction
— the direction of the swipe:'left'
or'right'
If you capture the response from the swiper()
function you get access to a few other things:
var swipe = swiper(document.body, function (e) { });
-
.off()
— Will disabled Swiper and stop listening. There’s also.noSwiping()
for those cool people who want to writeswiper.noSwiping()
. -
.directions
— An object that has theleft
andright
entries in it if you don’t want to write the actual strings in your if statements. You could do something like this:var swipe = swiper(document.body, function (e) { if (e.direction === swipe.directions.left) console.log('Swiped left'); });
© 2017 Thomas J Bradley
Licensed under the MIT License