Skip to content

Commit b0f7808

Browse files
author
yangyuji
committed
v1.1.1
1 parent 79c0e8c commit b0f7808

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

touch-es6.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* license: "MIT",
44
* github: "https://github.com/yangyuji/touch-libs",
55
* name: "touch-es6.js",
6-
* version: "1.1.0"
6+
* version: "1.1.1"
77
*/
88

99
class Touch {
1010
constructor(el) {
11+
this.version = '1.1.1'
1112
this.el = typeof el == 'string' ? document.querySelector(el) : el
1213
this.moved = false
1314
// start position
@@ -60,6 +61,8 @@ class Touch {
6061
this.touchMove(e)
6162
break
6263
case 'touchcancel':
64+
this.touchCancel(e)
65+
break
6366
case 'touchend':
6467
this.touchEnd(e)
6568
break
@@ -76,6 +79,7 @@ class Touch {
7679
this.endPos = {}
7780
this.el.addEventListener('touchmove', this, this._supportPassive() ? { passive: true } : false)
7881
this.el.addEventListener('touchend', this, false)
82+
this.el.addEventListener('touchcancel', this, false)
7983

8084
this.emit(this.EVENTS.start, this.startPos, e)
8185
}
@@ -112,6 +116,7 @@ class Touch {
112116
this.moved = false
113117
this.el.removeEventListener('touchmove', this, false)
114118
this.el.removeEventListener('touchend', this, false)
119+
this.el.removeEventListener('touchcancel', this, false)
115120
this.emit(this.EVENTS.end, this.endPos, e)
116121

117122
const action = this._getAction()
@@ -130,6 +135,16 @@ class Touch {
130135
}
131136
}
132137

138+
touchCancel(e) {
139+
this.moved = false
140+
this.el.removeEventListener('touchmove', this, false)
141+
this.el.removeEventListener('touchend', this, false)
142+
this.el.removeEventListener('touchcancel', this, false)
143+
this.startPos = {}
144+
this.endPos = {}
145+
this.emit(this.EVENTS.cancel, e);
146+
}
147+
133148
// action analyze
134149
_getAction() {
135150
const distX = this.endPos.x - this.startPos.x,
@@ -194,20 +209,17 @@ class Touch {
194209
callbacks.push(callback)
195210
this._events[event] = callbacks
196211
}
197-
198212
off(event, callback) {
199213
let callbacks = this._events[event] || []
200214
this._events[event] = callbacks.filter(fn => fn !== callback)
201215
this._events[event].length === 0 && delete this._events[event]
202216
}
203-
204217
emit(...args) {
205218
const event = args[0]
206219
const params = [].slice.call(args, 1)
207220
const callbacks = this._events[event] || []
208221
callbacks.forEach(fn => fn.apply(this, params))
209222
}
210-
211223
once(event, callback) {
212224
let wrapFunc = (...args) => {
213225
callback.apply(this, args)

touch-event.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* license: "MIT",
44
* github: "https://github.com/yangyuji/touch-libs",
55
* name: "touch-event.js",
6-
* version: "1.1.0"
6+
* version: "1.1.1"
77
*/
88

99
(function (root, factory) {
@@ -56,17 +56,23 @@
5656
this.startTime = 0;
5757
this.endTime = 0;
5858

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+
5964
// 采用事件驱动,不使用回调
6065
this._events = {};
6166

6267
// 绑定touch事件
63-
this._bindEvents();
68+
this.page.addEventListener('touchstart', this.start,
69+
utils._supportPassive() ? { passive: true } : false);
6470
}
6571

6672
touch.prototype = {
67-
version: '1.1.0',
73+
version: '1.1.1',
6874
destroy: function () {
69-
this._unbindEvents();
75+
this.page.removeEventListener('touchstart', this.start, false);
7076
this.emit(this.EVENTS.destroy);
7177
},
7278
_start: function (e) {
@@ -81,6 +87,11 @@
8187
this.pointX = point.pageX;
8288
this.pointY = point.pageY;
8389

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+
8495
this.emit(this.EVENTS.start, e);
8596
},
8697
_move: function (e) {
@@ -102,16 +113,22 @@
102113

103114
this.emit(this.EVENTS.move, this.distX, this.distY, e);
104115
// 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+
});
108120
},
109121
_end: function (e) {
110122
var point = e.changedTouches ? e.changedTouches[0] : e,
111123
duration = 0, // 耗时
112124
velocity = null, // 速度
113125
direction = 'none'; // 方向
114126

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+
115132
this.endTime = utils._getTime();
116133
this.emit(this.EVENTS.end);
117134

@@ -137,6 +154,9 @@
137154
},
138155
_cancel: function (e) {
139156
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);
140160
this.distX = 0;
141161
this.distY = 0;
142162
this.pointX = 0;
@@ -176,25 +196,6 @@
176196
}
177197
return y < 0 ? 'up' : 'down';
178198
},
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-
},
198199
// Event
199200
emit: function (type) {
200201
if (!this._events[type]) {

0 commit comments

Comments
 (0)