-
Notifications
You must be signed in to change notification settings - Fork 33
/
jquery.mThumbnailScroller.js
1681 lines (1475 loc) · 63.5 KB
/
jquery.mThumbnailScroller.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 thumbnail scroller plugin ==
Version: 2.0.3
Plugin URI: http://manos.malihu.gr/jquery-thumbnail-scroller
Author: malihu
Author URI: http://manos.malihu.gr
License: MIT License (MIT)
*/
/*
Copyright 2010 Manos Malihutsakis (email: manos@malihu.gr)
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 the minified jquery.mThumbnailScroller.min.js script.
*/
;(function($,window,document){
/*
----------------------------------------
PLUGIN NAMESPACE, PREFIX, DEFAULT SELECTOR(S)
----------------------------------------
*/
var pluginNS="mThumbnailScroller",
pluginPfx="mTS",
defaultSelector=".mThumbnailScroller",
/*
----------------------------------------
DEFAULT OPTIONS
----------------------------------------
*/
defaults={
/*
set element/content width/height programmatically
values: boolean, pixels, percentage
option default
-------------------------------------
setWidth false
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,
/*
set the scrolling type
values (string): "hover-0"/"hover-100" (number indicating percentage), "hover-precise", "click-0"/"click-100" (number indicating percentage), "click-thumb"
*/
type:"hover-50",
/*
scroller axis (vertical and/or horizontal)
values (string): "y", "x", "yx"
*/
axis:"x",
/*
scrolling speed
values: integer (higher=faster)
*/
speed:15,
/*
enable content touch-swipe scrolling
values: boolean, integer, string (number)
integer values define the axis-specific minimum amount required for scrolling momentum
*/
contentTouchScroll:25,
/*
markup option parameters
*/
markup:{
/*
thumbnailsContainer sets the element containing your thumbnails. By default this is an unordered list ("ul")
thumbnailContainer sets the element containing each thumbnail. By default this is a list-item ("li")
thumbnailElement sets the actual thumbnail element. By default this is an image tag ("img")
values: boolean, string
option default
-------------------------------------
thumbnailsContainer null
thumbnailContainer null
thumbnailElement null
*/
/*
set the placeholder element of the buttons
values: boolean, string
*/
buttonsPlaceholder:false,
/*
set buttons HTML
*/
buttonsHTML:{
up:"SVG set 1",
down:"SVG set 1",
left:"SVG set 1",
right:"SVG set 1"
}
},
/*
advanced option parameters
*/
advanced:{
/*
auto-expand content horizontally (for "x" or "yx" axis)
values: boolean
*/
autoExpandHorizontalScroll:true,
/*
auto-update scrollers 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 scrollers each time each image inside the element is fully loaded
values: boolean
*/
updateOnImageLoad:true
/*
auto-update scrollers based on the amount and size changes of specific selectors
useful when you need to update the scroller(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 the scroller each time list-items inside the element are changed)
a value of true (boolean) will auto-update the scroller each time any element is changed
option default
-------------------------------------
updateOnSelectorChange null
*/
},
/*
scroller theme
values: string
*/
theme:"none",
/*
user defined callback functions
*/
callbacks:{
/*
Available callbacks:
callback default
-------------------------------------
onInit null
onScrollStart null
onScroll null
onTotalScroll null
onTotalScrollBack null
whileScrolling null
*/
onTotalScrollOffset:0,
onTotalScrollBackOffset:0,
alwaysTriggerOffsets:true
}
/*
add scroller(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)
liveSelector values: string (selector)
option default
-------------------------------------
live false
liveSelector null
*/
},
/*
----------------------------------------
VARS, CONSTANTS
----------------------------------------
*/
totalInstances=0, /* plugin instances amount */
liveTimers={}, /* live option timers */
oldIE=(window.attachEvent && !window.addEventListener) ? 1 : 0, /* detect IE < 9 */
touchActive=false, /* global touch state (for touch and pointer events) */
touchi, /* touch interface */
/* general plugin classes */
classes=["mTS_disabled","mTS_destroyed","mTS_no_scroll"],
/*
----------------------------------------
METHODS
----------------------------------------
*/
methods={
/*
plugin initialization method
creates the scroller(s), plugin data object and options
----------------------------------------
*/
init:function(options){
var options=$.extend(true,{},defaults,options),
selector=_selector.call(this); /* validate selector */
/*
if live option is enabled, monitor for elements matching the current selector and
apply scroller(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).mThumbnailScroller({live:"off"});
*/
removeLiveTimers(liveSelector);
return;
}
liveTimers[liveSelector]=setTimeout(function(){
/* call mThumbnailScroller fn on live selector(s) every half-second */
$liveSelector.mThumbnailScroller(options);
if(options.live==="once" && $liveSelector.length){
/* disable live after first invocation */
removeLiveTimers(liveSelector);
}
},500);
}else{
removeLiveTimers(liveSelector);
}
/* options normalization */
options.speed=options.speed===0 ? 100 : options.speed;
_theme(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 */
html:null, /* element original html */
overflowed:null, /* overflowed axis */
bindEvents:false, /* object to check if events are bound */
tweenRunning:false, /* object to check if tween is running */
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("mTS").trigger
*/
trigger:null
});
/* HTML data attributes */
var o=$this.data(pluginPfx).opt,
htmlDataAxis=$this.data("mts-axis"),htmlDataType=$this.data("mts-type"),htmlDataTheme=$this.data("mts-theme");
if(htmlDataAxis){o.axis=htmlDataAxis;} /* usage example: data-mts-axis="y" */
if(htmlDataType){o.type=htmlDataType;} /* usage example: data-mts-type="hover-50" */
if(htmlDataTheme){ /* usage example: data-mts-theme="theme-name" */
o.theme=htmlDataTheme;
_theme(o); /* theme-specific options */
}
_pluginMarkup.call(this); /* add plugin markup */
methods.update.call(null,$this); /* call the update method */
}
});
},
/* ---------------------------------------- */
/*
plugin update method
updates content and scroller values, events and status
----------------------------------------
usage: $(selector).mThumbnailScroller("update");
*/
update:function(el){
var selector=el || _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,
mTS_container=$("#mTS_"+d.idx+"_container");
if(!mTS_container.length){return;}
if(d.tweenRunning){_stop($this);} /* stop any running tweens while updating */
/* if element was disabled or destroyed, remove class(es) */
if($this.hasClass(classes[0])){$this.removeClass(classes[0]);}
if($this.hasClass(classes[1])){$this.removeClass(classes[1]);}
_maxHeight.call(this); /* detect/set css max-height value */
_expandContentHorizontally.call(this); /* expand content horizontally */
d.overflowed=_overflowed.call(this); /* determine if scrolling is required */
_buttonsVisibility.call(this); /* show/hide buttons */
_bindEvents.call(this); /* bind scroller events */
/* reset scrolling position and/or events */
var to=[(mTS_container[0].offsetTop),(mTS_container[0].offsetLeft)];
if(o.axis!=="x"){ /* y/yx axis */
if(!d.overflowed[0]){ /* y scrolling is not required */
_resetContentPosition.call(this); /* reset content position */
if(o.axis==="y"){
_scrollTo($this,"0",{dir:"y",dur:0,overwrite:"none"});
_unbindEvents.call(this);
}else if(o.axis==="yx" && d.overflowed[1]){
_scrollTo($this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
}
}else{ /* y scrolling is required */
_scrollTo($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 */
_resetContentPosition.call(this); /* reset content position */
if(o.axis==="x"){
_scrollTo($this,"0",{dir:"x",dur:0,overwrite:"none"});
_unbindEvents.call(this);
}else if(o.axis==="yx" && d.overflowed[0]){
_scrollTo($this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
}
}else{ /* x scrolling is required */
_scrollTo($this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
}
}
/* no scroll class */
if(!d.overflowed[0] && !d.overflowed[1]){
$this.addClass(classes[2]);
}else{
$this.removeClass(classes[2]);
}
_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).mThumbnailScroller("scrollTo",value,options);
*/
scrollTo:function(val,options){
/* prevent silly things like $(selector).mThumbnailScroller("scrollTo",undefined); */
if(typeof val=="undefined" || val==null){return;}
var selector=_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) */
speed:o.speed, /* scrolling speed */
duration:1000, /* animation duration */
easing:"easeInOut", /* animation easing */
timeout:60, /* scroll-to delay */
callbacks:true, /* enable/disable callbacks */
onStart:true,
onUpdate:true,
onComplete:true
},
methodOptions=$.extend(true,{},methodDefaults,options),
to=_arr.call(this,val),dur=methodOptions.duration ? methodOptions.duration : 7000/(methodOptions.speed || 1);
/* translate yx values to actual scroll-to positions */
to[0]=_to.call(this,to[0],"y");
to[1]=_to.call(this,to[1],"x");
methodOptions.dur=dur>0 && dur<17 ? 17 : 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";
_scrollTo($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";
_scrollTo($this,-to[1].toString(),methodOptions);
}
},methodOptions.timeout);
}
});
},
/* ---------------------------------------- */
/*
plugin stop method
stops scrolling animation
----------------------------------------
usage: $(selector).mThumbnailScroller("stop");
*/
stop:function(){
var selector=_selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginPfx)){ /* check if plugin has initialized */
_stop($this);
}
});
},
/* ---------------------------------------- */
/*
plugin disable method
temporarily disables the scroller
----------------------------------------
usage: $(selector).mThumbnailScroller("disable",reset);
reset (boolean): resets content position to 0
*/
disable:function(r){
var selector=_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;
_autoUpdate.call(this,"remove"); /* remove automatic updating */
_unbindEvents.call(this); /* unbind events */
if(r){_resetContentPosition.call(this);} /* reset content position */
_buttonsVisibility.call(this,true); /* show/hide buttons */
$this.addClass(classes[0]); /* add disable class */
}
});
},
/* ---------------------------------------- */
/*
plugin destroy method
completely removes the scrollber and returns the element to its original state
----------------------------------------
usage: $(selector).mThumbnailScroller("destroy");
*/
destroy:function(){
var selector=_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,
mTS_wrapper=$("#mTS_"+d.idx),
mTS_container=$("#mTS_"+d.idx+"_container"),
mTS_buttons=$("#mTS_"+d.idx+"_buttonUp,#mTS_"+d.idx+"_buttonDown,#mTS_"+d.idx+"_buttonLeft,#mTS_"+d.idx+"_buttonRight");
if(o.live){removeLiveTimers(selector);} /* remove live timer */
_autoUpdate.call(this,"remove"); /* remove automatic updating */
_unbindEvents.call(this); /* unbind events */
_resetContentPosition.call(this); /* reset content position */
$this.removeData(pluginPfx); /* remove plugin data object */
_delete(this,"mts"); /* delete callbacks object */
/* remove plugin markup */
mTS_buttons.remove(); /* remove buttons first (those can be either inside or outside plugin's inner wrapper) */
mTS_wrapper.replaceWith(d.html); /* 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+" "+pluginPfx+"-"+o.theme+" "+classes[2]+" "+classes[0]).addClass(classes[1]);
}
});
}
/* ---------------------------------------- */
},
/*
----------------------------------------
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 buttonsPlaceholderOutside=["buttons-out"],
buttonsHtmlSet2=["buttons-in"],
buttonsHtmlSet3=["buttons-out"],
typeHover85=["hover-classic"],
typeHoverPrecise=["hover-full"];
obj.markup.buttonsPlaceholder=$.inArray(obj.theme,buttonsPlaceholderOutside) > -1 ? "outside" : obj.markup.buttonsPlaceholder;
obj.markup.buttonsHTML=$.inArray(obj.theme,buttonsHtmlSet2) > -1 ? {up:"SVG set 2",down:"SVG set 2",left:"SVG set 2",right:"SVG set 2"} : $.inArray(obj.theme,buttonsHtmlSet3) > -1 ? {up:"SVG set 3",down:"SVG set 3",left:"SVG set 3",right:"SVG set 3"} : obj.markup.buttonsHTML;
obj.type=$.inArray(obj.theme,typeHover85) > -1 ? "hover-85" : $.inArray(obj.theme,typeHoverPrecise) > -1 ? "hover-precise" : obj.type;
obj.speed=$.inArray(obj.theme,typeHover85) > -1 ? 60 : $.inArray(obj.theme,typeHoverPrecise) > -1 ? 10 : obj.speed;
},
/* -------------------- */
/* live option timers removal */
removeLiveTimers=function(selector){
if(liveTimers[selector]){
clearTimeout(liveTimers[selector]);
_delete(liveTimers,selector);
}
},
/* -------------------- */
/* generates plugin markup */
_pluginMarkup=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
wrapperClass=o.axis==="yx" ? "mTS_vertical_horizontal" : o.axis==="x" ? "mTS_horizontal" : "mTS_vertical",
thumbsContainer=o.markup.thumbnailsContainer || "ul",
thumbContainer=o.markup.thumbnailContainer || "li",
thumbElement=o.markup.thumbnailElement || "img";
d.html=$this.children().clone(true,true); /* store element original html to restore when destroy method is called */
if(!$this.find(thumbsContainer).length){ /* create images container automatically if not present */
var thumbsAutoContainer=$this.find("li").length ? "<ul class='"+pluginPfx+"AutoContainer' />" : "<div class='"+pluginPfx+"AutoContainer' />";
$this.wrapInner(thumbsAutoContainer);
thumbsContainer="."+pluginPfx+"AutoContainer";
}
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+" "+pluginPfx+"-"+o.theme)
.find(thumbsContainer).wrap("<div id='mTS_"+d.idx+"' class='mTSWrapper "+wrapperClass+"' />")
.addClass(pluginPfx+"Container").attr("id","mTS_"+d.idx+"_container")
.css({"position":"relative","top":o.setTop,"left":o.setLeft})
.find(thumbContainer).addClass(pluginPfx+"ThumbContainer")
.find(thumbElement).addClass(pluginPfx+"Thumb");
_scrollButtons.call(this); /* add scroller buttons */
},
/* -------------------- */
/* expands content horizontally */
_expandContentHorizontally=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mTS_container=$("#mTS_"+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.
*/
mTS_container.css({"position":"absolute","width":"auto"})
.wrap("<div class='mTS_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(mTS_container[0].getBoundingClientRect().right)-Math.floor(mTS_container[0].getBoundingClientRect().left)),
"position":"relative"
}).unwrap();
}
},
/* -------------------- */
/* adds scroller buttons */
_scrollButtons=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
placeholder=!o.markup.buttonsPlaceholder ? $("#mTS_"+d.idx) : o.markup.buttonsPlaceholder==="outside" ? $this : $(o.markup.buttonsPlaceholder),
btnHTML=[
"<a href='#' id='mTS_"+d.idx+"_buttonUp' class='mTSButton mTSButtonUp'><span class='mTSButtonIconContainer'>"+_scrollButtonsIcons.call(this,"up")+"</span></a>",
"<a href='#' id='mTS_"+d.idx+"_buttonDown' class='mTSButton mTSButtonDown'><span class='mTSButtonIconContainer'>"+_scrollButtonsIcons.call(this,"down")+"</span></a>",
"<a href='#' id='mTS_"+d.idx+"_buttonLeft' class='mTSButton mTSButtonLeft'><span class='mTSButtonIconContainer'>"+_scrollButtonsIcons.call(this,"left")+"</span></a>",
"<a href='#' id='mTS_"+d.idx+"_buttonRight' class='mTSButton mTSButtonRight'><span class='mTSButtonIconContainer'>"+_scrollButtonsIcons.call(this,"right")+"</span></a>"
],
btn=[(o.axis==="x" ? btnHTML[2] : btnHTML[0]),(o.axis==="x" ? btnHTML[3] : btnHTML[1])];
if(placeholder.hasClass(pluginNS) && placeholder.css("position")==="static"){ /* requires elements with non-static position */
placeholder.css("position","relative");
}
if(o.type.indexOf("click")!==-1){
if(o.axis!=="x"){
placeholder.append(btnHTML[0]+btnHTML[1]);
}
if(o.axis!=="y"){
placeholder.append(btnHTML[2]+btnHTML[3]);
}
}
},
/* -------------------- */
/* sets scroller buttons icons */
_scrollButtonsIcons=function(b){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
btn=o.markup.buttonsHTML,
i=btn[b]==="SVG set 1" ? 0 : btn[b]==="SVG set 2" ? 1 : btn[b]==="SVG set 3" ? 2 : btn[b]==="SVG set 4" ? 3 : btn[b]==="SVG set 5" ? 4 : null;
switch(b){
case "up":
return i===null ? btn[b] : oldIE ? "↑" : _icons(btn[b])[i][0];
break;
case "down":
return i===null ? btn[b] : oldIE ? "↓" : _icons(btn[b])[i][1];
break;
case "left":
return i===null ? btn[b] : oldIE ? "←" : _icons(btn[b])[i][2];
break;
case "right":
return i===null ? btn[b] : oldIE ? "→" : _icons(btn[b])[i][3];
break;
}
},
/* -------------------- */
/* icons */
_icons=function(){
/* SVG */
var svgo="<svg version='1.1' viewBox='0 0 24 24' preserveAspectRatio='xMinYMin meet' class='mTSButtonIcon'><g><line stroke-width='1' x1='' y1='' x2='' y2='' stroke='#449FDB' opacity=''></line></g>",
svgc="</svg>";
return [
/* set 1 */
[svgo+"<path d='M20.561 9.439l-7.5-7.5c-0.586-0.586-1.535-0.586-2.121 0l-7.5 7.5c-0.586 0.586-0.586 1.536 0 2.121s1.536 0.586 2.121 0l4.939-4.939v14.379c0 0.828 0.672 1.5 1.5 1.5s1.5-0.672 1.5-1.5v-14.379l4.939 4.939c0.293 0.293 0.677 0.439 1.061 0.439s0.768-0.146 1.061-0.439c0.586-0.586 0.586-1.535 0-2.121z'></path>"+svgc,svgo+"<path d='M3.439 14.561l7.5 7.5c0.586 0.586 1.536 0.586 2.121 0l7.5-7.5c0.586-0.586 0.586-1.536 0-2.121s-1.536-0.586-2.121 0l-4.939 4.939v-14.379c0-0.828-0.672-1.5-1.5-1.5s-1.5 0.672-1.5 1.5v14.379l-4.939-4.939c-0.293-0.293-0.677-0.439-1.061-0.439s-0.768 0.146-1.061 0.439c-0.586 0.586-0.586 1.536 0 2.121z'></path>"+svgc,svgo+"<path d='M9.439 3.439l-7.5 7.5c-0.586 0.586-0.586 1.536 0 2.121l7.5 7.5c0.586 0.586 1.536 0.586 2.121 0s0.586-1.536 0-2.121l-4.939-4.939h14.379c0.828 0 1.5-0.672 1.5-1.5s-0.672-1.5-1.5-1.5h-14.379l4.939-4.939c0.293-0.293 0.439-0.677 0.439-1.061s-0.146-0.768-0.439-1.061c-0.586-0.586-1.536-0.586-2.121 0z'></path>"+svgc,svgo+"<path d='M14.561 20.561l7.5-7.5c0.586-0.586 0.586-1.536 0-2.121l-7.5-7.5c-0.586-0.586-1.536-0.586-2.121 0s-0.586 1.536 0 2.121l4.939 4.939h-14.379c-0.828 0-1.5 0.672-1.5 1.5s0.672 1.5 1.5 1.5h14.379l-4.939 4.939c-0.293 0.293-0.439 0.677-0.439 1.061s0.146 0.768 0.439 1.061c0.586 0.586 1.536 0.586 2.121 0z'></path>"+svgc],
/* set 2 */
[svgo+"<path d='M18.58 13.724c-0.488-0.502-5.634-5.402-5.634-5.402-0.262-0.268-0.604-0.402-0.946-0.402-0.343 0-0.685 0.134-0.946 0.402 0 0-5.146 4.901-5.635 5.402-0.488 0.502-0.522 1.404 0 1.939 0.523 0.534 1.252 0.577 1.891 0l4.69-4.496 4.688 4.496c0.641 0.577 1.37 0.534 1.891 0 0.523-0.536 0.491-1.439 0-1.939z'</path>"+svgc,svgo+"<path d='M18.58 10.276c-0.488 0.502-5.634 5.404-5.634 5.404-0.262 0.268-0.604 0.401-0.946 0.401-0.343 0-0.685-0.133-0.946-0.401 0 0-5.146-4.902-5.635-5.404-0.488-0.502-0.522-1.403 0-1.939 0.523-0.535 1.252-0.577 1.891 0l4.69 4.498 4.688-4.496c0.641-0.577 1.37-0.535 1.891 0 0.523 0.535 0.491 1.438 0 1.938z'></path>"+svgc,svgo+"<path d='M13.724 5.419c-0.502 0.49-5.402 5.635-5.402 5.635-0.268 0.262-0.401 0.604-0.401 0.946s0.133 0.684 0.401 0.946c0 0 4.901 5.146 5.402 5.634 0.502 0.49 1.404 0.523 1.939 0 0.534-0.522 0.576-1.25-0.001-1.89l-4.496-4.69 4.496-4.69c0.577-0.641 0.535-1.369 0.001-1.891-0.536-0.522-1.439-0.49-1.939 0z'></path>"+svgc,svgo+"<path d='M10.276 5.419c0.502 0.49 5.402 5.635 5.402 5.635 0.269 0.262 0.402 0.604 0.402 0.946s-0.133 0.684-0.402 0.946c0 0-4.901 5.146-5.402 5.634-0.502 0.49-1.403 0.523-1.939 0-0.535-0.522-0.577-1.25 0-1.89l4.498-4.69-4.496-4.69c-0.577-0.641-0.535-1.369 0-1.891s1.438-0.49 1.938 0z'></path>"+svgc],
/* set 3 */
[svgo+"<path d='M20.902 17.279c0.325 0.322 0.851 0.322 1.175 0 0.325-0.322 0.325-0.841 0-1.163l-9.49-9.396c-0.324-0.322-0.85-0.322-1.174 0l-9.49 9.396c-0.324 0.322-0.325 0.841 0 1.163s0.85 0.322 1.175 0l8.902-8.569 8.902 8.569z'></path>"+svgc,svgo+"<path d='M3.098 6.721c-0.325-0.322-0.851-0.322-1.175 0-0.324 0.32-0.324 0.841 0 1.163l9.49 9.396c0.325 0.322 0.85 0.322 1.175 0l9.49-9.396c0.324-0.322 0.325-0.841 0-1.163s-0.852-0.322-1.175-0.001l-8.903 8.569-8.902-8.568z'></path>"+svgc,svgo+"<path d='M17.279 20.902c0.322 0.325 0.322 0.85 0 1.175s-0.841 0.325-1.163 0l-9.396-9.488c-0.322-0.325-0.322-0.851 0-1.175l9.396-9.49c0.322-0.325 0.841-0.325 1.163 0s0.322 0.85 0 1.175l-8.568 8.902 8.568 8.902z'</path>"+svgc,svgo+"<path d='M6.72 20.902c-0.322 0.325-0.322 0.85 0 1.175s0.841 0.325 1.163 0l9.396-9.488c0.322-0.325 0.322-0.851 0-1.175l-9.396-9.49c-0.322-0.325-0.841-0.325-1.163 0s-0.322 0.85 0 1.175l8.568 8.902-8.568 8.902z'</path>"+svgc],
/* set 4 */
[svgo+"<path d='M12 0l-12 12h7.5v12l9 0v-12h7.5z'></path>"+svgc,svgo+"<path d='M12 24l12-12h-7.5v-12l-9-0v12h-7.5z'></path>"+svgc,svgo+"<path d='M0 12l12 12v-7.5h12l0-9h-12v-7.5z'></path>"+svgc,svgo+"<path d='M24 12l-12-12v7.5h-12l-0 9h12v7.5z'></path>"+svgc],
/* set 5 */
[svgo+"<path d='M6.48 16.8h11.040l-5.521-9.6z'></path>"+svgc,svgo+"<path d='M17.52 7.201l-11.040-0.001 5.52 9.6z'></path>"+svgc,svgo+"<path d='M16.799 6.48l0.001 11.040-9.6-5.52z'></path>"+svgc,svgo+"<path d='M7.201 6.48l-0.001 11.040 9.6-5.52z'></path>"+svgc]
];
},
/* -------------------- */
/* detects/sets css max-height value */
_maxHeight=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mTS_wrapper=$("#mTS_"+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()));}
mTS_wrapper.css("max-height",Math.round(val));
}
},
/* -------------------- */
/* checks if content overflows its container to determine if scrolling is required */
_overflowed=function(){
var $this=$(this),d=$this.data(pluginPfx),
mTS_wrapper=$("#mTS_"+d.idx),
mTS_container=$("#mTS_"+d.idx+"_container");
return [mTS_container.height()>mTS_wrapper.height(),mTS_container.width()>mTS_wrapper.width()];
},
/* -------------------- */
/* resets content position to 0 */
_resetContentPosition=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
mTS_wrapper=$("#mTS_"+d.idx),
mTS_container=$("#mTS_"+d.idx+"_container");
_stop($this); /* stop any current scrolling before resetting */
if((o.axis!=="x" && !d.overflowed[0]) || (o.axis==="y" && d.overflowed[0])){mTS_container.css("top",0);} /* reset y */
if((o.axis!=="y" && !d.overflowed[1]) || (o.axis==="x" && d.overflowed[1])){ /* reset x */
var rp=d.langDir==="rtl" ? mTS_wrapper.width()-mTS_container.width() : 0;
mTS_container.css("left",rp);
}
},
/* -------------------- */
/* binds scroller events */
_bindEvents=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt;
if(!d.bindEvents){ /* check if events are already bound */
if(o.contentTouchScroll){_contentDraggable.call(this);}
if(o.type.indexOf("hover")!==-1){
if(o.type==="hover-precise"){
_hoverPrecise.call(this);
}else{
_hover.call(this);
}
}else if(o.type.indexOf("click")!==-1){
_click.call(this);
}
d.bindEvents=true;
}
},
/* -------------------- */
/* unbinds scroller events */
_unbindEvents=function(){
var $this=$(this),d=$this.data(pluginPfx),
namespace=pluginPfx+"_"+d.idx,
sel=$("#mTS_"+d.idx+",#mTS_"+d.idx+"_container,#mTS_"+d.idx+"_buttonUp,#mTS_"+d.idx+"_buttonDown,#mTS_"+d.idx+"_buttonLeft,#mTS_"+d.idx+"_buttonRight"),
mTS_container=$("#mTS_"+d.idx+"_container");
if(d.bindEvents){ /* check if events are bound */
/* unbind namespaced events from selectors */
sel.each(function(){
$(this).unbind("."+namespace);
});
/* clear and delete timeouts/objects */
clearTimeout(mTS_container[0].onCompleteTimeout); _delete(mTS_container[0],"onCompleteTimeout");
d.bindEvents=false;
}
},
/* -------------------- */
/* toggles scroller buttons visibility */
_buttonsVisibility=function(disabled,hideBtn,dir){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt;
if(o.type.indexOf("click")!==-1){
if(!dir){dir=o.axis;}
var mTS_buttons=[$("#mTS_"+d.idx+"_buttonUp"),$("#mTS_"+d.idx+"_buttonDown"),$("#mTS_"+d.idx+"_buttonLeft"),$("#mTS_"+d.idx+"_buttonRight")],
hideClass=pluginPfx+"-hidden";
if(dir!=="x"){
if(d.overflowed[0] && !disabled && !hideBtn){
sel=[!d.overflowed[1] ? mTS_buttons[2].add(mTS_buttons[3]) : null,mTS_buttons[0].add(mTS_buttons[1])];
}else{
sel=hideBtn===1 ? [mTS_buttons[0],mTS_buttons[1]] : hideBtn===2 ? [mTS_buttons[1],mTS_buttons[0]] : [mTS_buttons[0].add(mTS_buttons[1]),null];
}
}
if(dir!=="y"){
if(d.overflowed[1] && !disabled && !hideBtn){
sel=[!d.overflowed[0] ? mTS_buttons[0].add(mTS_buttons[1]) : null,mTS_buttons[2].add(mTS_buttons[3])];
}else{
sel=hideBtn===1 ? [mTS_buttons[2],mTS_buttons[3]] : hideBtn===2 ? [mTS_buttons[3],mTS_buttons[2]] : [mTS_buttons[2].add(mTS_buttons[3]),null];
}
}
if(sel[0]){sel[0].addClass(hideClass);}
if(sel[1]){sel[1].removeClass(hideClass);}
}
},
/* -------------------- */
/* returns input coordinates of pointer, touch and mouse events (relative to document) */
_coordinates=function(e){
var t=e.type;
switch(t){
case "pointerdown": case "MSPointerDown": case "pointermove": case "MSPointerMove": case "pointerup": case "MSPointerUp":
return [e.originalEvent.pageY,e.originalEvent.pageX];
break;
case "touchstart": case "touchmove": case "touchend":
var touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
return [touch.pageY,touch.pageX];
break;
default:
return [e.pageY,e.pageX];
}
},
/* -------------------- */
/* returns input type */
_inputType=function(e){
return touchi || e.type.indexOf("touch")!==-1 || (typeof e.pointerType!=="undefined" && (e.pointerType===2 || e.pointerType==="touch")) ? "touch" : "mouse";
},
/* -------------------- */
/*
TOUCH SWIPE EVENTS
scrolls content via touch swipe
Emulates the native touch-swipe scrolling with momentum found in iOS, Android and WP devices
*/
_contentDraggable=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
namespace=pluginPfx+"_"+d.idx,
mTS_wrapper=$("#mTS_"+d.idx),
mTS_container=$("#mTS_"+d.idx+"_container"),
dragY,dragX,touchStartY,touchStartX,touchMoveY=[],touchMoveX=[],startTime,runningTime,endTime,distance,speed,amount,
durA=0,durB,overwrite=o.axis==="yx" ? "none" : "all",touchIntent=[];
mTS_container.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
if(!_pointerTouch(e) || touchActive){return;}
$this.removeClass("mTS_touch_action");
var offset=mTS_container.offset();
dragY=_coordinates(e)[0]-offset.top;
dragX=_coordinates(e)[1]-offset.left;
touchIntent=[_coordinates(e)[0],_coordinates(e)[1]];
}).bind("touchmove."+namespace+" pointermove."+namespace+" MSPointerMove."+namespace,function(e){
if(!_pointerTouch(e) || touchActive){return;}
e.stopImmediatePropagation();
runningTime=_getTime();
var offset=mTS_wrapper.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left,
easing="linearOut";
touchMoveY.push(y);
touchMoveX.push(x);
touchIntent[2]=Math.abs(_coordinates(e)[0]-touchIntent[0]); touchIntent[3]=Math.abs(_coordinates(e)[1]-touchIntent[1]);
if(d.overflowed[0]){
var limit=mTS_wrapper.height()-mTS_container.height(),
prevent=((dragY-y)>0 && (y-dragY)>limit && (touchIntent[3]*2<touchIntent[2]));
}
if(d.overflowed[1]){
var limitX=mTS_wrapper.width()-mTS_container.width(),
preventX=((dragX-x)>0 && (x-dragX)>limitX && (touchIntent[2]*2<touchIntent[3]));
}
if(prevent || preventX){e.preventDefault();}else{$this.addClass("mTS_touch_action");} /* prevent native document scrolling */
amount=o.axis==="yx" ? [(dragY-y),(dragX-x)] : o.axis==="x" ? [null,(dragX-x)] : [(dragY-y),null];
mTS_container[0].idleTimer=250;
if(d.overflowed[0]){_drag(amount[0],durA,easing,"y","all");}
if(d.overflowed[1]){_drag(amount[1],durA,easing,"x",overwrite);}
});
mTS_wrapper.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
if(!_pointerTouch(e) || touchActive){return;}
e.stopImmediatePropagation();
touchi=true;
_stop($this);
startTime=_getTime();
var offset=mTS_wrapper.offset();
touchStartY=_coordinates(e)[0]-offset.top;
touchStartX=_coordinates(e)[1]-offset.left;
touchMoveY=[]; touchMoveX=[];
}).bind("touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace,function(e){
if(!_pointerTouch(e) || touchActive){return;}
e.stopImmediatePropagation();
endTime=_getTime();
var offset=mTS_wrapper.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left;
if((endTime-runningTime)>30){return;}
speed=1000/(endTime-startTime);
var easing="easeOut",slow=speed<2.5,
diff=slow ? [touchMoveY[touchMoveY.length-2],touchMoveX[touchMoveX.length-2]] : [0,0];
distance=slow ? [(y-diff[0]),(x-diff[1])] : [y-touchStartY,x-touchStartX];
var absDistance=[Math.abs(distance[0]),Math.abs(distance[1])];
speed=slow ? [Math.abs(distance[0]/4),Math.abs(distance[1]/4)] : [speed,speed];
var a=[
Math.abs(mTS_container[0].offsetTop)-(distance[0]*_m((absDistance[0]/speed[0]),speed[0])),
Math.abs(mTS_container[0].offsetLeft)-(distance[1]*_m((absDistance[1]/speed[1]),speed[1]))
];
amount=o.axis==="yx" ? [a[0],a[1]] : o.axis==="x" ? [null,a[1]] : [a[0],null];
durB=[(absDistance[0]*4)+(o.speed*60),(absDistance[1]*4)+(o.speed*60)];
var md=parseInt(o.contentTouchScroll) || 0; /* absolute minimum distance required */
amount[0]=absDistance[0]>md ? amount[0] : 0;
amount[1]=absDistance[1]>md ? amount[1] : 0;
if(d.overflowed[0]){_drag(amount[0],durB[0],easing,"y",overwrite);}
if(d.overflowed[1]){_drag(amount[1],durB[1],easing,"x",overwrite);}
});
function _m(ds,s){
var r=[s*1.5,s*2,s/1.5,s/2];
if(ds>90){
return s>4 ? r[0] : r[3];
}else if(ds>60){
return s>3 ? r[3] : r[2];
}else if(ds>30){
return s>8 ? r[1] : s>6 ? r[0] : s>4 ? s : r[2];
}else{
return s>8 ? s : r[3];
}
}
function _drag(amount,dur,easing,dir,overwrite){
if(!amount){return;}
_scrollTo($this,-amount.toString(),{dur:dur,easing:easing,dir:dir,overwrite:overwrite});
}
},
/* -------------------- */
/*
HOVER PRECISE EVENT
scrolls content via cursor movement
*/
_hoverPrecise=function(){
var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
namespace=pluginPfx+"_"+d.idx,
mTS_wrapper=$("#mTS_"+d.idx),
mTS_container=$("#mTS_"+d.idx+"_container"),
evt=window.navigator.pointerEnabled ? "pointermove" : window.navigator.msPointerEnabled ? "MSPointerMove" : "mousemove",
cursor,dest,to;
mTS_wrapper.bind(evt+"."+namespace,function(e){
if(_inputType(e.originalEvent || e)==="touch" || (!d.overflowed[0] && !d.overflowed[1])){return;}
e.preventDefault();
var wrapperHeight=mTS_wrapper.height(),containerHeight=mTS_container.height(),
wrapperWidth=mTS_wrapper.width(),containerWidth=mTS_container.width(),
speed=((containerWidth/wrapperWidth)*7000)/(o.speed || 1);
cursor=[_coordinates(e)[0]-mTS_wrapper.offset().top,_coordinates(e)[1]-mTS_wrapper.offset().left];
dest=[cursor[0]/mTS_wrapper.height(),cursor[1]/mTS_wrapper.width()];
to=[Math.round(-((containerHeight-wrapperHeight)*(dest[0]))),Math.round(-((containerWidth-wrapperWidth)*(dest[1])))];
if(o.axis!=="x" && d.overflowed[0]){
_scrollTo($this,to[0].toString(),{dir:"y",dur:speed,easing:"easeOut"});
}
if(o.axis!=="y" && d.overflowed[1]){
_scrollTo($this,to[1].toString(),{dir:"x",dur:speed,easing:"easeOut"});
}
});
},
/* -------------------- */