0.1.6 • Published 10 years ago
touchy v0.1.6
touchy
Yet another tap, tap:hold and swipe events enabler. Based on
xtap-event/tap.js
TODO: Add unit tests
installation
npm i --save touchyusage
// usually this is enough to generate the events
// this can be done in a module that can be required
// at the beginning of your app to generate the events
var touchy = require('touchy');
touchy.enableOn(document);Events
This library will generate the following events:
taptap:holdswipeswipe:upswipe:downswipe:leftswipe:right
tap
The tap event is generated when:
- a
touchstart/mousedownevent is followed by atouchend/mouseup - the
end targetis the same as thestart target - the user hasn't move the finger between the starting event and the end
event for more than
options.minTapDisplacementTolerance(default: 10px) - the time between
touchstart/mousedownandtouchend/mouseupis less thanopts.tapHoldMinThreshold(default: 500ms)
Example
// usually this is enough to generate the events
var touchy = require('touchy');
touchy.enableOn(document);
var ele = document.querySelector('#selector');
ele.on('tap', function (e) {
console.log('tap!');
});tap:hold
The tap:hold event is generated when:
- a
touchstart/mousedownevent is followed by atouchend/mouseup - the
end targetis the same as thestart target - the user hasn't move the finger between the starting event and the end
event for more than
options.minTapDisplacementTolerance(default: 10px) - the user has not released the finger for more than
opts.tapHoldMinThreshold(default: 500ms)
Example
// usually this is enough to generate the events
var touchy = require('touchy');
touchy.enableOn(document);
var ele = document.querySelector('#selector');
ele.on('tap:hold', function (e) {
console.log('tap:hold!');
});swipe, swipe:left, swipe:right, swipe:up and swipe:down
The swipe event is generated when
- a
touchstart/mousedownevent is followed bytouchmove/mousemoveand then by atouchend/mouseup - the user has moved the finger between the starting event and the end
event for more than
options.minTapDisplacementTolerance(default: 10px) - a
tap:holdevent has not being generated
Example
// usually this is enough to generate the events
var touchy = require('touchy');
touchy.enableOn(document);
var ele = document.querySelector('#selector');
ele.on('swipe', function (e) {
console.log('swipe', e.detail.direction); // direction: down, up, left, right
});Custom events are also generated with the direction of the swipe: swipe:left, swipe:right, swipe:up and swipe:down
// usually this is enough to generate the events
var touchy = require('touchy');
touchy.enableOn(document);
var ele = document.querySelector('#selector');
ele.on('swipe:left', function (e) {
console.log('swipe:left');
});