A lightweight (3kb minified) library for handling touch events.
Install with NPM: npm install --save touchd
Or clone/download the repo and copy dist/touchd.js to your project folder.
Touchd doesn't have many methods (yet), but the ones currently available are as follows:
el(required) - Node Element element ID to apply Touchd events tooptions(optional) - an object to configure the following options:panThreshold(default:10) - distance to be moved before firingpaneventswipeTimeout(default:500) - timeout from touchstart to touchend in milliseconds. If the time difference between touchstart and touchend is greater than this value, the swipe event will not fireswipeThreshold(default:200) - distance to be moved before firingswipeevent
event- the 'type' of event to trigger (currently supportspan,swipe&tapevents)handler- function to call when the event is triggeredprevent- boolean (default:false). This addspreventDefault()to default touch listeners
Example with React Component:
import React from 'react';
import Touchd from 'touchd';
export default class MyComponent extends React.Component {
constructor(props) {
// ...your code
}
// Should be called in or after componentDidMount()
componentDidMount() {
const element = document.getElementById('root');
const touchd = new Touchd(element);
touchd.on('swipe', (e) => {
// do something with the event (e)
});
}
}More to come.
- better handling for
pan- needs to handle switching directions - available by default, rather than using
element = new Touchd(...) - update documentation
- values returned in Events
- more examples