Coder Social home page Coder Social logo

john-doherty / swiped-events Goto Github PK

View Code? Open in Web Editor NEW
575.0 15.0 176.0 820 KB

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

License: MIT License

JavaScript 72.99% HTML 27.01%
javascript hybrid-apps cordova html5 touch vanilla-js no-dependencies swipe

swiped-events's Introduction

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:

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

swiped-events's People

Contributors

avrahamcool avatar btjones avatar collimarco avatar endel avatar enkrod avatar ghiculescu avatar john-doherty avatar martincivan avatar wasabithumb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiped-events's Issues

Is event isset?

Hello! Please guide me how can I determine if this event exists?

remove Dependencies

first of all: yeahaa, npm support! makes things so much easier. thank you, I really like your lib.

but could you just give it a thought to remove the gulp-sizereport package? not really usefull for production ...

thanks again, cheers

Where to place data attributes for document

I use this code:

document.addEventListener(...);

Where should I put the configuration / data attributes? On html, on the body, or where?

The documentation only show the case of a div, not the general case of document.

Better feature detection for CustomEvent

The if-statement that detects if the CustomEvent constructor is supported is too broad and gets executed in modern browsers. Loads of browsers (link to MDN) still support the (deprecated) initCustomEvent function.

Solution: replace

if ('initCustomEvent' in document.createEvent('CustomEvent')) {

with

if (typeof window.CustomEvent !== "function" ) {

console.log statement pollutes developer console

I am using this script on a production level website, I don't think logging every swipe is needed all the time.
This line just does that:

if (console && console.log) console.log(eventType + ' fired on ' + startEl.tagName);

Also, I am open if there is any chance of contributing.

How can I prevent page reload or refresh

I tried using the swiped-down event listener but that doesn't seem to work.

Plus I wouldn't like to prevent the whole page from scrolling up.

document.addEventListener("swiped-down", (e) => { e.preventDefault(); console.log("Swiped down") //But this stops the page from scrolling back to the top })

That aside, is there a way to check for the swiped down event to prevent the page from reloading? @john-doherty

data-swipe-ignore doesn't work properly

I like to load all my js at the bottom of the page like most people, but data-swipe-ignore is overwritten by the default false value. Below code only works if the js library is loaded before the div.

How can I get the ignore to work when the library is loaded last?

<div data-swipe-ignore="true">
     Swipe area
</div>

Mouse simulation

Suggestion: It would be good to have a mouse swipe simulation, so it can be tested directly in the desktop browser.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.