forked from rochal/jQuery-slimScroll
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslimScroll.js
More file actions
325 lines (277 loc) · 8.72 KB
/
slimScroll.js
File metadata and controls
325 lines (277 loc) · 8.72 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*! Copyright (c) 2011 Piotr Rochala (http://rocha.la)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version: 0.5.0
*
*/
(function($) {
jQuery.fn.extend({
slimScroll: function(options) {
var defaults = {
wheelStep : 20,
width : 'auto',
height : '250px',
size : '7px',
color: '#000',
position : 'right',
distance : '1px',
start : 'top',
opacity : .4,
alwaysVisible : false,
railVisible : false,
railColor : '#333',
railOpacity : '0.2',
railClass : 'slimScrollRail',
barClass : 'slimScrollBar',
wrapperClass : 'slimScrollDiv',
allowPageScroll: false,
scroll: 0
};
var o = ops = $.extend( defaults , options );
// used in event handlers and for better minification
var me = $(this);
// do it for every element that matches selector
this.each(function(){
var isOverPanel, isOverBar, isDragg, queueHide, barHeight, percentScroll,
divS = '<div></div>',
minBarHeight = 30,
releaseScroll = false,
wheelStep = parseInt(o.wheelStep),
cwidth = o.width,
cheight = o.height,
size = o.size,
color = o.color,
position = o.position,
distance = o.distance,
start = o.start,
opacity = o.opacity,
alwaysVisible = o.alwaysVisible,
railVisible = o.railVisible,
railColor = o.railColor,
railOpacity = o.railOpacity,
allowPageScroll = o.allowPageScroll,
scroll = o.scroll;
// optionally set height to the parent's height
if (cheight == 'auto') {
cheight = me.parent().innerHeight();
}
//ensure we are not binding it again
if (me.parent().hasClass('slimScrollDiv'))
{
//check if we should scroll existing instance
if (scroll)
{
//find bar and rail
bar = me.parent().find('.slimScrollBar');
rail = me.parent().find('.slimScrollRail');
//scroll by given amount of pixels
scrollContent( me.scrollTop() + parseInt(scroll), false, true);
}
return;
}
// wrap content
var wrapper = $(divS)
.addClass( o.wrapperClass )
.css({
position: 'relative',
overflow: 'hidden',
width: cwidth,
height: cheight
});
// update style for the div
me.css({
overflow: 'hidden',
width: cwidth,
height: cheight
});
// create scrollbar rail
var rail = $(divS)
.addClass( o.railClass )
.css({
width: size,
height: '100%',
position: 'absolute',
top: 0,
display: (alwaysVisible && railVisible) ? 'block' : 'none',
'border-radius': size,
background: railColor,
opacity: railOpacity,
zIndex: 90
});
// create scrollbar
var bar = $(divS)
.addClass( o.barClass )
.css({
background: color,
width: size,
position: 'absolute',
top: 0,
opacity: opacity,
display: alwaysVisible ? 'block' : 'none',
'border-radius' : size,
BorderRadius: size,
MozBorderRadius: size,
WebkitBorderRadius: size,
zIndex: 99
});
// set position
var posCss = (position == 'right') ? { right: distance } : { left: distance };
rail.css(posCss);
bar.css(posCss);
// wrap it
me.wrap(wrapper);
// append to parent div
me.parent().append(bar);
me.parent().append(rail);
// make it draggable
bar.draggable({
axis: 'y',
containment: 'parent',
start: function() { isDragg = true; },
stop: function() { isDragg = false; hideBar(); },
drag: function(e)
{
// scroll content
scrollContent(0, $(this).position().top, false);
}
});
// on rail over
rail.hover(function(){
showBar();
}, function(){
hideBar();
});
// on bar over
bar.hover(function(){
isOverBar = true;
}, function(){
isOverBar = false;
});
// show on parent mouseover
me.hover(function(){
isOverPanel = true;
showBar();
hideBar();
}, function(){
isOverPanel = false;
hideBar();
});
var _onWheel = function(e)
{
// use mouse wheel only when mouse is over
if (!isOverPanel) { return; }
var e = e || window.event;
var delta = 0;
if (e.wheelDelta) { delta = -e.wheelDelta/120; }
if (e.detail) { delta = e.detail / 3; }
// scroll content
scrollContent(delta, true);
// stop window scroll
if (e.preventDefault && !releaseScroll) { e.preventDefault(); }
if (!releaseScroll) { e.returnValue = false; }
}
function scrollContent(y, isWheel, isJump)
{
var delta = y;
if (isWheel)
{
// move bar with mouse wheel
delta = parseInt(bar.css('top')) + y * wheelStep / 100 * bar.outerHeight();
// move bar, make sure it doesn't go out
var maxTop = me.outerHeight() - bar.outerHeight();
delta = Math.min(Math.max(delta, 0), maxTop);
// scroll the scrollbar
bar.css({ top: delta + 'px' });
}
// calculate actual scroll amount
percentScroll = parseInt(bar.css('top')) / (me.outerHeight() - bar.outerHeight());
delta = percentScroll * (me[0].scrollHeight - me.outerHeight());
if (isJump)
{
delta = y;
var offsetTop = delta / me[0].scrollHeight * me.outerHeight();
bar.css({ top: offsetTop + 'px' });
}
// scroll content
me.scrollTop(delta);
// ensure bar is visible
showBar();
// trigger hide when scroll is stopped
hideBar();
}
var attachWheel = function()
{
if (window.addEventListener)
{
this.addEventListener('DOMMouseScroll', _onWheel, false );
this.addEventListener('mousewheel', _onWheel, false );
}
else
{
document.attachEvent("onmousewheel", _onWheel)
}
}
// attach scroll events
attachWheel();
function getBarHeight()
{
// calculate scrollbar height and make sure it is not too small
barHeight = Math.max((me.outerHeight() / me[0].scrollHeight) * me.outerHeight(), minBarHeight);
bar.css({ height: barHeight + 'px' });
}
// set up initial height
getBarHeight();
function showBar()
{
// recalculate bar height
getBarHeight();
clearTimeout(queueHide);
// release wheel when bar reached top or bottom
releaseScroll = allowPageScroll && percentScroll == ~~ percentScroll;
// show only when required
if(barHeight >= me.outerHeight()) {
//allow window scroll
releaseScroll = true;
return;
}
bar.stop(true,true).fadeIn('fast');
if (railVisible) { rail.stop(true,true).fadeIn('fast'); }
}
function hideBar()
{
// only hide when options allow it
if (!alwaysVisible)
{
queueHide = setTimeout(function(){
if (!isOverBar && !isDragg)
{
bar.fadeOut('slow');
rail.fadeOut('slow');
}
}, 1000);
}
}
// check start position
if (start == 'bottom')
{
// scroll content to bottom
bar.css({ top: me.outerHeight() - bar.outerHeight() });
scrollContent(0, true);
}
else if (typeof start == 'object')
{
// scroll content
scrollContent($(start).position().top, null, true);
// make sure bar stays hidden
if (!alwaysVisible) { bar.hide(); }
}
});
// maintain chainability
return this;
}
});
jQuery.fn.extend({
slimscroll: jQuery.fn.slimScroll
});
})(jQuery);