/
jquery.scrollbar.js
309 lines (267 loc) · 10.3 KB
/
jquery.scrollbar.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* A jQuery plugin for adding custom scrollbars to a div
*
* Scrollbars, a jQuery plugin for adding custom scrollbars to a div.
* Version: 0.1
* Plugin URI: https://github.com/dgposey/jQuery-Scrollbars
* Author: David Posey
* Author URI: http://www.davidgposey.com
* License: GPL
*
* Full documentation is available in README.md.
*/
(function($){
/**
* Apply the scrollbar() method to the div or divs you want to have scrollbars.
* Example Usage: $("#mydiv").scrollbar();
* @param userOptions optional JSON object with settings, including any of the following:
* regInterval (default: 50) the interval, in pixels, between scrolls when pressing an arrow key or clicking on the up/down buttons
* trackInterval (default: 200) the interval, in pixels, between scrolls when clicking on the track
* initialScrollDelay (default: 700) the delay, in milliseconds, between the first scroll and the second scroll when the mouse is down
* scrollDelay (default: 100) the delay, in milliseconds, between all subsequent scrolls when the mouse is down on up/down buttons
* trackScrollDelay (default: 100) the delay, in milliseconds, between all subsequent scrolls when the mouse is down on the track
*/
$.fn.scrollbar = function(userOptions){
var settings = {
regInterval: 50, // the interval, in pixels, between scrolls when pressing an arrow key or clicking on the up/down buttons
trackInterval: 200, // the interval, in pixels, between scrolls when clicking on the track
initialScrollDelay: 700, // the delay, in milliseconds, between the first scroll and the second scroll when the mouse is down
scrollDelay: 100, // the delay, in milliseconds, between all subsequent scrolls when the mouse is down on up/down buttons
trackScrollDelay: 100 // the delay, in milliseconds, between all subsequent scrolls when the mouse is down on the track
};
if(userOptions){
$.extend(settings,userOptions);
}
// iterate through each container
this.each(function(){
var cont = $(this);
// set necessary css values on the container passed in by the user
cont.css({"overflow": "hidden", "position": "relative"});
// reconstruct the innards to include a movable div with the contents and a scrollbar with its elements
cont.html("<div class='sb-inner'>" + cont.html() + "</div><div class='sb-scrollbar'><div class='sb-up'></div><div class='sb-toptrack'></div><div class='sb-thumb'></div><div class='sb-bottomtrack'></div><div class='sb-down'></div></div>").css({"position": "relative"});
var sbInner = cont.children(".sb-inner");
var sbBar = cont.children(".sb-scrollbar");
// do some initial calculations
var innerHeight, contHeight, trackHeight, heightRatio, thumbHeight, thumbDragInterval;
function init(){
if(sbInner.height() > cont.height()){
innerHeight = sbInner.height();
contHeight = cont.height();
trackHeight = sbBar.height() - cont.find(".sb-up").height() * 2; // height of the track
heightRatio = contHeight / innerHeight; // ratio of container height to content height, used in calculating thumbHeight
thumbHeight = heightRatio * trackHeight; // height of the thumb
thumbDragInterval = (innerHeight - contHeight) / (trackHeight - thumbHeight); // the interval, in pixels, between scrolls when dragging 1 pixel with the thumb
}else{
sbBar.width(0).hide();
}
// set the inner width
var innerWidth = cont.width() - sbBar.width() - sbInner.padding().left - sbInner.padding().right - sbInner.margin().left - sbInner.margin().right - sbInner.border().left - sbInner.border().right;
sbInner.width(innerWidth);
// adjust track and thumb heights and positions based on current conditions (initial)
adjustTracks();
}
init();
var scrollEvent; // the current timeout, used for clearTimeout()
var sbclicked = false; // whether there is currently a mousedown on any part of the scrollbar
var current = null; // the current object in the scrollbar that's being clicked on
/********************
*** handle events ***
********************/
// click (and hold) on down button
cont.find(".sb-down").mousedown(function(e){
if(!sbclicked){
$(this).addClass("clicked");
sbclicked = true;
current = $(this);
goDirection(-1, settings.regInterval);
scrollEvent = setTimeout(goDown, settings.initialScrollDelay);
}else{
goDown();
}
});
// click (and hold) on up button
cont.find(".sb-up").mousedown(function(){
if(!sbclicked){
$(this).addClass("clicked");
sbclicked = true;
current = $(this);
goDirection(1, settings.regInterval);
scrollEvent = setTimeout(goUp, settings.initialScrollDelay);
}else{
goUp();
}
});
// drag the thumb
var trackMovement = false;
var prevPos;
cont.find(".sb-thumb").mousedown(function(e){
$(this).addClass("clicked");
trackMovement = true;
prevPos = e.pageY;
$(document).mousemove(function(e){
if(trackMovement){
var change = prevPos - e.pageY;
var interval = thumbDragInterval * Math.abs(change);
var dir = change / Math.abs(change);
goDirection(dir, interval);
prevPos = e.pageY;
}
});
return false;
});
// click (and hold) on top track
cont.find(".sb-toptrack").mousedown(function(){
if(!sbclicked){
$(this).addClass("clicked");
sbclicked = true;
current = $(this);
goDirection(1, settings.trackInterval);
scrollEvent = setTimeout(goUpBig, settings.initialScrollDelay);
}else{
goUpBig();
}
});
// click (and hold) on bottom track
cont.find(".sb-bottomtrack").mousedown(function(){
if(!sbclicked){
$(this).addClass("clicked");
sbclicked = true;
current = $(this);
goDirection(-1, settings.trackInterval);
scrollEvent = setTimeout(goDownBig, settings.initialScrollDelay);
}else{
goDownBig();
}
});
// release any button, thumb, or track
$(window).mouseup(function(){
$(".sb-scrollbar").children().removeClass("clicked");
clearTimeout(scrollEvent);
sbclicked = false;
trackMovement = false;
current = null;
});
if(sbBar.isVisible()){
// press (and hold) down or up arrow
$(document).keydown(function(e){
if(e.which == 40) {
// down arrow
e.preventDefault();
goDirection(-1, settings.regInterval);
scrollEvent = setTimeout(goDown, settings.initialScrollDelay);
}else if(e.which == 38){
// up arrow
e.preventDefault();
goDirection(1, settings.regInterval);
scrollEvent = setTimeout(goUp, settings.initialScrollDelay);
}else if(e.which == 34){
// page down
e.preventDefault();
goDirection(-1, contHeight);
scrollEvent = setTimeout(goPageDown, settings.initialScrollDelay);
}else if(e.which == 33){
// page up
e.preventDefault();
goDirection(1, contHeight);
scrollEvent = setTimeout(goPageUp, settings.initialScrollDelay);
}
});
// release a key
$(document).keydown(function(e){
clearTimeout(scrollEvent);
});
// scroll mousewheel (uses external library)
cont.mousewheel(function(e, delta, deltaX, deltaY){
if(delta < 0){
// mousewheel is scrolling down
goDirection(-1, settings.regInterval);
}else{
// mousewheel is scrolling up
goDirection(1, settings.regInterval);
}
});
}
/**************************************
*** functions for going up and down ***
**************************************/
/**
* Go down and repeat (recursive)
*/
function goDown(){
goDirection(-1, settings.regInterval);
scrollEvent = setTimeout(goDown, settings.scrollDelay);
}
/**
* Go up and repeat (recursive)
*/
function goUp(){
goDirection(1, settings.regInterval);
scrollEvent = setTimeout(goUp, settings.scrollDelay);
}
/**
* Go down and repeat (recursive) (big interval)
*/
function goDownBig(){
goDirection(-1, settings.trackInterval);
scrollEvent = setTimeout(goDownBig, settings.trackScrollDelay);
}
/**
* Go up and repeat (recursive) (big interval)
*/
function goUpBig(){
goDirection(1, settings.trackInterval);
scrollEvent = setTimeout(goUpBig, settings.trackScrollDelay);
}
function goPageDown(){
goDirection(-1, contHeight);
scrollEvent = setTimeout(goPageDown, settings.scrollDelay);
}
function goPageUp(){
goDirection(1, contHeight);
scrollEvent = setTimeout(goPageUp, settings.scrollDelay);
}
/**
* Go up (dir=1) or down (dir=-1) one time
*/
function goDirection(dir, interval){
var vertPos = sbInner.position().top;
var bottomPos = contHeight - innerHeight;
var newVertPos = vertPos + interval * dir;
if(newVertPos >= 0 && dir == 1){
// we've reached the top and we're trying to go up
clearTimeout(scrollEvent);
sbInner.css("top", "0px");
adjustTracks();
return;
}else if(newVertPos <= bottomPos && dir == -1){
// we've reached the bottom and we're trying to go down
clearTimeout(scrollEvent);
sbInner.css("top", bottomPos + "px");
adjustTracks();
return;
}
sbInner.css("top", newVertPos + "px");
adjustTracks();
}
/**
* Adjust track and thumb heights and positions based on current conditions.
*/
function adjustTracks(){
var vertPos = sbInner.position().top;
var posRatio = (vertPos * -1) / (innerHeight - contHeight);
var topTrackHeight = (trackHeight - thumbHeight) * posRatio;
var bottomTrackHeight = (trackHeight - thumbHeight) * (1 - posRatio);
cont.find(".sb-toptrack").css({"height": topTrackHeight + thumbHeight * 0.5 + "px", "top": cont.find(".sb-up").height() + "px"});
cont.find(".sb-bottomtrack").css({"height": bottomTrackHeight + thumbHeight * 0.5 + "px", "top": cont.find(".sb-up").height() + topTrackHeight + thumbHeight * 0.5 + "px"});
cont.find(".sb-thumb").css({"height": thumbHeight + "px", "top": cont.find(".sb-up").height() + topTrackHeight + "px"});
}
/**
* Function called on div after content of that div is changed to adjust the scrollbars.
*/
$.fn.adjust = function(){
init();
};
});
return this;
};
})(jQuery);