-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpullRefresh.js
107 lines (103 loc) · 4.64 KB
/
pullRefresh.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
99
100
101
102
103
104
105
106
107
$.fn.extend({
refresh: function(option) {
var el = $(this);
var defaults = {
height:60,//设置触发下拉刷新的距离;
loading_text:$('.loading_text'),//设置文字容器;
loading_icon:$('.loading_icon'),//设置加载icon的容器;
coefficient:0.6,//阻尼系数;注,阻尼系数越小,越难触发。
pullFunction:function(){},//下拉刷新请求数据函数;
}
var settings = $.extend(defaults, option || {}); //init
console.log(settings)
var _hasPhone = navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i);
var height=settings.height;
var className="loading_icon";
var _hasTouch = 'ontouchstart' in window;
var _pulldownConfig = { normalStatus: "下拉即可刷新", maxStatus: "松开立即刷新", releaseStatus: "正在加载…" };
var _start = 0,_end = 0;
var _TransitionObj = {
translate: function (height) {
el.css({ "-webkit-transform": "translate(0," + height + "px)", "transform": "translate(0," + height + "px)" });
},
translitionTime: function (time) {
el.css({ "-webkit-transition": "all " + time + "s", "transition": "all " + time + "s" });
},
goDefault: function () {
_TransitionObj.translitionTime(0.5);
_TransitionObj.translate(0);
}
};
var flag=true
var _bindTouchEvents = function () {
if(_hasPhone){
el.bind("touchstart", _touchstartHandler);
el.bind("touchmove", _touchmoveHandler);
el.bind("touchend", _touchendHandler);
}else{
el.bind("mousedown", _touchstartHandler);
el.bind("mousemove", _touchmoveHandler);
el.bind("mouseup", _touchendHandler);
}
};
var _touchstartHandler = function (e) {
flag=false;
settings.loading_icon.removeClass(className);
var even = typeof event == "undefined" ? e : event;
//保存当前鼠标Y坐标
_start = _hasTouch ? even.touches[0].pageY : even.pageY;
if (el.scrollTop() > 0) {
console.log(el.scrollTop());
//消除滑块动画时间
_TransitionObj.translitionTime(0);
}
};
var changeHeight;
var _touchmoveHandler = function (e) {
if($(document).scrollTop()>=10||flag){
return
}
changeHeight=_end - _start;
var even = typeof event == "undefined" ? e : event;
//保存当前鼠标Y坐标
_end = _hasTouch ? even.touches[0].pageY : even.pageY;
if (changeHeight<0||changeHeight>200){
return
}
if (changeHeight*settings.coefficient > height) {
settings.loading_text.html(_pulldownConfig.maxStatus);
} else {
settings.loading_text.html(_pulldownConfig.normalStatus);
}
even.preventDefault();
//消除滑块动画时间
_TransitionObj.translitionTime(0);
_TransitionObj.translate(changeHeight*settings.coefficient);
};
var back=function(){
_TransitionObj.translate(0)
}
var _touchendHandler = function (e) {
flag=true;
if($(document).scrollTop()>0){
return
}
//判断滑动距离是否大于等于指定值
if (changeHeight*settings.coefficient>= height) {
settings.loading_icon.addClass(className);
//设置滑块回弹时间
_TransitionObj.translitionTime(1);
//设置刷新时的文字
settings.loading_text.html(_pulldownConfig.releaseStatus);
//保留提示部分
_TransitionObj.translate(40);
//执行回调函数
settings.pullFunction(3000,function(){ _TransitionObj.translate(0)})
} else {
//返回初始状态
_TransitionObj.goDefault();
}
}
_bindTouchEvents();
}
})