All Projects → umanghome → Swipe Listener

umanghome / Swipe Listener

Licence: mit
Zero-dependency, minimal swipe-gesture listener for the web.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Swipe Listener

gestures
A library for normalized events and gesture for desktop and mobile.
Stars: ✭ 31 (-54.41%)
Mutual labels:  touch, swipe, gestures
React Native Swipe Gestures
4-directional swipe gestures for react-native
Stars: ✭ 471 (+592.65%)
Mutual labels:  swipe, gestures, touch
React Swipeable
React swipe event handler hook
Stars: ✭ 1,348 (+1882.35%)
Mutual labels:  swipe, touch
Vuetify Swipeout
👆 A swipe out example built with Vue CLI 3 + Vuetify + Swiper.
Stars: ✭ 117 (+72.06%)
Mutual labels:  swipe, touch
Slip
Slip.js — UI library for manipulating lists via swipe and drag gestures
Stars: ✭ 2,421 (+3460.29%)
Mutual labels:  swipe, touch
Layerjs
layerJS: Javascript UI composition framework
Stars: ✭ 1,825 (+2583.82%)
Mutual labels:  swipe, touch
React Easy Swipe
Easy handler for common swipe operations
Stars: ✭ 85 (+25%)
Mutual labels:  swipe, touch
Slider
Touch swipe image slider/slideshow/gallery/carousel/banner mobile responsive bootstrap
Stars: ✭ 2,046 (+2908.82%)
Mutual labels:  swipe, touch
React Spring Lightbox
📷 A flexible image gallery lightbox with native-feeling touch gestures and buttery smooth animations, built with react-spring.
Stars: ✭ 76 (+11.76%)
Mutual labels:  gestures, touch
react-gallery-carousel
Mobile-friendly gallery carousel 🎠 with server side rendering, lazy loading, fullscreen, thumbnails, touch, mouse emulation, RTL, keyboard navigation and customisations.
Stars: ✭ 178 (+161.76%)
Mutual labels:  touch, swipe
Cupertino Pane
🎉📱Multi-functional panes and boards for next generation progressive applications
Stars: ✭ 267 (+292.65%)
Mutual labels:  swipe, touch
Swiped Events
Adds `swiped` events to the DOM in 0.7k of pure JavaScript
Stars: ✭ 249 (+266.18%)
Mutual labels:  swipe, touch
Any Touch
👋 手势库, 按需2kb~5kb, 兼容PC / 移动端
Stars: ✭ 567 (+733.82%)
Mutual labels:  swipe, touch
Swiper
Most modern mobile touch slider with hardware accelerated transitions
Stars: ✭ 29,519 (+43310.29%)
Mutual labels:  swipe, touch
Fullpage.js
fullPage plugin by Alvaro Trigo. Create full screen pages fast and simple
Stars: ✭ 32,974 (+48391.18%)
Mutual labels:  swipe
Jquery.touchslider
A jQuery plugin that makes it easy to create touch sliders.
Stars: ✭ 43 (-36.76%)
Mutual labels:  swipe
Hammer Slider
DISCONTINUED - HammerSlider touch is a lightweight infinite carousel plugin.
Stars: ✭ 21 (-69.12%)
Mutual labels:  touch
React Native Overlay Section
Overlay section like iOS Notification Center
Stars: ✭ 14 (-79.41%)
Mutual labels:  swipe
React Soft Slider
Simple, fast and impartial slider
Stars: ✭ 54 (-20.59%)
Mutual labels:  swipe
Inputsystem
An efficient and versatile input system for Unity.
Stars: ✭ 1,013 (+1389.71%)
Mutual labels:  touch

Swipe-Listener

npm version

Zero-dependency, minimal swipe-gesture listener for the web.


Demo

What

Swipe-listener is a very minimal library that allows listening for swipe gesture on literally any DOM element. Once invoked with a DOM element, simply listen for swipe event and determine the direction with the directions object.

Example Code

var container = document.querySelector('#container');
var listener = SwipeListener(container);
container.addEventListener('swipe', function (e) {
  var directions = e.detail.directions;
  var x = e.detail.x;
  var y = e.detail.y;

  if (directions.left) {
    console.log('Swiped left.');
  }

  if (directions.right) {
    console.log('Swiped right.');
  }

  if (directions.top) {
    console.log('Swiped top.');
  }

  if (directions.bottom) {
    console.log('Swiped bottom.');
  }

  if (directions.top && directions.right) {
    console.log('Swiped top-right.');
  }

  if (directions.top && directions.left) {
    console.log('Swiped top-left.');
  }

  if (directions.bottom && directions.right) {
    console.log('Swiped bottom-right.');
  }

  if (directions.bottom && directions.left) {
    console.log('Swiped bottom-left.');
  }

  console.log('Started horizontally at', x[0], 'and ended at', x[1]);
  console.log('Started vertically at', y[0], 'and ended at', y[1]);
});

