-
Notifications
You must be signed in to change notification settings - Fork 54
/
jquery.plugin.pullToRefresh.js
executable file
·114 lines (95 loc) · 2.73 KB
/
jquery.plugin.pullToRefresh.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
108
109
110
111
112
113
114
/*!
* jquery.plugin.pullToRefresh.js
* version 1.0
* author: Damien Antipa
* https://github.com/dantipa/pull-to-refresh-js
*/
(function( $ ){
$.fn.pullToRefresh = function( options ) {
var isTouch = !!('ontouchstart' in window),
cfg = $.extend(true, {
message: {
pull: 'Pull to refresh',
release: 'Release to refresh',
loading: 'Loading'
}
}, options),
html = '<div class="pull-to-refresh">' +
'<div class="icon"></div>' +
'<div class="message">' +
'<i class="arrow"></i>' +
'<i class="spinner large"></i>' +
'<span class="pull">' + cfg.message.pull + '</span>' +
'<span class="release">' + cfg.message.release + '</span>' +
'<span class="loading">' + cfg.message.loading + '</span>' +
'</div>' +
'</div>';
return this.each(function() {
if (!isTouch) {
return;
}
var e = $(this).prepend(html),
content = e.find('.wrap'),
ptr = e.find('.pull-to-refresh'),
arrow = e.find('.arrow'),
spinner = e.find('.spinner'),
pull = e.find('.pull'),
release = e.find('.release'),
loading = e.find('.loading'),
ptrHeight = ptr.height(),
arrowDelay = ptrHeight / 3 * 2,
isActivated = false,
isLoading = false;
content.on('touchstart', function (ev) {
if (e.scrollTop() === 0) { // fix scrolling
e.scrollTop(1);
}
}).on('touchmove', function (ev) {
var top = e.scrollTop(),
deg = 180 - (top < -ptrHeight ? 180 : // degrees to move for the arrow (starts at 180° and decreases)
(top < -arrowDelay ? Math.round(180 / (ptrHeight - arrowDelay) * (-top - arrowDelay))
: 0));
if (isLoading) { // if is already loading -> do nothing
return true;
}
arrow.show();
arrow.css('transform', 'rotate('+ deg + 'deg)'); // move arrow
spinner.hide();
if (-top > ptrHeight) { // release state
release.css('opacity', 1);
pull.css('opacity', 0);
loading.css('opacity', 0);
isActivated = true;
} else if (top > -ptrHeight) { // pull state
release.css('opacity', 0);
loading.css('opacity', 0);
pull.css('opacity', 1);
isActivated = false;
}
}).on('touchend', function(ev) {
var top = e.scrollTop();
if (isActivated) { // loading state
isLoading = true;
isActivated = false;
release.css('opacity', 0);;
pull.css('opacity', 0);
loading.css('opacity', 1);
arrow.hide();
spinner.show();
ptr.css('position', 'static');
cfg.callback().done(function() {
ptr.animate({
height: 10
}, 'fast', 'linear', function () {
ptr.css({
position: 'absolute',
height: ptrHeight
});
isLoading = false;
});
});
}
});
});
};
})( jQuery );