Socket
Socket
Sign inDemoInstall

swiped-events

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    swiped-events

A 1k script that adds swipe events to the DOM for touch enabled devices


Version published
Weekly downloads
5.8K
increased by6.74%
Maintainers
1
Install size
204 kB
Created
Weekly downloads
 

Readme

Source

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
  fingers: 1,           // number of fingers used
  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:

AttributeDescriptionTypeDefault
data-swipe-thresholdNumber of pixels or percent of viewport-axis a user must move before swipe firesinteger20
data-swipe-unitUnit of the threshold (can be either "px", "vh" or "vw")string"px"
data-swipe-timeoutNumber of milliseconds from touchstart to touchendinteger500
data-swipe-ignoreIf true, swipe events on this element are ignoredbooleanfalse

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

Keywords

FAQs

Last updated on 17 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc