npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swipe-listener

v1.3.0

Published

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

Downloads

11,596

Readme

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