-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullRefresh.js
More file actions
90 lines (72 loc) · 3.22 KB
/
pullRefresh.js
File metadata and controls
90 lines (72 loc) · 3.22 KB
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
/**
* 下拉刷新组件,不用iscroll
* @author samczhang@tencent.com
* --------------------------------------
* 对外调用接口及自定义事件
* @customEvent canPullDownMove touchmove时触发,此时可处理下拉时Loading状态
* @customEvent clearPullDownMove touchend时触发,介于canPullDownMove与canPullDownRefresh之间,可用于取消loading状态
* @customEvent canPullDownRefresh touchend时触发,所有状态准备完毕,可请求新数据
* demo http://info.3g.qq.com/g/s?aid=wechat_l
*
*/
define( 'pullRefresh', [ 'jqmobi' ], function( $ ) {
var pullRefresh = function( config ) {
this.defaultConfig = {
el: $( document.body ), //绑定事件的dom元素 id或jq对象
offsetScrollTop: 2, //滚动条偏移值,大于此值不触发下拉事件
offsetY: 75 //触摸起止Y偏移值,大于些值才会触发下拉事件
};
this.config = $.extend( this.defaultConfig, config || {} );
this.init.call( this );
};
$.extend( pullRefresh.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 scrollTop = document.body.scrollTop,
touchTarget = event.changedTouches[ 0 ],
touchMoveX = touchTarget.clientX,
touchMoveY = touchTarget.clientY;
var offsetX = touchMoveX - touchStartX,
offsetY = touchMoveY - touchStartY;
if ( offsetY > 5 && scrollTop < config.offsetScrollTop && Math.abs( offsetX ) < Math.abs( offsetY ) ) {
event.preventDefault();
$.trigger( me, 'canPullDownMove', [ {
touchStartY: touchStartY,
touchMoveY: touchMoveY
} ] );
}
} );
el.on( 'touchend', function( event ) {
var scrollTop = document.body.scrollTop,
touchTarget = event.changedTouches[ 0 ],
touchEndX = touchTarget.clientX,
touchEndY = touchTarget.clientY;
var offsetX = touchEndX - touchStartX,
offsetY = touchEndY - touchStartY;
if ( offsetY > config.offsetY && scrollTop < config.offsetScrollTop && Math.abs( offsetX ) < Math.abs( offsetY ) ) {
$.trigger( me, 'canPullDownRefresh' );
} else {
$.trigger( me, 'clearPullDownMove' );
}
} );
}
} );
return pullRefresh;
} );