forked from malihu/malihu-custom-scrollbar-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.mCustomScrollbar.js
2086 lines (1858 loc) · 79.4 KB
/
jquery.mCustomScrollbar.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
== malihu jquery custom scrollbar plugin ==
Version: 3.0.2
Plugin URI: http://manos.malihu.gr/jquery-custom-content-scroller
Author: malihu
Author URI: http://manos.malihu.gr
License: MIT License (MIT)
*/
/*
Copyright 2010 Manos Malihutsakis (email: [email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
The code below is fairly long, fully commented and should be normally used in development.
For production, use either the minified jquery.mCustomScrollbar.min.js script or
the production-ready jquery.mCustomScrollbar.concat.min.js which contains the plugin
and dependencies (minified).
*/
;(function($,window,document,undefined){
/*
----------------------------------------
PLUGIN NAMESPACE, PREFIX, DEFAULT SELECTOR(S)
----------------------------------------
*/
var pluginNS="mCustomScrollbar",
pluginPfx="mCS",
defaultSelector=".mCustomScrollbar",
/*
----------------------------------------
DEFAULT OPTIONS
----------------------------------------
*/
defaults={
/*
set element/content width programmatically
values: boolean, pixels, percentage
*/
setWidth:false,
/*
set element/content height programmatically
values: boolean, pixels, percentage
*/
setHeight:false,
/*
set the initial css top property of content
values: string (e.g. "-100px", "10%" etc.)
*/
setTop:0,
/*
set the initial css left property of content
values: string (e.g. "-100px", "10%" etc.)
*/
setLeft:0,
/*
scrollbar axis (vertical and/or horizontal scrollbars)
values (string): "y", "x", "yx"
*/
axis:"y",
/*
position of scrollbar relative to content
values (string): "inside", "outside" ("outside" requires elements with position:relative)
*/
scrollbarPosition:"inside",
/*
scrolling inertia
values: integer (milliseconds)
*/
scrollInertia:950,
/*
auto-adjust scrollbar dragger length
values: boolean
*/
autoDraggerLength:true,
/*
auto-hide scrollbar when idle
values: boolean
*/
autoHideScrollbar:false,
/*
auto-expands scrollbar on mouse-over and dragging
*/
autoExpandScrollbar:false,
/*
always show scrollbar, even when there's nothing to scroll
values: integer (0=disable, 1=always show dragger rail, 2=always show dragger rail, dragger and buttons), boolean
*/
alwaysShowScrollbar:0,
/*
scrolling always snaps to a multiple of this number in pixels
values: integer
*/
snapAmount:null,
/*
when snapping, snap with this number in pixels as an offset
values: integer
*/
snapOffset:0,
/*
mouse-wheel scrolling
*/
mouseWheel:{
/*
enable mouse-wheel scrolling
values: boolean
*/
enable:true,
/*
scrolling amount in pixels
values: "auto", integer
*/
scrollAmount:"auto",
/*
mouse-wheel scrolling axis
the default scrolling direction when both vertical and horizontal scrollbars are present
values (string): "y", "x"
*/
axis:"y",
/*
prevent the default behaviour which automatically scrolls the parent element(s)
when end of scrolling is reached
values: boolean
*/
preventDefault:false,
/*
the reported mouse-wheel delta value. The number of lines (translated to pixels) one wheel notch scrolls.
values: "auto", integer
"auto" uses the default OS/browser value
*/
deltaFactor:"auto",
/*
normalize mouse-wheel delta to -1 or 1 (disables mouse-wheel acceleration)
values: boolean
*/
normalizeDelta:false,
/*
invert mouse-wheel scrolling direction
values: boolean
*/
invert:false,
/*
the tags that disable mouse-wheel when cursor is over them
*/
disableOver:["select","option","keygen","datalist","textarea"]
},
/*
scrollbar buttons
*/
scrollButtons:{
/*
enable scrollbar buttons
values: boolean
*/
enable:false,
/*
scrollbar buttons scrolling type
values (string): "stepless", "stepped"
*/
scrollType:"stepless",
/*
scrolling amount in pixels
values: "auto", integer
*/
scrollAmount:"auto"
},
/*
keyboard scrolling
*/
keyboard:{
/*
enable scrolling via keyboard
values: boolean
*/
enable:true,
/*
keyboard scrolling type
values (string): "stepless", "stepped"
*/
scrollType:"stepless",
/*
scrolling amount in pixels
values: "auto", integer
*/
scrollAmount:"auto"
},
/*
enable content touch-swipe scrolling
values: boolean, integer, string (number)
integer values define the axis-specific minimum amount required for scrolling momentum
*/
contentTouchScroll:25,
/*
advanced option parameters
*/
advanced:{
/*
auto-expand content horizontally (for "x" or "yx" axis)
values: boolean
*/
autoExpandHorizontalScroll:false,
/*
auto-scroll to elements with focus
*/
autoScrollOnFocus:"input,textarea,select,button,datalist,keygen,a[tabindex],area,object,[contenteditable='true']",
/*
auto-update scrollbars on content, element or viewport resize
should be true for fluid layouts/elements, adding/removing content dynamically, hiding/showing elements, content with images etc.
values: boolean
*/
updateOnContentResize:true,
/*
auto-update scrollbars each time each image inside the element is fully loaded
values: boolean
*/
updateOnImageLoad:true,
/*
auto-update scrollbars based on the amount and size changes of specific selectors
useful when you need to update the scrollbar(s) automatically, each time a type of element is added, removed or changes its size
values: boolean, string (e.g. "ul li" will auto-update scrollbars each time list-items inside the element are changed)
a value of true (boolean) will auto-update scrollbars each time any element is changed
*/
updateOnSelectorChange:false
},
/*
scrollbar theme
values: string
ready-to-use themes: "light", "dark", "light-2", "dark-2", "light-3", "dark-3", "light-thick", "dark-thick", "light-thin", "dark-thin",
"rounded", "rounded-dark", "rounded-dots", "rounded-dots-dark", "3d", "3d-dark", "3d-thick", "3d-thick-dark", "minimal", "minimal-dark",
"inset", "inset-dark", "inset-2", "inset-2-dark", "inset-3", "inset-3-dark"
*/
theme:"light",
/*
user defined callback functions
*/
callbacks:{
/*
function to call when a scroll event starts
values (function): function(){}
*/
onScrollStart:false,
/*
function to call when a scroll event is complete
values (function): function(){}
*/
onScroll:false,
/*
function to call when a scroll event is complete and content is scrolled all the way to the end (bottom/right)
values (function): function(){}
*/
onTotalScroll:false,
/*
function to call when a scroll event is complete and content is scrolled back to the beginning (top/left)
values (function): function(){}
*/
onTotalScrollBack:false,
/*
function to call when a scroll event is running
values (function): function(){}
*/
whileScrolling:false,
/*
onTotalScroll offset value
values: integer (pixels)
*/
onTotalScrollOffset:0,
/*
onTotalScrollBack offset value
values: integer (pixels)
*/
onTotalScrollBackOffset:0,
/*
callback offsets will trigger even if content is already scrolled to the end or beginning
values: boolean
*/
alwaysTriggerOffsets:true
},
/*
add scrollbar(s) on all elements matching the current selector, now and in the future
values: boolean, string
string values: "on" (enable), "once" (disable after first invocation), "off" (disable)
*/
live:false,
/*
the matching set of elements (instead of the current selector) to add scrollbar(s), now and in the future
values: string (selector)
*/
liveSelector:null
},
/*
----------------------------------------
VARS, CONSTANTS
----------------------------------------
*/
totalInstances=0, /* plugin instances amount */
liveTimers={}, /* live option timers */
/* live option timers removal */
removeLiveTimers=function(selector){
if(liveTimers[selector]){
clearTimeout(liveTimers[selector]);
functions._delete.call(null,liveTimers[selector]);
}
},
oldIE=(window.attachEvent && !window.addEventListener) ? 1 : 0, /* detect IE < 9 */
touchActive=false, /* global touch state (for touch and pointer events) */
/*
----------------------------------------
METHODS
----------------------------------------
*/
methods={
/*
plugin initialization method
creates the scrollbar(s), plugin data object and options
----------------------------------------
*/
init:function(options){
var options=$.extend(true,{},defaults,options),
selector=functions._selector.call(this); /* validate selector */
/*
if live option is enabled, monitor for elements matching the current selector and
apply scrollbar(s) when found (now and in the future)
*/
if(options.live){
var liveSelector=options.liveSelector || this.selector || defaultSelector, /* live selector(s) */
$liveSelector=$(liveSelector); /* live selector(s) as jquery object */
if(options.live==="off"){
/*
disable live if requested
usage: $(selector).mCustomScrollbar({live:"off"});
*/
removeLiveTimers(liveSelector);
return;
}
liveTimers[liveSelector]=setTimeout(function(){
/* call mCustomScrollbar fn on live selector(s) every half-second */
$liveSelector.mCustomScrollbar(options);
if(options.live==="once" && $liveSelector.length){
/* disable live after first invocation */
removeLiveTimers(liveSelector);
}
},500);
}else{
removeLiveTimers(liveSelector);
}
/* options backward compatibility (for versions < 3.0.0) and normalization */
options.setWidth=(options.set_width) ? options.set_width : options.setWidth;
options.setHeight=(options.set_height) ? options.set_height : options.setHeight;
options.axis=(options.horizontalScroll) ? "x" : functions._findAxis.call(null,options.axis);
options.scrollInertia=options.scrollInertia<17 ? 17 : options.scrollInertia;
if(typeof options.mouseWheel!=="object" && options.mouseWheel==true){ /* old school mouseWheel option (non-object) */
options.mouseWheel={enable:true,scrollAmount:"auto",axis:"y",preventDefault:false,deltaFactor:"auto",normalizeDelta:false,invert:false}
}
options.mouseWheel.scrollAmount=!options.mouseWheelPixels ? options.mouseWheel.scrollAmount : options.mouseWheelPixels;
options.mouseWheel.normalizeDelta=!options.advanced.normalizeMouseWheelDelta ? options.mouseWheel.normalizeDelta : options.advanced.normalizeMouseWheelDelta;
options.scrollButtons.scrollType=functions._findScrollButtonsType.call(null,options.scrollButtons.scrollType);
functions._theme.call(null,options); /* theme-specific options */
/* plugin constructor */
return $(selector).each(function(){
var $this=$(this);
if(!$this.data(pluginPfx)){ /* prevent multiple instantiations */
/* store options and create objects in jquery data */
$this.data(pluginPfx,{
idx:++totalInstances, /* instance index */
opt:options, /* options */
scrollRatio:{y:null,x:null}, /* scrollbar to content ratio */
overflowed:null, /* overflowed axis */
bindEvents:false, /* object to check if events are bound */
tweenRunning:false, /* object to check if tween is running */
sequential:{}, /* sequential scrolling object */
langDir:$this.css("direction"), /* detect/store direction (ltr or rtl) */
cbOffsets:null, /* object to check whether callback offsets always trigger */
/*
object to check how scrolling events where last triggered
"internal" (default - triggered by this script), "external" (triggered by other scripts, e.g. via scrollTo method)
usage: object.data("mCS").trigger
*/
trigger:null
});
/* HTML data attributes */
var o=$this.data(pluginPfx).opt,
htmlDataAxis=$this.data("mcs-axis"),htmlDataSbPos=$this.data("mcs-scrollbar-position"),htmlDataTheme=$this.data("mcs-theme");
if(htmlDataAxis){o.axis=htmlDataAxis;} /* usage example: data-mcs-axis="y" */
if(htmlDataSbPos){o.scrollbarPosition=htmlDataSbPos;} /* usage example: data-mcs-scrollbar-position="outside" */
if(htmlDataTheme){ /* usage example: data-mcs-theme="minimal" */
o.theme=htmlDataTheme;
functions._theme.call(null,o); /* theme-specific options */
}
functions._pluginMarkup.call(this); /* add plugin markup */
methods.update.call(null,$this); /* call the update method */
}
});
},
/* ---------------------------------------- */
/*
plugin update method
updates content and scrollbar(s) values, events and status
----------------------------------------
usage: $(selector).mCustomScrollbar("update");
*/
update:function(el){
var selector=el || functions._selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginPfx)){ /* check if plugin has initialized */
var d=$this.data(pluginPfx),o=d.opt,
mCSB_container=$("#mCSB_"+d.idx+"_container"),
mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
if(!mCSB_container.length){return;}
if(d.tweenRunning){functions._stop.call(null,$this);} /* stop any running tweens while updating */
/* if element was disabled or destroyed, remove class(es) */
if($this.hasClass("mCS_disabled")){$this.removeClass("mCS_disabled");}
if($this.hasClass("mCS_destroyed")){$this.removeClass("mCS_destroyed");}
functions._maxHeight.call(this); /* detect/set css max-height value */
functions._expandContentHorizontally.call(this); /* expand content horizontally */
if(o.axis!=="y" && !o.advanced.autoExpandHorizontalScroll){
mCSB_container.css("width",functions._contentWidth(mCSB_container.children()));
}
d.overflowed=functions._overflowed.call(this); /* determine if scrolling is required */
functions._scrollbarVisibility.call(this); /* show/hide scrollbar(s) */
/* auto-adjust scrollbar dragger length analogous to content */
if(o.autoDraggerLength){functions._setDraggerLength.call(this);}
functions._scrollRatio.call(this); /* calculate and store scrollbar to content ratio */
functions._bindEvents.call(this); /* bind scrollbar events */
/* reset scrolling position and/or events */
var to=[Math.abs(mCSB_container[0].offsetTop),Math.abs(mCSB_container[0].offsetLeft)];
if(o.axis!=="x"){ /* y/yx axis */
if(!d.overflowed[0]){ /* y scrolling is not required */
functions._resetContentPosition.call(this); /* reset content position */
if(o.axis==="y"){
functions._unbindEvents.call(this);
}else if(o.axis==="yx" && d.overflowed[1]){
functions._scrollTo.call(this,$this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
}
}else if(mCSB_dragger[0].height()>mCSB_dragger[0].parent().height()){
functions._resetContentPosition.call(this); /* reset content position */
}else{ /* y scrolling is required */
functions._scrollTo.call(this,$this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
}
}
if(o.axis!=="y"){ /* x/yx axis */
if(!d.overflowed[1]){ /* x scrolling is not required */
functions._resetContentPosition.call(this); /* reset content position */
if(o.axis==="x"){
functions._unbindEvents.call(this);
}else if(o.axis==="yx" && d.overflowed[0]){
functions._scrollTo.call(this,$this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
}
}else if(mCSB_dragger[1].width()>mCSB_dragger[1].parent().width()){
functions._resetContentPosition.call(this); /* reset content position */
}else{ /* x scrolling is required */
functions._scrollTo.call(this,$this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
}
}
functions._autoUpdate.call(this); /* initialize automatic updating (for dynamic content, fluid layouts etc.) */
}
});
},
/* ---------------------------------------- */
/*
plugin scrollTo method
triggers a scrolling event to a specific value
----------------------------------------
usage: $(selector).mCustomScrollbar("scrollTo",value,options);
*/
scrollTo:function(val,options){
/* prevent silly things like $(selector).mCustomScrollbar("scrollTo",undefined); */
if(typeof val=="undefined" || val==null){return;}
var selector=functions._selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginPfx)){ /* check if plugin has initialized */
var d=$this.data(pluginPfx),o=d.opt,
/* method default options */
methodDefaults={
trigger:"external", /* method is by default triggered externally (e.g. from other scripts) */
scrollInertia:o.scrollInertia, /* scrolling inertia (animation duration) */
scrollEasing:"mcsEaseInOut", /* animation easing */
moveDragger:false, /* move dragger instead of content */
callbacks:true, /* enable/disable callbacks */
onStart:true,
onUpdate:true,
onComplete:true
},
methodOptions=$.extend(true,{},methodDefaults,options),
to=functions._arr.call(this,val),dur=methodOptions.scrollInertia < 17 ? 17 : methodOptions.scrollInertia;
/* translate yx values to actual scroll-to positions */
to[0]=functions._to.call(this,to[0],"y");
to[1]=functions._to.call(this,to[1],"x");
/*
check if scroll-to value moves the dragger instead of content.
Only pixel values apply on dragger (e.g. 100, "100px", "-=100" etc.)
*/
if(methodOptions.moveDragger){
to[0]*=d.scrollRatio.y;
to[1]*=d.scrollRatio.x;
}
methodOptions.dur=dur;
setTimeout(function(){
/* do the scrolling */
if(to[0]!==null && typeof to[0]!=="undefined" && o.axis!=="x" && d.overflowed[0]){ /* scroll y */
methodOptions.dir="y";
methodOptions.overwrite="all";
functions._scrollTo.call(this,$this,to[0].toString(),methodOptions);
}
if(to[1]!==null && typeof to[1]!=="undefined" && o.axis!=="y" && d.overflowed[1]){ /* scroll x */
methodOptions.dir="x";
methodOptions.overwrite="none";
functions._scrollTo.call(this,$this,to[1].toString(),methodOptions);
}
},60);
}
});
},
/* ---------------------------------------- */
/*
plugin stop method
stops scrolling animation
----------------------------------------
usage: $(selector).mCustomScrollbar("stop");
*/
stop:function(){
var selector=functions._selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginPfx)){ /* check if plugin has initialized */
functions._stop.call(null,$this);
}
});
},
/* ---------------------------------------- */
/*
plugin disable method
temporarily disables the scrollbar(s)
----------------------------------------
usage: $(selector).mCustomScrollbar("disable",reset);
reset (boolean): resets content position to 0
*/
disable:function(r){
var selector=functions._selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginPfx)){ /* check if plugin has initialized */
var d=$this.data(pluginPfx),o=d.opt;
functions._autoUpdate.call(this,"remove"); /* remove automatic updating */
functions._unbindEvents.call(this); /* unbind events */
if(r){functions._resetContentPosition.call(this);} /* reset content position */
functions._scrollbarVisibility.call(this,true); /* show/hide scrollbar(s) */
$this.addClass("mCS_disabled"); /* add disable class */
}
});
},
/* ---------------------------------------- */
/*
plugin destroy method
completely removes the scrollbar(s) and returns the element to its original state
----------------------------------------
usage: $(selector).mCustomScrollbar("destroy");
*/
destroy:function(){
var selector=functions._selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginPfx)){ /* check if plugin has initialized */
var d=$this.data(pluginPfx),o=d.opt,
mCustomScrollBox=$("#mCSB_"+d.idx),
mCSB_container=$("#mCSB_"+d.idx+"_container"),
scrollbar=$(".mCSB_"+d.idx+"_scrollbar");
if(o.live){removeLiveTimers(selector);} /* remove live timer */
functions._autoUpdate.call(this,"remove"); /* remove automatic updating */
functions._unbindEvents.call(this); /* unbind events */
functions._resetContentPosition.call(this); /* reset content position */
$this.removeData(pluginPfx); /* remove plugin data object */
functions._delete.call(null,this.mcs); /* delete callbacks object */
/* remove plugin markup */
scrollbar.remove(); /* remove scrollbar(s) first (those can be either inside or outside plugin's inner wrapper) */
mCustomScrollBox.replaceWith(mCSB_container.contents()); /* replace plugin's inner wrapper with the original content */
/* remove plugin classes from the element and add destroy class */
$this.removeClass(pluginNS+" _"+pluginPfx+"_"+d.idx+" mCS-autoHide mCS-dir-rtl mCS_no_scrollbar mCS_disabled").addClass("mCS_destroyed");
}
});
}
/* ---------------------------------------- */
},
/*
----------------------------------------
FUNCTIONS
----------------------------------------
*/
functions={
/* validates selector (if selector is invalid or undefined uses the default one) */
_selector:function(){
return (typeof $(this)!=="object" || $(this).length<1) ? defaultSelector : this;
},
/* -------------------- */
/* changes options according to theme */
_theme:function(obj){
var fixedSizeScrollbarThemes=["rounded","rounded-dark","rounded-dots","rounded-dots-dark"],
nonExpandedScrollbarThemes=["rounded-dots","rounded-dots-dark","3d","3d-dark","3d-thick","3d-thick-dark","inset","inset-dark","inset-2","inset-2-dark","inset-3","inset-3-dark"],
disabledScrollButtonsThemes=["minimal","minimal-dark"],
enabledAutoHideScrollbarThemes=["minimal","minimal-dark"],
scrollbarPositionOutsideThemes=["minimal","minimal-dark"];
obj.autoDraggerLength=$.inArray(obj.theme,fixedSizeScrollbarThemes) > -1 ? false : obj.autoDraggerLength;
obj.autoExpandScrollbar=$.inArray(obj.theme,nonExpandedScrollbarThemes) > -1 ? false : obj.autoExpandScrollbar;
obj.scrollButtons.enable=$.inArray(obj.theme,disabledScrollButtonsThemes) > -1 ? false : obj.scrollButtons.enable;
obj.autoHideScrollbar=$.inArray(obj.theme,enabledAutoHideScrollbarThemes) > -1 ? true : obj.autoHideScrollbar;
obj.scrollbarPosition=$.inArray(obj.theme,scrollbarPositionOutsideThemes) > -1 ? "outside" : obj.scrollbarPosition;
},
/* -------------------- */
/* normalizes axis option to valid values: "y", "x", "yx" */
_findAxis:function(val){
return (val==="yx" || val==="xy" || val==="auto") ? "yx" : (val==="x" || val==="horizontal") ? "x" : "y";
},
/* -------------------- */
/* normalizes scrollButtons.scrollType option to valid values: "stepless", "stepped" */
_findScrollButtonsType:function(val){
return (val==="stepped" || val==="pixels" || val==="step" || val==="click") ? "stepped" : "stepless";
},
/* -------------------- */
/* generates plugin markup */
_pluginMarkup:function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
expandClass=o.autoExpandScrollbar ? " mCSB_scrollTools_onDrag_expand" : "",
scrollbar=["<div id='mCSB_"+d.idx+"_scrollbar_vertical' class='mCSB_scrollTools mCSB_"+d.idx+"_scrollbar mCS-"+o.theme+" mCSB_scrollTools_vertical"+expandClass+"'><div class='mCSB_draggerContainer'><div id='mCSB_"+d.idx+"_dragger_vertical' class='mCSB_dragger' style='position:absolute;' oncontextmenu='return false;'><div class='mCSB_dragger_bar' /></div><div class='mCSB_draggerRail' /></div></div>","<div id='mCSB_"+d.idx+"_scrollbar_horizontal' class='mCSB_scrollTools mCSB_"+d.idx+"_scrollbar mCS-"+o.theme+" mCSB_scrollTools_horizontal"+expandClass+"'><div class='mCSB_draggerContainer'><div id='mCSB_"+d.idx+"_dragger_horizontal' class='mCSB_dragger' style='position:absolute;' oncontextmenu='return false;'><div class='mCSB_dragger_bar' /></div><div class='mCSB_draggerRail' /></div></div>"],
wrapperClass=o.axis==="yx" ? "mCSB_vertical_horizontal" : o.axis==="x" ? "mCSB_horizontal" : "mCSB_vertical",
scrollbars=o.axis==="yx" ? scrollbar[0]+scrollbar[1] : o.axis==="x" ? scrollbar[1] : scrollbar[0],
contentWrapper=o.axis==="yx" ? "<div id='mCSB_"+d.idx+"_container_wrapper' class='mCSB_container_wrapper' />" : "",
autoHideClass=o.autoHideScrollbar ? " mCS-autoHide" : "",
scrollbarDirClass=(o.axis!=="x" && d.langDir==="rtl") ? " mCS-dir-rtl" : "";
if(o.setWidth){$this.css("width",o.setWidth);} /* set element width */
if(o.setHeight){$this.css("height",o.setHeight);} /* set element height */
o.setLeft=(o.axis!=="y" && d.langDir==="rtl") ? "989999px" : o.setLeft; /* adjust left position for rtl direction */
$this.addClass(pluginNS+" _"+pluginPfx+"_"+d.idx+autoHideClass+scrollbarDirClass).wrapInner("<div id='mCSB_"+d.idx+"' class='mCustomScrollBox mCS-"+o.theme+" "+wrapperClass+"'><div id='mCSB_"+d.idx+"_container' class='mCSB_container' style='position:relative; top:"+o.setTop+"; left:"+o.setLeft+";' dir="+d.langDir+" /></div>");
var mCustomScrollBox=$("#mCSB_"+d.idx),
mCSB_container=$("#mCSB_"+d.idx+"_container");
if(o.axis!=="y" && !o.advanced.autoExpandHorizontalScroll){
mCSB_container.css("width",functions._contentWidth(mCSB_container.children()));
}
if(o.scrollbarPosition==="outside"){
if($this.css("position")==="static"){ /* requires elements with non-static position */
$this.css("position","relative");
}
$this.css("overflow","visible");
mCustomScrollBox.addClass("mCSB_outside").after(scrollbars);
}else{
mCustomScrollBox.addClass("mCSB_inside").append(scrollbars);
mCSB_container.wrap(contentWrapper);
}
functions._scrollButtons.call(this); /* add scrollbar buttons */
/* minimum dragger length */
var mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
mCSB_dragger[0].css("min-height",mCSB_dragger[0].height());
mCSB_dragger[1].css("min-width",mCSB_dragger[1].width());
},
/* -------------------- */
/* calculates content width */
_contentWidth:function(el){
return Math.max.apply(Math,el.map(function(){return $(this).outerWidth(true);}).get());
},
/* -------------------- */
/* expands content horizontally */
_expandContentHorizontally:function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mCSB_container=$("#mCSB_"+d.idx+"_container");
if(o.advanced.autoExpandHorizontalScroll && o.axis!=="y"){
/*
wrap content with an infinite width div and set its position to absolute and width to auto.
Setting width to auto before calculating the actual width is important!
We must let the browser set the width as browser zoom values are impossible to calculate.
*/
mCSB_container.css({"position":"absolute","width":"auto"})
.wrap("<div class='mCSB_h_wrapper' style='position:relative; left:0; width:999999px;' />")
.css({ /* set actual width, original position and un-wrap */
/*
get the exact width (with decimals) and then round-up.
Using jquery outerWidth() will round the width value which will mess up with inner elements that have non-integer width
*/
"width":(Math.ceil(mCSB_container[0].getBoundingClientRect().right+0.4)-Math.floor(mCSB_container[0].getBoundingClientRect().left)),
"position":"relative"
}).unwrap();
}
},
/* -------------------- */
/* adds scrollbar buttons */
_scrollButtons:function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mCSB_scrollTools=$(".mCSB_"+d.idx+"_scrollbar:first"),
btnHTML=[
"<a href='#' class='mCSB_buttonUp' oncontextmenu='return false;' />","<a href='#' class='mCSB_buttonDown' oncontextmenu='return false;' />",
"<a href='#' class='mCSB_buttonLeft' oncontextmenu='return false;' />","<a href='#' class='mCSB_buttonRight' oncontextmenu='return false;' />"
],
btn=[(o.axis==="x" ? btnHTML[2] : btnHTML[0]),(o.axis==="x" ? btnHTML[3] : btnHTML[1]),btnHTML[2],btnHTML[3]];
if(o.scrollButtons.enable){
mCSB_scrollTools.prepend(btn[0]).append(btn[1]).next(".mCSB_scrollTools").prepend(btn[2]).append(btn[3]);
}
},
/* -------------------- */
/* detects/sets css max-height value */
_maxHeight:function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mCustomScrollBox=$("#mCSB_"+d.idx),
mh=$this.css("max-height"),pct=mh.indexOf("%")!==-1,
bs=$this.css("box-sizing");
if(mh!=="none"){
var val=pct ? $this.parent().height()*parseInt(mh)/100 : parseInt(mh);
/* if element's css box-sizing is "border-box", subtract any paddings and/or borders from max-height value */
if(bs==="border-box"){val-=(($this.innerHeight()-$this.height())+($this.outerHeight()-$this.innerHeight()));}
mCustomScrollBox.css("max-height",Math.round(val));
}
},
/* -------------------- */
/* auto-adjusts scrollbar dragger length */
_setDraggerLength:function(){
var $this=$(this),d=$this.data(pluginPfx),
mCustomScrollBox=$("#mCSB_"+d.idx),
mCSB_container=$("#mCSB_"+d.idx+"_container"),
mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
ratio=[mCustomScrollBox.height()/mCSB_container.outerHeight(false),mCustomScrollBox.width()/mCSB_container.outerWidth(false)],
l=[
parseInt(mCSB_dragger[0].css("min-height")),Math.round(ratio[0]*mCSB_dragger[0].parent().height()),
parseInt(mCSB_dragger[1].css("min-width")),Math.round(ratio[1]*mCSB_dragger[1].parent().width())
],
h=oldIE && (l[1]<l[0]) ? l[0] : l[1],w=oldIE && (l[3]<l[2]) ? l[2] : l[3];
mCSB_dragger[0].css({
"height":h,"max-height":(mCSB_dragger[0].parent().height()-10)
}).find(".mCSB_dragger_bar").css({"line-height":l[0]+"px"});
mCSB_dragger[1].css({
"width":w,"max-width":(mCSB_dragger[1].parent().width()-10)
});
},
/* -------------------- */
/* calculates scrollbar to content ratio */
_scrollRatio:function(){
var $this=$(this),d=$this.data(pluginPfx),
mCustomScrollBox=$("#mCSB_"+d.idx),
mCSB_container=$("#mCSB_"+d.idx+"_container"),
mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
scrollAmount=[mCSB_container.outerHeight(false)-mCustomScrollBox.height(),mCSB_container.outerWidth(false)-mCustomScrollBox.width()],
ratio=[
scrollAmount[0]/(mCSB_dragger[0].parent().height()-mCSB_dragger[0].height()),
scrollAmount[1]/(mCSB_dragger[1].parent().width()-mCSB_dragger[1].width())
];
d.scrollRatio={y:ratio[0],x:ratio[1]};
},
/* -------------------- */
/* toggles scrolling classes */
_onDragClasses:function(el,action,xpnd){
var expandClass=xpnd ? "mCSB_dragger_onDrag_expanded" : "",classes=["mCSB_dragger_onDrag","mCSB_scrollTools_onDrag"],
scrollbar=el.closest(".mCSB_scrollTools");
if(action==="active"){
el.toggleClass(classes[0]+" "+expandClass); scrollbar.toggleClass(classes[1]);
el[0]._draggable=el[0]._draggable ? 0 : 1;
}else{
if(!el[0]._draggable){
if(action==="hide"){
el.removeClass(classes[0]); scrollbar.removeClass(classes[1]);
}else{
el.addClass(classes[0]); scrollbar.addClass(classes[1]);
}
}
}
},
/* -------------------- */
/* checks if content overflows its container to determine if scrolling is required */
_overflowed:function(){
var $this=$(this),d=$this.data(pluginPfx),
mCustomScrollBox=$("#mCSB_"+d.idx),
mCSB_container=$("#mCSB_"+d.idx+"_container"),
contentHeight=d.overflowed==null ? mCSB_container.height() : mCSB_container.outerHeight(false),
contentWidth=d.overflowed==null ? mCSB_container.width() : mCSB_container.outerWidth(false);
return [contentHeight>mCustomScrollBox.height(),contentWidth>mCustomScrollBox.width()];
},
/* -------------------- */
/* resets content position to 0 */
_resetContentPosition:function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mCustomScrollBox=$("#mCSB_"+d.idx),
mCSB_container=$("#mCSB_"+d.idx+"_container"),
mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
functions._stop($this); /* stop any current scrolling before resetting */
if((o.axis!=="x" && !d.overflowed[0]) || (o.axis==="y" && d.overflowed[0])){mCSB_dragger[0].add(mCSB_container).css("top",0);} /* reset y */
if((o.axis!=="y" && !d.overflowed[1]) || (o.axis==="x" && d.overflowed[1])){ /* reset x */
var cx=dx=0;
if(d.langDir==="rtl"){ /* adjust left position for rtl direction */
cx=mCustomScrollBox.width()-mCSB_container.outerWidth(false);
dx=Math.abs(cx/d.scrollRatio.x);
}
mCSB_container.css("left",cx);
mCSB_dragger[1].css("left",dx);
}
},
/* -------------------- */
/* binds scrollbar events */
_bindEvents:function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt;
if(!d.bindEvents){ /* check if events are already bound */
functions._draggable.call(this);
if(o.contentTouchScroll){functions._contentDraggable.call(this);}
if(o.mouseWheel.enable){ /* bind mousewheel fn when plugin is available */
function _mwt(){
mousewheelTimeout=setTimeout(function(){
if(!$.event.special.mousewheel){
_mwt();
}else{
clearTimeout(mousewheelTimeout);
functions._mousewheel.call($this[0]);
}
},1000);
}
var mousewheelTimeout;
_mwt();
}
functions._draggerRail.call(this);
functions._wrapperScroll.call(this);
if(o.advanced.autoScrollOnFocus){functions._focus.call(this);}
if(o.scrollButtons.enable){functions._buttons.call(this);}
if(o.keyboard.enable){functions._keyboard.call(this);}
d.bindEvents=true;
}
},
/* -------------------- */
/* unbinds scrollbar events */