1 | # touch
|
2 |
|
3 | [![version](https://img.shields.io/npm/v/@axe/touch.svg)](https://www.npmjs.org/package/@axe/touch)
|
4 |
|
5 | Define some usual gesture with touch events.
|
6 |
|
7 | ## Installation
|
8 |
|
9 | ```console
|
10 | npm install @axe/touch
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | import { Tap, Press, Swipe } from '@axe/touch'
|
17 |
|
18 | // tap
|
19 | const tap = new Tap('#tap')
|
20 | tap.addEvent(() => {
|
21 | console.info('tap')
|
22 | })
|
23 |
|
24 | // longtap
|
25 | const longtap = new Tap('#longtap')
|
26 | longtap.addEvent(() => {
|
27 | console.info('longtap')
|
28 | }, 'longtap')
|
29 |
|
30 | // doubletap
|
31 | const doubletap = new Tap('#doubletap')
|
32 | doubletap.addEvent(() => {
|
33 | console.info('doubletap')
|
34 | }, 'doubletap')
|
35 |
|
36 | // press
|
37 | const press = new Press('#press')
|
38 | press.addEvent(() => {
|
39 | console.info('press')
|
40 | })
|
41 |
|
42 | // swipe
|
43 | const swipeNode = document.getElementById('swipe')
|
44 |
|
45 | const swipe = new Swipe('#swipe')
|
46 | swipe.onMove((offsetX, offsetY) => {
|
47 | swipeNode.style.left = offsetX + 'px'
|
48 | swipeNode.style.top = offsetY + 'px'
|
49 | console.info('swipe: x=' + offsetX + ', y=' + offsetY)
|
50 | })
|
51 | swipe.addEvent((direction) => {
|
52 | console.info('swipe ' + direction)
|
53 | })
|
54 | ```
|
55 |
|
56 | ## API
|
57 |
|
58 | ### tap
|
59 |
|
60 | This method is same to click but without delay for browser.
|
61 |
|
62 | It 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 |
|
69 | event
|
70 |
|
71 | * addEvent(fn, type = 'tap')
|
72 | * type: tap, doubletap, longtap
|
73 | * removeEvent(fn, type = 'tap')
|
74 | * dispatchEvent(type = 'tap')
|
75 |
|
76 | ### press
|
77 |
|
78 | This method is same to longtap but `trigger when timeout`.
|
79 |
|
80 | It 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 |
|
90 | This method will be trigger when slide side.
|
91 |
|
92 | It 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 |
|
105 | when 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
|
111 | fle dev
|
112 |
|
113 | # build for production with minification
|
114 | fle build
|
115 | ```
|
116 |
|
117 | For detailed explanation, consult the [docs for fle-cli](https://www.npmjs.com/package/fle-cli).
|