• Stars
    star
    580
  • Rank 77,010 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Adds `swiped` events to the DOM in 1k of pure JavaScript

swiped-events

npm

A 1k script that adds swiped-left, swiped-right, swiped-up and swiped-down events to the DOM using CustomEvent and pure JS. Based on the StackOverflow thread Detect a finger swipe through JavaScript on the iPhone and Android

Usage

Add swiped-events.min.js to your page and start listening for swipe events. Events bubble, so you can addEventListener at document level, or on individual elements.

swiped

document.addEventListener('swiped', function(e) {
  console.log(e.target); // element that was swiped
  console.log(e.detail); // see event data below
  console.log(e.detail.dir); // swipe direction
});

swiped-left

document.addEventListener('swiped-left', function(e) {
  console.log(e.target); // element that was swiped
  console.log(e.detail); // see event data below
});

swiped-right

document.addEventListener('swiped-right', function(e) {
  console.log(e.target); // element that was swiped
  console.log(e.detail); // see event data below
});

swiped-up

document.addEventListener('swiped-up', function(e) {
  console.log(e.target); // element that was swiped
  console.log(e.detail); // see event data below
});

swiped-down

document.addEventListener('swiped-down', function(e) {
  console.log(e.target); // element that was swiped
  console.log(e.detail); // see event data below
});

event data

The following event data is included with every event and accessible via e.detail

{
  dir: 'up',            // swipe direction (up,down,left,right)
  touchType: 'direct',  // touch type (stylus,direct) - stylus=apple pencil and direct=finger
  xStart: 196,          // x coords of swipe start
  xEnd: 230,            // x coords of swipe end
  yStart: 196,          // y coords of swipe start
  yEnd: 4               // y coords of swipe end
}

Configure

You can optionally configure how swiped-events works using the following HTML attributes:

Attribute Description Type Default
data-swipe-threshold Number of pixels or percent of viewport-axis a user must move before swipe fires integer 20
data-swipe-unit Unit of the threshold (can be either "px", "vh" or "vw") string "px"
data-swipe-timeout Number of milliseconds from touchstart to touchend integer 500
data-swipe-ignore If true, swipe events on this element are ignored boolean false

If you do not provide any attributes, it assumes the following:

<div data-swipe-threshold="20"
     data-swipe-unit="px"
     data-swipe-timeout="500"
     data-swipe-ignore="false">
     Swiper, get swiping!
</div>

To set defaults application wide, set config attributes on a parent/topmost element:

<body data-swipe-threshold="50" data-swipe-unit="vw" data-swipe-timeout="250">
    <div>Swipe me</div>
    <div>or me</div>
</body>

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Development

The project includes everything needed to tweak, including a node webserver. Run the following, then visit http://localhost:8080 in your browser.

You can test on a desktop using Device Mode in Google Chrome.

git clone https://github.com/john-doherty/swiped-events
cd swiped-events
npm install
npm start

Update .min files

To create a new version of the minified swiped-events.min.js file from source, tweak the version number in package.json and run the following:

npm run build

Star the repo

If you find this useful, please star the repo. It helps me priorities which OSS issues to tackle first 🙌

History

For change-log, check releases.

License

Licensed under MIT License © John Doherty

More Repositories

1

long-press-event

Adds `long-press` event to the DOM in 1k of pure JavaScript
JavaScript
323
star
2

selenium-cucumber-js

Browser automation framework written in pure JavaScript using official selenium-webdriver and cucumber-js
JavaScript
120
star
3

mixpanel-lite

3k alternative to mixpanel-js with offline support for PWAs
JavaScript
53
star
4

node-iframe-replacement

An alternative to sticking that lovely web app into an <iframe> on a corp website
JavaScript
50
star
5

raspberry-pi-mjpeg-server

Node.js module providing Motion JPEG access to the Raspberry PI camera module to enable video streaming via HTTP
JavaScript
44
star
6

notch-detected-event

A cross-browser script to detect the existence of a device notch
JavaScript
20
star
7

pure-dialog

<pure-dialog> is a 3k, self-contained, pure JavaScript dialog
JavaScript
20
star
8

cordova-file-storage

An easy way to read/write files in Cordova
12
star
9

jsdoc-to-json-schema

Generate JSON schemas from JavaScript code comments using jsDoc @schema tag.
JavaScript
11
star
10

offline-fetch

1.2k script that adds offline support to fetch by storing responses in localStorage - ideal for PWAs
JavaScript
11
star
11

pureGrid

Lightweight JavaScript Grid/SpreadSheet component written in pure JavaScript
JavaScript
11
star
12

offline-geo-from-ip

Get geo location information from an IP address
JavaScript
7
star
13

express-batch-requests

Express middleware to process batch HTTP requests
JavaScript
6
star
14

express-url-breadcrumb

Express middleware that generates a breadcrumb from the current URL and makes it available in all views via breadcrumb variable
JavaScript
4
star
15

fetch-reply-with

Unit test fetch requests with ease
JavaScript
3
star
16

seo-tabs

Simple SEO friendly tabs (1k of pure JS)
JavaScript
3
star
17

express-seo-noslash

Remove trailing slashes from incoming urls
JavaScript
2
star
18

express-set-domain

Force an express app to use a particular domain
JavaScript
2
star
19

express-request-transfer

Transfer requests without redirects (aka Server.Transfer in C#)
JavaScript
2
star
20

isomorphic-mongo-objectid

pure JavaScript implementation of mongo ObjectID for client and server in 0.7k
JavaScript
1
star
21

jsdom-add-files

An easy way to add files to HTML file inputs in jsdom
JavaScript
1
star
22

is-port-busy

A simple node module to check if a TCP port is busy on the local machine.
JavaScript
1
star
23

simple-storage

1k auto-casting localStorage wrapper
JavaScript
1
star
24

is-port-free

A simple node module to check if a TCP port is free on the local machine.
JavaScript
1
star