Coder Social home page Coder Social logo

tri613 / swipe-it Goto Github PK

View Code? Open in Web Editor NEW
13.0 4.0 2.0 45 KB

An event listener for swiping gestures with vanilla js.

Home Page: https://plnkr.co/edit/FqCGSVcsXL3vFOruUDCL?p=preview

License: MIT License

JavaScript 100.00%
swipe swipe-gestures event-listener javascript vanilla-js

swipe-it's Introduction

Swipe-it

An event listener for swiping gestures with pure javascript only, no dependency needed. Touch devices only.

Also supports mouse now! -- 2016/09/23

Here's a simple plunker demo: https://plnkr.co/edit/FqCGSVcsXL3vFOruUDCL?p=preview

Usage

  • include dist/swipe-it.js or dist/swipe-it.min.js into your file.
  • then use it like this:
const mySwipeIt = new SwipeIt('your_element_selector_here' [,options]);

mySwipeIt
.on('swipeLeft', function(event) {
    // your event handler here
    const swipeStartPoint = event.swipe.start;
    const swipeEndPoint = event.swipe.end;
    const swipeDistance = event.swipe.distance; //the swipe distance (px)
    console.log(`mySwipeIt is on swipeLeft with ${swipeDistance} px!`);
})
.on('swipeRight', function(event) {
    // your event handler here
    console.log('mySwipeIt is on swipeRight!');
});
  • That's it!

Options [object]

All options are optionable.

  • mouseEvent [boolean] | true

    Whether using mouse event or not.

  • minDistance [int] | 30

    The minimal distance (px) between the start point and end point for triggering swipe events.

example

const swipeWithOptions = new SwipeIt('#with-options', {
    mouseEvent: false,
    minDistance: 50
});

Events

  • swipe (for all directions)
  • swipeLeft
  • swipeRight
  • swipeUp
  • swipeDown

Extra event data

Every swipe event (except swipe) contains a swipe data object which could be access by event.swipe.

  • event.swipe.distance

    The distance between the start point and end point

  • event.swipe.start

    The start point position. For vertical swipe events, this would be the event.clientY value; for horizontal swipe events, this would be the event.clientX value of the start point.

  • event.swipe.end

    Just like event.swipe.start instead of this is the end point position.

Bugfix

  • 2017/03/17: Swipe events are now dispatched to it's listener instead of event.target.
  • 2017/03/22: Add support to IE11.

swipe-it's People

Contributors

tri613 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

Forkers

41yn14 halkim44

swipe-it's Issues

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.