forked from eduardomb/scroll-up-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll-up-bar.js
201 lines (171 loc) · 5.86 KB
/
scroll-up-bar.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* scroll-up-bar v0.3.0 (https://github.com/eduardomb/scroll-up-bar) */
(function($) {
'use strict';
var _destroyFn;
$.scrollupbar = function($bar, options) {
// Default options
options = $.extend({
enterViewport: $.noop,
fullyEnterViewport: $.noop,
exitViewport: $.noop,
partiallyExitViewport: $.noop
}, options);
function isFullyInViewport() {
return $window.scrollTop() <= $bar.offset().top;
}
function isInViewport() {
return $window.scrollTop() < $bar.offset().top + $bar.outerHeight();
}
var $window = $(window),
$document = $(document),
minY = $bar.css('position') == 'fixed' ? 0 : $bar.offset().top,
lastY = $window.scrollTop(), // Use last Y to detect scroll direction.
initialPosTop = $bar.position().top,
iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent),
timeout;
$.scrollupbar.isInViewport = isInViewport();
$.scrollupbar.isFullyInViewport = isFullyInViewport();
// iOS can't handle momentum scroll properly (See discussion on
// http://stackoverflow.com/questions/2863547).
if (!iOS) {
$window.on('scroll.scrollupbar', function() {
var y = $window.scrollTop(),
barHeight = $bar.outerHeight();
// Ignore elastic scrolling.
if (y < 0 || y > ($document.height() - $window.height())) {
return;
}
// Cancel the event fired by the previous scroll.
if (timeout) {
clearTimeout(timeout);
}
if (y < lastY) { // Scrolling up
// If the bar is hidden, place it right above the top frame.
if (!$.scrollupbar.isInViewport && lastY - barHeight >= minY) {
$bar.css('top', lastY - barHeight);
$.scrollupbar.isInViewport = true;
options.enterViewport();
}
// Scrolls up bigger than the bar's height fixes the bar on top.
if (isFullyInViewport()) {
if (y >= minY) {
$bar.css({
'position': 'fixed',
'top': 0
});
} else {
$bar.css({
'position': 'absolute',
'top': initialPosTop
});
}
if (!$.scrollupbar.isFullyInViewport) {
$.scrollupbar.isFullyInViewport = true;
options.fullyEnterViewport();
}
}
// Fire an event to reveal the entire bar after 400ms if the scroll
// wasn't big enough.
timeout = setTimeout(function() {
if (!isFullyInViewport()) {
$bar.css({
'position': 'fixed',
'top': $bar.offset().top - y
});
$bar.animate({'top': 0}, 100, function() {
$.scrollupbar.isFullyInViewport = true;
options.fullyEnterViewport();
});
}
}, 400);
} else if (y > lastY) { // Scrolling down
// Unfix the bar allowing it to scroll with the page.
if ($.scrollupbar.isFullyInViewport) {
$bar.css({
'position': 'absolute',
'top': lastY > minY ? lastY : initialPosTop
});
if (!isFullyInViewport()) {
$.scrollupbar.isFullyInViewport = false;
options.partiallyExitViewport();
}
}
if ($.scrollupbar.isInViewport && !isInViewport()) {
$.scrollupbar.isInViewport = false;
options.exitViewport();
}
// Fire an event to hide the entire bar after 400ms if the scroll
// wasn't big enough.
timeout = setTimeout(function() {
if (isInViewport() && y - barHeight >= minY) {
$bar.animate({'top': y - barHeight}, 100, function() {
$.scrollupbar.isInViewport = false;
options.exitViewport();
});
}
}, 400);
}
lastY = y;
});
} else { // Fallback simplified behaviour for iOS.
$window.on('touchstart.scrollupbar', function () {
lastY = $window.scrollTop();
});
$window.on('touchend.scrollupbar', function () {
var y = $window.scrollTop();
if (y < lastY || y - $bar.outerHeight() < minY) { // Scrolling up
if (y <= minY) {
// Restore original position.
$bar.css({
'position': 'absolute',
'top': initialPosTop
});
$bar.show(function() {
$.scrollupbar.isInViewport = true;
$.scrollupbar.isFullyInViewport = true;
options.enterViewport();
options.fullyEnterViewport();
});
} else {
$bar.css({
'position': 'fixed',
'top': 0
});
$.scrollupbar.isInViewport = true;
options.enterViewport();
$bar.slideDown(function() {
$.scrollupbar.isFullyInViewport = true;
options.fullyEnterViewport();
});
}
} else if (y > lastY) { // Scrolling down
$.scrollupbar.isFullyInViewport = false;
options.partiallyExitViewport();
$bar.slideUp(function() {
$.scrollupbar.isInViewport = false;
options.exitViewport();
});
}
lastY = y;
});
}
_destroyFn = function() {
// Unbind all listeners added by scrollupbar plugin
$window.off('.scrollupbar');
// Restore original bar position.
$bar.css({
'position': 'absolute',
'top': initialPosTop
});
};
return $bar;
};
$.scrollupbar.destroy = function() {
if (_destroyFn) {
return _destroyFn();
}
};
$.fn.scrollupbar = function(options) {
return $.scrollupbar(this, options);
};
})(jQuery);