Installation

Browser

<script src="path/to/swipe-listener.min.js" type="text/javascript"></script>
<script>
  var container = document.querySelector('#container');
  var listener = SwipeListener(container);
  container.addEventListener('swipe', function (e) {
    console.log(e.detail);
  });
</script>

Swipe-listener is also available from unpkg: https://unpkg.com/[email protected]/dist/swipe-listener.min.js

Installing using NPM

Install from NPM using npm i --save swipe-listener, then

import SwipeListener from 'swipe-listener';

OR

const SwipeListener = require('swipe-listener');

API

SwipeListener(element, options)

  • element DOM Element on which you want to enable swipe gesture tracking. This is the element on which you will be attacking the swipe event listener.
  • options [Optional] Configuration options (see below)

Listen for swipe event on the element passed. Access details using event.detail. For example, directions can be accessed using event.detail.directions. See events for more events.

Data passed to event.detail:

  • directions (Object)
    • top (Boolean) Signifies a top-swipe.
    • right (Boolean) Signifies a right-swipe.
    • bottom (Boolean) Signifies a bottom-swipe.
    • left (Boolean) Signifies a left-swipe.
  • x (Array) Array containing two elements: starting and ending x-coords.
  • y (Array) Array containing two elements: starting and ending y-coords.
  • touch (Boolean) Whether or not TouchEvent was used for this particular event.

Note that multiple directions can be true at one. In case of a top-left swipe, directions.top and directions.left will both be true.

Options

Key Description Default value
minHorizontal Minimum number of horizontal pixels travelled for the gesture to be considered as a left or right swipe. 10
minVertical Minimum number of vertical pixels travelled for the gesture to be considered as a top or bottom swipe. 10
deltaHorizontal Maximum difference between the rightmost pixel (right-swipe) or the leftmost pixel (left-swipe) travelled to and the pixel at which the gesture is released. 3
deltaVertical Maximum difference between the bottommost pixel (bottom-swipe) or the topmost pixel (top-swipe) travelled to and the pixel at which the gesture is released. 5
preventScroll Prevents page scrolling when swiping on the DOM element. Can also be specified as a function with the signature (event) => boolean false
lockAxis Enforces only one direction to be true instead of multiple. Selects the direction with the most travel. Is not enforced when the travel is equal. Example: for a top-left swipe, only one of top and left will be true instead of both. true
touch Whether to listen for swipes with touch events true
mouse Whether to listen for swipes with mouse events true

.off()

Turns off the swipe-listener on a given element.

Usage:

var listener = SwipeListener(myElem);
listener.off();

Events

swipe - Emitted once a swipe is performed.

Emitted once a swipe is completed.

event.detail contains

key type description
directions Object Object containing top, left, bottom, right keys. The directions in which the swipe is performed are set to true.
x Array Array of two items: the starting x-coordinate and the ending x-coordinate.
y Array Array of two items: the starting y-coordinate and the ending y-coordinate.
touch Boolean Whether or not TouchEvent was used for this particular event.

swiping - Emitted while a swipe is being performed.

Emitted multiple times during a single swipe.

event.detail contains

key type description
x Array Array of two items: the starting x-coordinate and the ending x-coordinate.
y Array Array of two items: the starting y-coordinate and the ending y-coordinate.
touch Boolean Whether or not TouchEvent was used for this particular event.

swiperelease - Emitted once the swipe is released/completed.

Emitted at the end of the swipe.

event.detail contains

key type description
x Array Array of two items: the starting x-coordinate and the ending x-coordinate.
y Array Array of two items: the starting y-coordinate and the ending y-coordinate.
touch Boolean Whether or not TouchEvent was used for this particular event.

swipecancel - Emitted if the swipe-distance did not meet minimum travel-distance.

Emitted at the end of the swipe.

event.detail contains

key type description
x Array Array of two items: the starting x-coordinate and the ending x-coordinate.
y Array Array of two items: the starting y-coordinate and the ending y-coordinate.
touch Boolean Whether or not TouchEvent was used for this particular event.

Misc

  • When lockAxis is false, swipes using the mouse might make multiple directions true even when the travel in a certain direction may not be much. You can work around this by setting lockAxis to true when the page is not being accessed from a touch-enabled device. Or, you can use event.detail.x and event.detail.y to calculate which direction has more travel and consider only that direction. Or, you can increase the values of minVertical and minHorizontal.
  • TouchEvent is not supported in IE and Edge. Unless you have polyfilled it into the page and it's available as TouchEvent, swipes made using touch will not be detected as touch swipes.

License

MIT License © Umang Galaiya

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].