UNPKG

3.02 kBMarkdownView Raw
1# touch
2
3[![version](https://img.shields.io/npm/v/@axe/touch.svg)](https://www.npmjs.org/package/@axe/touch)
4
5Define some usual gesture with touch events.
6
7## Installation
8
9```console
10npm install @axe/touch
11```
12
13## Usage
14
15```js
16import { Tap, Press, Swipe } from '@axe/touch'
17
18// tap
19const tap = new Tap('#tap')
20tap.addEvent(() => {
21 console.info('tap')
22})
23
24// longtap
25const longtap = new Tap('#longtap')
26longtap.addEvent(() => {
27 console.info('longtap')
28}, 'longtap')
29
30// doubletap
31const doubletap = new Tap('#doubletap')
32doubletap.addEvent(() => {
33 console.info('doubletap')
34}, 'doubletap')
35
36// press
37const press = new Press('#press')
38press.addEvent(() => {
39 console.info('press')
40})
41
42// swipe
43const swipeNode = document.getElementById('swipe')
44
45const swipe = new Swipe('#swipe')
46swipe.onMove((offsetX, offsetY) => {
47 swipeNode.style.left = offsetX + 'px'
48 swipeNode.style.top = offsetY + 'px'
49 console.info('swipe: x=' + offsetX + ', y=' + offsetY)
50})
51swipe.addEvent((direction) => {
52 console.info('swipe ' + direction)
53})
54```
55
56## API
57
58### tap
59
60This method is same to click but without delay for browser.
61
62It has 3 arguments:
63
64* node [HTMLElement] The element will be addEventListener
65* options [Object] Define default data by yourself, you needn't config as usual
66 * time [Number:250] It will be trigger if touch less than time
67 * offset [Number:10] It will be trigger if offset `less than` distance, unit is `px`
68
69event
70
71* addEvent(fn, type = 'tap')
72 * type: tap, doubletap, longtap
73* removeEvent(fn, type = 'tap')
74* dispatchEvent(type = 'tap')
75
76### press
77
78This method is same to longtap but `trigger when timeout`.
79
80It has 3 arguments:
81
82* node [HTMLElement] The element will be addEventListener
83* callback [Function] It will be work when evnet trigger
84* options [Object] Define default data by yourself, you needn't config as usual
85 * time [Number:350] It will be trigger if touch `more than` time
86 * offset [Number:10] It will be trigger if offset less than distance, unit is `px`
87
88### swipe
89
90This method will be trigger when slide side.
91
92It has 3 arguments:
93
94* node [HTMLElement] The element will be addEventListener
95* callback [Function] It will be work when evnet trigger
96* options [Object] Define default data by yourself, you needn't config as usual
97 * axis [String:'all'] The other value is horizontal and vertical
98 * offset [Number:100] It will be trigger if offset `more than` distance, unit is `px`
99 * speed [Number:200] It will be grigger if speed is fast and needn't enough offset
100 * touchmove [Function(offsetX, offsetY)] It allow you handle moving event, and you can change element position in live
101 * prevent [Boolean:true] PreventDefault when touch start
102
103**NOTE**
104
105when you use Touch, the node was inject initially so that you needn't pass it again.
106
107## Build Setup
108
109``` bash
110# serve with hot reload at localhost:5000
111fle dev
112
113# build for production with minification
114fle build
115```
116
117For detailed explanation, consult the [docs for fle-cli](https://www.npmjs.com/package/fle-cli).