-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepDisTouchMove.js
63 lines (63 loc) · 2.05 KB
/
deepDisTouchMove.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
*
* @param {*} element 接受css的class和id,以及html元素标签, (body)默认
* @param {*} direction 横向:landscape 垂直:vertical(默认)
*/
const disTouchMove = function(option){
this.element = !option.element ? 'body' : option.element;
this.direction = option.direction || 'vertical';
this.startX = this.startY = 0;
this._init(this.element, this.direction)
}
disTouchMove.prototype = {
_init: function(element) {
let _ele = document.querySelectorAll(element)
if(!_ele.length){
console.log('element对象不存在')
return
}
if(_ele.length){
_ele.forEach( t => {
this._disTouchMove(t)
if(t.childNodes.length){
this.dElement(t)
}
})
}
// else{
// this._disTouchMove(_ele)
// }
},
dElement: function(element){
if ( !element.childNodes.length ){
this._disTouchMove(element)
}else{
element.childNodes.forEach(t => {
if( t.childNodes.length ){
this.dElement(t)
}
this._disTouchMove(t)
})
}
},
_disTouchMove: function(element){
element.addEventListener('touchstart', (e) => {
let _touch = e.targetTouches[0];
this.startX = _touch.pageX
this.startY = _touch.pageY
})
element.addEventListener('touchmove', (e) => {
let _touch = e.targetTouches[0],
diffX = Math.abs(_touch.pageX - this.startX),
diffY = Math.abs(_touch.pageY - this.startY);
if( diffX < diffY && this.direction == 'vertical' ){
e.stopPropagation()
e.preventDefault()
}
if( diffX > diffY && this.direction == 'landscape' ){
e.stopPropagation()
e.preventDefault()
}
}, false)
}
}