|
3 | 3 | * license: "MIT",
|
4 | 4 | * github: "https://github.com/yangyuji/touch-libs",
|
5 | 5 | * name: "touch-event.js",
|
6 |
| - * version: "1.1.0" |
| 6 | + * version: "1.1.1" |
7 | 7 | */
|
8 | 8 |
|
9 | 9 | (function (root, factory) {
|
|
56 | 56 | this.startTime = 0;
|
57 | 57 | this.endTime = 0;
|
58 | 58 |
|
| 59 | + this.start = this._start.bind(this); |
| 60 | + this.move = this._move.bind(this); |
| 61 | + this.end = this._end.bind(this); |
| 62 | + this.cancel = this._cancel.bind(this); |
| 63 | + |
59 | 64 | // 采用事件驱动,不使用回调
|
60 | 65 | this._events = {};
|
61 | 66 |
|
62 | 67 | // 绑定touch事件
|
63 |
| - this._bindEvents(); |
| 68 | + this.page.addEventListener('touchstart', this.start, |
| 69 | + utils._supportPassive() ? { passive: true } : false); |
64 | 70 | }
|
65 | 71 |
|
66 | 72 | touch.prototype = {
|
67 |
| - version: '1.1.0', |
| 73 | + version: '1.1.1', |
68 | 74 | destroy: function () {
|
69 |
| - this._unbindEvents(); |
| 75 | + this.page.removeEventListener('touchstart', this.start, false); |
70 | 76 | this.emit(this.EVENTS.destroy);
|
71 | 77 | },
|
72 | 78 | _start: function (e) {
|
|
81 | 87 | this.pointX = point.pageX;
|
82 | 88 | this.pointY = point.pageY;
|
83 | 89 |
|
| 90 | + this.page.addEventListener('touchmove', this.move, |
| 91 | + utils._supportPassive() ? { passive: true } : false); |
| 92 | + this.page.addEventListener('touchend', this.end, false); |
| 93 | + this.page.addEventListener('touchcancel', this.cancel, false); |
| 94 | + |
84 | 95 | this.emit(this.EVENTS.start, e);
|
85 | 96 | },
|
86 | 97 | _move: function (e) {
|
|
102 | 113 |
|
103 | 114 | this.emit(this.EVENTS.move, this.distX, this.distY, e);
|
104 | 115 | // throttle
|
105 |
| - requestAnimationFrame((function () { |
106 |
| - this.emit(this.EVENTS.throttle, this.distX, this.distY, e); |
107 |
| - }).bind(this)); |
| 116 | + var _this = this; |
| 117 | + requestAnimationFrame(function () { |
| 118 | + _this.emit(_this.EVENTS.throttle, _this.distX, _this.distY, e); |
| 119 | + }); |
108 | 120 | },
|
109 | 121 | _end: function (e) {
|
110 | 122 | var point = e.changedTouches ? e.changedTouches[0] : e,
|
111 | 123 | duration = 0, // 耗时
|
112 | 124 | velocity = null, // 速度
|
113 | 125 | direction = 'none'; // 方向
|
114 | 126 |
|
| 127 | + this.moved = false; |
| 128 | + this.page.removeEventListener('touchmove', this.move, false); |
| 129 | + this.page.removeEventListener('touchend', this.end, false); |
| 130 | + this.page.removeEventListener('touchcancel', this.cancel, false); |
| 131 | + |
115 | 132 | this.endTime = utils._getTime();
|
116 | 133 | this.emit(this.EVENTS.end);
|
117 | 134 |
|
|
137 | 154 | },
|
138 | 155 | _cancel: function (e) {
|
139 | 156 | this.moved = false;
|
| 157 | + this.page.removeEventListener('touchmove', this.move, false); |
| 158 | + this.page.removeEventListener('touchend', this.end, false); |
| 159 | + this.page.removeEventListener('touchcancel', this.cancel, false); |
140 | 160 | this.distX = 0;
|
141 | 161 | this.distY = 0;
|
142 | 162 | this.pointX = 0;
|
|
176 | 196 | }
|
177 | 197 | return y < 0 ? 'up' : 'down';
|
178 | 198 | },
|
179 |
| - _bindEvents: function () { |
180 |
| - this.start = this._start.bind(this); |
181 |
| - this.move = this._move.bind(this); |
182 |
| - this.end = this._end.bind(this); |
183 |
| - this.cancel = this._cancel.bind(this); |
184 |
| - |
185 |
| - this.page.addEventListener('touchstart', this.start, |
186 |
| - utils._supportPassive() ? {passive: true} : false); |
187 |
| - this.page.addEventListener('touchmove', this.move, |
188 |
| - utils._supportPassive() ? {passive: true} : false); |
189 |
| - this.page.addEventListener('touchend', this.end, false); |
190 |
| - this.page.addEventListener('touchcancel', this.cancel, false); |
191 |
| - }, |
192 |
| - _unbindEvents: function () { |
193 |
| - this.page.removeEventListener('touchstart', this.start, false); |
194 |
| - this.page.removeEventListener('touchmove', this.move, false); |
195 |
| - this.page.removeEventListener('touchend', this.end, false); |
196 |
| - this.page.removeEventListener('touchcancel', this.cancel, false); |
197 |
| - }, |
198 | 199 | // Event
|
199 | 200 | emit: function (type) {
|
200 | 201 | if (!this._events[type]) {
|
|
0 commit comments