-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchTab.js
98 lines (80 loc) · 3.25 KB
/
touchTab.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* 横向tab滑动组件,不用iscroll
* @author [email protected]
* --------------------------------------
* 对外调用接口及自定义事件
* @customEvent canTouchTabMove touchmove时触发,此时可处理滑动tab
* @customEvent clearTouchTabMove touchend时触发,可以滑动但不满足切换条件时触发
* @customEvent canTouchTabSwitch touchend时触发,达到切换条件,触发切换事件
*
* demo http://info.3g.qq.com/g/s?aid=wechat_l
*
*/
( function( root, factory ) {
if ( typeof define === 'function' ) {
define( 'touchTab', [ 'jqmobi' ], function( $ ) {
return factory( root, $ );
} );
} else {
root.touchTab = factory( root, root.$ );
}
} )( window, function( root, $ ) {
var touchTab = function( config ) {
this.defaultConfig = {
el: $( document.body ), //绑定事件的dom元素 id或jq对象
offsetX: 50 //触摸起止X偏移值,大于些值才会触发下拉事件
};
this.config = $.extend( {}, this.defaultConfig, config || {} );
this.el = ( typeof this.config.el === 'string' ) ? $( this.config.el ) : this.config.el;
this.init.call( this );
};
$.extend( touchTab.prototype, {
init: function() {
this._cacheDom();
this._initEvent();
},
_cacheDom: function() {
this.el = ( typeof this.config.el === 'string' ) ? $( this.config.el ) : this.config.el;
},
_initEvent: function() {
var me = this,
config = this.config,
el = this.el,
touchStartX = 0,
touchStartY = 0;
el.on( 'touchstart', function( event ) {
var touchTarget = event.changedTouches[ 0 ];
touchStartX = touchTarget.clientX;
touchStartY = touchTarget.clientY;
} );
el.on( 'touchmove', function( event ) {
var touchTarget = event.changedTouches[ 0 ],
touchMoveX = touchTarget.clientX,
touchMoveY = touchTarget.clientY;
var offsetX = touchMoveX - touchStartX,
offsetY = touchMoveY - touchStartY;
if ( Math.abs( offsetX ) > 5 && Math.abs( offsetX ) > Math.abs( offsetY ) ) {
event.preventDefault();
$.trigger( me, 'canTouchTabMove', [ {
offsetX: offsetX
} ] );
}
} );
el.on( 'touchend', function( event ) {
var touchTarget = event.changedTouches[ 0 ],
touchEndX = touchTarget.clientX,
touchEndY = touchTarget.clientY;
var offsetX = touchEndX - touchStartX,
offsetY = touchEndY - touchStartY;
if ( Math.abs( offsetX ) > config.offsetX && Math.abs( offsetX ) > Math.abs( offsetY ) ) {
$.trigger( me, 'canTouchTabSwitch', [ {
offsetX: offsetX
} ] );
} else {
$.trigger( me, 'clearTouchTabMove' );
}
} );
}
} );
return touchTab;
} );