Skip to content

Commit 0f463b4

Browse files
committed
add comment
1 parent 34f78a2 commit 0f463b4

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

移动端单点手势(over)/index.js

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
doc = document
1717

1818
//对事件集筛选触发
19+
/**
20+
*
21+
* @param {String} eventname
22+
* @param {Event} e
23+
*/
1924
let emit = function (eventname, e) {
2025
// console.log(eventname)
2126
let events = this._events,
@@ -38,9 +43,13 @@
3843
if (rest_events.length === 0) return
3944
}
4045
//用类名代理 判断。
46+
/**
47+
* @param {Boolean} node
48+
* @param {String} classname
49+
* @return {Boolean}
50+
*/
4151
function hasp(node = false, classname = '') {
4252
if (!classname) return false
43-
var node = node || false
4453
while (node) {
4554
if (node === this._ele.parentNode) {
4655
return false;
@@ -53,15 +62,39 @@
5362
return false
5463
}
5564
//计算方位返回时间类型
65+
/**
66+
* @api public
67+
* @param {Number} x1
68+
* @param {Number} x2
69+
* @param {Number} y1
70+
* @param {Number} y2
71+
* @return {String}
72+
*/
5673
function countposition(x1, x2, y1, y2) {
5774
// 先判断垂直方向水平方向
5875
// 再判断上下左右
5976
return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'swipeleft' : 'swiperight') : (y1 - y2 > 0 ? 'swipeup' : 'swipedown')
6077
}
78+
/**
79+
* @api public
80+
* @param {Number} x1
81+
* @param {Number} x2
82+
* @param {Number} y1
83+
* @param {Number} y2
84+
* @return {String}
85+
*/
6186
function countpositioning(x1, x2, y1, y2) {
6287
return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'swipingleft' : 'swipingright') : (y1 - y2 > 0 ? 'swipingup' : 'swipingdown')
6388
}
6489
//事件触发 对事件做二次打包
90+
/**
91+
*
92+
* @param {String} name
93+
* @param {Function} fn
94+
* @param {HTML Element} dom
95+
* @param {Event} e
96+
* @return {Boolean|undefined}
97+
*/
6598
let event_pack = function (name, fn, dom, e) {
6699
// e.stopPropagation();
67100
let new_event = {
@@ -78,6 +111,12 @@
78111
return call_res
79112
}
80113
//事件监听主要逻辑
114+
/**
115+
* @api public
116+
* @param {HTML Element} dom
117+
* @return {Function}
118+
* @description return value is a function to removeEventListener
119+
*/
81120
let eventListener = function (dom) {
82121

83122
let self = this;
@@ -90,16 +129,23 @@
90129
eventmark = null,
91130
longtap,//用于长按
92131
tap//用于单击
93-
132+
/**
133+
* @description clearTimeout and make isAction change flase
134+
*/
94135
function actionfunc() {
95136
isActive = false;
96137
clearTimeout(touchDelay);
97138
clearTimeout(longtap);
98139
}
140+
/**
141+
* @param {Event} e
142+
* @description add Event Listener
143+
*/
99144
function touchstart(e) {
100145
var e = e || window.event
101146
if (self.stopPropagation) {
102147
e.stopPropagation();
148+
e.preventDefault();
103149
}
104150
eventmark = e || win.event;
105151
starttime = new Date();
@@ -117,11 +163,16 @@
117163
}, 500)
118164

119165
}
166+
/**
167+
* @param {Event} e
168+
* @description add Event Listener
169+
*/
120170
function touchmove(e) {
121171

122172
var e = e || window.event
123173
if (self.stopPropagation) {
124174
e.stopPropagation();
175+
e.preventDefault();
125176
}
126177

127178
eventmark = e
@@ -135,16 +186,24 @@
135186
emit.call(self, 'swiping', eventmark);
136187
emit.call(self, countpositioning(x1, x2, y1, y2), eventmark);
137188
} else {
138-
if (!isActive) return;
189+
if (!isActive){
190+
return false;
191+
}
192+
// console.log('asdasdsa')
139193
emit.call(self, 'singletap', e);
140194
actionfunc()
141195
}
142196

143197
}
198+
/**
199+
* @param {Event} e
200+
* @description add Event Listener
201+
*/
144202
function touchend(e) {
145203
var e = e || window.event
146204
if (self.stopPropagation) {
147205
e.stopPropagation();
206+
e.preventDefault();
148207
}
149208

150209
if (!isActive) {
@@ -159,7 +218,7 @@
159218
//断定此次事件为连续两次轻击事件
160219
emit.call(self, 'doubletap', eventmark);
161220
// console.log('123')
162-
} else if (y2 > 0 && x2 > 0 && (Math.abs(x1 - x2) > 100 || Math.abs(y1 - y2) > 100)) {
221+
} else if (y2 > 0 && x2 > 0 && (Math.abs(x1 - x2) > 10 || Math.abs(y1 - y2) > 10)) {
163222
// console.log('2223')
164223
// console.log(x1,x2,y1,y2)
165224
emit.call(self, countposition(x1, x2, y1, y2), eventmark);
@@ -190,7 +249,11 @@
190249
//touch主事件
191250
//内置对象_event存放触发的时间
192251
//ele对应初始化元素节点
193-
var _touch = function (element, stopPropagation) {
252+
/**
253+
* @param {HTML Element} element
254+
* @param {Boolean} stopPropagation
255+
*/
256+
const _touch = function (element, stopPropagation=true) {
194257
if (!(this instanceof _touch)) return new _touch(element, stopPropagation);
195258
this._events = {};
196259
this._ele = element;
@@ -223,7 +286,7 @@
223286
_touch.prototype._remove = function (eventname, name) {
224287
if (!(this instanceof _touch)) throw new Error('error use');
225288

226-
var _events = this._events;
289+
let _events = this._events;
227290

228291
if (_events[eventname] === void 0 && (!name)) throw new Error('不存在这个事件')
229292
let len = _events[eventname]

0 commit comments

Comments
 (0)