jquery.mCustomScrollbar.nomiin.js 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. /*
  2. == malihu jquery custom scrollbar plugin ==
  3. Version: 3.0.7
  4. Plugin URI: http://manos.malihu.gr/jquery-custom-content-scroller
  5. Author: malihu
  6. Author URI: http://manos.malihu.gr
  7. License: MIT License (MIT)
  8. */
  9. /*
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. */
  26. /*
  27. The code below is fairly long, fully commented and should be normally used in development.
  28. For production, use either the minified jquery.mCustomScrollbar.min.js script or
  29. the production-ready jquery.mCustomScrollbar.concat.min.js which contains the plugin
  30. and dependencies (minified).
  31. */
  32. ;(function($,window,document){
  33. (function(init){
  34. var _rjs=typeof define==="function" && define.amd, /* RequireJS */
  35. _dlp=("https:"==document.location.protocol) ? "https:" : "http:", /* location protocol */
  36. _url="cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.12/jquery.mousewheel.min.js";
  37. if(!_rjs){
  38. /* load jquery-mousewheel plugin (via CDN) if it's not present or not loaded via RequireJS
  39. (works when mCustomScrollbar fn is called on window load) */
  40. $.event.special.mousewheel || $("head").append(decodeURI("%3Cscript src="+_dlp+"//"+_url+"%3E%3C/script%3E"));
  41. }
  42. init();
  43. }(function(){
  44. /*
  45. ----------------------------------------
  46. PLUGIN NAMESPACE, PREFIX, DEFAULT SELECTOR(S)
  47. ----------------------------------------
  48. */
  49. var pluginNS="mCustomScrollbar",
  50. pluginPfx="mCS",
  51. defaultSelector=".mCustomScrollbar",
  52. /*
  53. ----------------------------------------
  54. DEFAULT OPTIONS
  55. ----------------------------------------
  56. */
  57. defaults={
  58. /*
  59. set element/content width/height programmatically
  60. values: boolean, pixels, percentage
  61. option default
  62. -------------------------------------
  63. setWidth false
  64. setHeight false
  65. */
  66. /*
  67. set the initial css top property of content
  68. values: string (e.g. "-100px", "10%" etc.)
  69. */
  70. setTop:0,
  71. /*
  72. set the initial css left property of content
  73. values: string (e.g. "-100px", "10%" etc.)
  74. */
  75. setLeft:0,
  76. /*
  77. scrollbar axis (vertical and/or horizontal scrollbars)
  78. values (string): "y", "x", "yx"
  79. */
  80. axis:"y",
  81. /*
  82. position of scrollbar relative to content
  83. values (string): "inside", "outside" ("outside" requires elements with position:relative)
  84. */
  85. scrollbarPosition:"inside",
  86. /*
  87. scrolling inertia
  88. values: integer (milliseconds)
  89. */
  90. scrollInertia:950,
  91. /*
  92. auto-adjust scrollbar dragger length
  93. values: boolean
  94. */
  95. autoDraggerLength:true,
  96. /*
  97. auto-hide scrollbar when idle
  98. values: boolean
  99. option default
  100. -------------------------------------
  101. autoHideScrollbar false
  102. */
  103. /*
  104. auto-expands scrollbar on mouse-over and dragging
  105. values: boolean
  106. option default
  107. -------------------------------------
  108. autoExpandScrollbar false
  109. */
  110. /*
  111. always show scrollbar, even when there's nothing to scroll
  112. values: integer (0=disable, 1=always show dragger rail and buttons, 2=always show dragger rail, dragger and buttons), boolean
  113. */
  114. alwaysShowScrollbar:0,
  115. /*
  116. scrolling always snaps to a multiple of this number in pixels
  117. values: integer
  118. option default
  119. -------------------------------------
  120. snapAmount null
  121. */
  122. /*
  123. when snapping, snap with this number in pixels as an offset
  124. values: integer
  125. */
  126. snapOffset:0,
  127. /*
  128. mouse-wheel scrolling
  129. */
  130. mouseWheel:{
  131. /*
  132. enable mouse-wheel scrolling
  133. values: boolean
  134. */
  135. enable:true,
  136. /*
  137. scrolling amount in pixels
  138. values: "auto", integer
  139. */
  140. scrollAmount:"auto",
  141. /*
  142. mouse-wheel scrolling axis
  143. the default scrolling direction when both vertical and horizontal scrollbars are present
  144. values (string): "y", "x"
  145. */
  146. axis:"y",
  147. /*
  148. prevent the default behaviour which automatically scrolls the parent element(s) when end of scrolling is reached
  149. values: boolean
  150. option default
  151. -------------------------------------
  152. preventDefault null
  153. */
  154. /*
  155. the reported mouse-wheel delta value. The number of lines (translated to pixels) one wheel notch scrolls.
  156. values: "auto", integer
  157. "auto" uses the default OS/browser value
  158. */
  159. deltaFactor:"auto",
  160. /*
  161. normalize mouse-wheel delta to -1 or 1 (disables mouse-wheel acceleration)
  162. values: boolean
  163. option default
  164. -------------------------------------
  165. normalizeDelta null
  166. */
  167. /*
  168. invert mouse-wheel scrolling direction
  169. values: boolean
  170. option default
  171. -------------------------------------
  172. invert null
  173. */
  174. /*
  175. the tags that disable mouse-wheel when cursor is over them
  176. */
  177. disableOver:["select","option","keygen","datalist","textarea"]
  178. },
  179. /*
  180. scrollbar buttons
  181. */
  182. scrollButtons:{
  183. /*
  184. enable scrollbar buttons
  185. values: boolean
  186. option default
  187. -------------------------------------
  188. enable null
  189. */
  190. /*
  191. scrollbar buttons scrolling type
  192. values (string): "stepless", "stepped"
  193. */
  194. scrollType:"stepless",
  195. /*
  196. scrolling amount in pixels
  197. values: "auto", integer
  198. */
  199. scrollAmount:"auto"
  200. /*
  201. tabindex of the scrollbar buttons
  202. values: false, integer
  203. option default
  204. -------------------------------------
  205. tabindex null
  206. */
  207. },
  208. /*
  209. keyboard scrolling
  210. */
  211. keyboard:{
  212. /*
  213. enable scrolling via keyboard
  214. values: boolean
  215. */
  216. enable:true,
  217. /*
  218. keyboard scrolling type
  219. values (string): "stepless", "stepped"
  220. */
  221. scrollType:"stepless",
  222. /*
  223. scrolling amount in pixels
  224. values: "auto", integer
  225. */
  226. scrollAmount:"auto"
  227. },
  228. /*
  229. enable content touch-swipe scrolling
  230. values: boolean, integer, string (number)
  231. integer values define the axis-specific minimum amount required for scrolling momentum
  232. */
  233. contentTouchScroll:25,
  234. /*
  235. advanced option parameters
  236. */
  237. advanced:{
  238. /*
  239. auto-expand content horizontally (for "x" or "yx" axis)
  240. values: boolean
  241. option default
  242. -------------------------------------
  243. autoExpandHorizontalScroll null
  244. */
  245. /*
  246. auto-scroll to elements with focus
  247. */
  248. autoScrollOnFocus:"input,textarea,select,button,datalist,keygen,a[tabindex],area,object,[contenteditable='true']",
  249. /*
  250. auto-update scrollbars on content, element or viewport resize
  251. should be true for fluid layouts/elements, adding/removing content dynamically, hiding/showing elements, content with images etc.
  252. values: boolean
  253. */
  254. updateOnContentResize:true,
  255. /*
  256. auto-update scrollbars each time each image inside the element is fully loaded
  257. values: boolean
  258. */
  259. updateOnImageLoad:true
  260. /*
  261. auto-update scrollbars based on the amount and size changes of specific selectors
  262. useful when you need to update the scrollbar(s) automatically, each time a type of element is added, removed or changes its size
  263. values: boolean, string (e.g. "ul li" will auto-update scrollbars each time list-items inside the element are changed)
  264. a value of true (boolean) will auto-update scrollbars each time any element is changed
  265. option default
  266. -------------------------------------
  267. updateOnSelectorChange null
  268. */
  269. /*
  270. extra selectors that'll release scrollbar dragging upon mouseup, pointerup, touchend etc. (e.g. "selector-1, selector-2")
  271. option default
  272. -------------------------------------
  273. releaseDraggableSelectors null
  274. */
  275. },
  276. /*
  277. scrollbar theme
  278. values: string (see CSS/plugin URI for a list of ready-to-use themes)
  279. */
  280. theme:"light",
  281. /*
  282. user defined callback functions
  283. */
  284. callbacks:{
  285. /*
  286. Available callbacks:
  287. callback default
  288. -------------------------------------
  289. onInit null
  290. onScrollStart null
  291. onScroll null
  292. onTotalScroll null
  293. onTotalScrollBack null
  294. whileScrolling null
  295. onOverflowY null
  296. onOverflowX null
  297. onOverflowYNone null
  298. onOverflowXNone null
  299. onImageLoad null
  300. onSelectorChange null
  301. onUpdate null
  302. */
  303. onTotalScrollOffset:0,
  304. onTotalScrollBackOffset:0,
  305. alwaysTriggerOffsets:true
  306. }
  307. /*
  308. add scrollbar(s) on all elements matching the current selector, now and in the future
  309. values: boolean, string
  310. string values: "on" (enable), "once" (disable after first invocation), "off" (disable)
  311. liveSelector values: string (selector)
  312. option default
  313. -------------------------------------
  314. live false
  315. liveSelector null
  316. */
  317. },
  318. /*
  319. ----------------------------------------
  320. VARS, CONSTANTS
  321. ----------------------------------------
  322. */
  323. totalInstances=0, /* plugin instances amount */
  324. liveTimers={}, /* live option timers */
  325. oldIE=(window.attachEvent && !window.addEventListener) ? 1 : 0, /* detect IE < 9 */
  326. touchActive=false,touchable, /* global touch vars (for touch and pointer events) */
  327. /* general plugin classes */
  328. classes=[
  329. "mCSB_dragger_onDrag","mCSB_scrollTools_onDrag","mCS_img_loaded","mCS_disabled","mCS_destroyed","mCS_no_scrollbar",
  330. "mCS-autoHide","mCS-dir-rtl","mCS_no_scrollbar_y","mCS_no_scrollbar_x","mCS_y_hidden","mCS_x_hidden","mCSB_draggerContainer",
  331. "mCSB_buttonUp","mCSB_buttonDown","mCSB_buttonLeft","mCSB_buttonRight"
  332. ],
  333. /*
  334. ----------------------------------------
  335. METHODS
  336. ----------------------------------------
  337. */
  338. methods={
  339. /*
  340. plugin initialization method
  341. creates the scrollbar(s), plugin data object and options
  342. ----------------------------------------
  343. */
  344. init:function(options){
  345. var options=$.extend(true,{},defaults,options),
  346. selector=_selector.call(this); /* validate selector */
  347. /*
  348. if live option is enabled, monitor for elements matching the current selector and
  349. apply scrollbar(s) when found (now and in the future)
  350. */
  351. if(options.live){
  352. var liveSelector=options.liveSelector || this.selector || defaultSelector, /* live selector(s) */
  353. $liveSelector=$(liveSelector); /* live selector(s) as jquery object */
  354. if(options.live==="off"){
  355. /*
  356. disable live if requested
  357. usage: $(selector).mCustomScrollbar({live:"off"});
  358. */
  359. removeLiveTimers(liveSelector);
  360. return;
  361. }
  362. liveTimers[liveSelector]=setTimeout(function(){
  363. /* call mCustomScrollbar fn on live selector(s) every half-second */
  364. $liveSelector.mCustomScrollbar(options);
  365. if(options.live==="once" && $liveSelector.length){
  366. /* disable live after first invocation */
  367. removeLiveTimers(liveSelector);
  368. }
  369. },500);
  370. }else{
  371. removeLiveTimers(liveSelector);
  372. }
  373. /* options backward compatibility (for versions < 3.0.0) and normalization */
  374. options.setWidth=(options.set_width) ? options.set_width : options.setWidth;
  375. options.setHeight=(options.set_height) ? options.set_height : options.setHeight;
  376. options.axis=(options.horizontalScroll) ? "x" : _findAxis(options.axis);
  377. options.scrollInertia=options.scrollInertia>0 && options.scrollInertia<17 ? 17 : options.scrollInertia;
  378. if(typeof options.mouseWheel!=="object" && options.mouseWheel==true){ /* old school mouseWheel option (non-object) */
  379. options.mouseWheel={enable:true,scrollAmount:"auto",axis:"y",preventDefault:false,deltaFactor:"auto",normalizeDelta:false,invert:false}
  380. }
  381. options.mouseWheel.scrollAmount=!options.mouseWheelPixels ? options.mouseWheel.scrollAmount : options.mouseWheelPixels;
  382. options.mouseWheel.normalizeDelta=!options.advanced.normalizeMouseWheelDelta ? options.mouseWheel.normalizeDelta : options.advanced.normalizeMouseWheelDelta;
  383. options.scrollButtons.scrollType=_findScrollButtonsType(options.scrollButtons.scrollType);
  384. _theme(options); /* theme-specific options */
  385. /* plugin constructor */
  386. return $(selector).each(function(){
  387. var $this=$(this);
  388. if(!$this.data(pluginPfx)){ /* prevent multiple instantiations */
  389. /* store options and create objects in jquery data */
  390. $this.data(pluginPfx,{
  391. idx:++totalInstances, /* instance index */
  392. opt:options, /* options */
  393. scrollRatio:{y:null,x:null}, /* scrollbar to content ratio */
  394. overflowed:null, /* overflowed axis */
  395. contentReset:{y:null,x:null}, /* object to check when content resets */
  396. bindEvents:false, /* object to check if events are bound */
  397. tweenRunning:false, /* object to check if tween is running */
  398. sequential:{}, /* sequential scrolling object */
  399. langDir:$this.css("direction"), /* detect/store direction (ltr or rtl) */
  400. cbOffsets:null, /* object to check whether callback offsets always trigger */
  401. /*
  402. object to check how scrolling events where last triggered
  403. "internal" (default - triggered by this script), "external" (triggered by other scripts, e.g. via scrollTo method)
  404. usage: object.data("mCS").trigger
  405. */
  406. trigger:null
  407. });
  408. var d=$this.data(pluginPfx),o=d.opt,
  409. /* HTML data attributes */
  410. htmlDataAxis=$this.data("mcs-axis"),htmlDataSbPos=$this.data("mcs-scrollbar-position"),htmlDataTheme=$this.data("mcs-theme");
  411. if(htmlDataAxis){o.axis=htmlDataAxis;} /* usage example: data-mcs-axis="y" */
  412. if(htmlDataSbPos){o.scrollbarPosition=htmlDataSbPos;} /* usage example: data-mcs-scrollbar-position="outside" */
  413. if(htmlDataTheme){ /* usage example: data-mcs-theme="minimal" */
  414. o.theme=htmlDataTheme;
  415. _theme(o); /* theme-specific options */
  416. }
  417. _pluginMarkup.call(this); /* add plugin markup */
  418. $("#mCSB_"+d.idx+"_container img:not(."+classes[2]+")").addClass(classes[2]); /* flag loaded images */
  419. methods.update.call(null,$this); /* call the update method */
  420. }
  421. });
  422. },
  423. /* ---------------------------------------- */
  424. /*
  425. plugin update method
  426. updates content and scrollbar(s) values, events and status
  427. ----------------------------------------
  428. usage: $(selector).mCustomScrollbar("update");
  429. */
  430. update:function(el,cb){
  431. var selector=el || _selector.call(this); /* validate selector */
  432. return $(selector).each(function(){
  433. var $this=$(this);
  434. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  435. var d=$this.data(pluginPfx),o=d.opt,
  436. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  437. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
  438. if(!mCSB_container.length){return;}
  439. if(d.tweenRunning){_stop($this);} /* stop any running tweens while updating */
  440. /* if element was disabled or destroyed, remove class(es) */
  441. if($this.hasClass(classes[3])){$this.removeClass(classes[3]);}
  442. if($this.hasClass(classes[4])){$this.removeClass(classes[4]);}
  443. _maxHeight.call(this); /* detect/set css max-height value */
  444. _expandContentHorizontally.call(this); /* expand content horizontally */
  445. if(o.axis!=="y" && !o.advanced.autoExpandHorizontalScroll){
  446. mCSB_container.css("width",_contentWidth(mCSB_container.children()));
  447. }
  448. d.overflowed=_overflowed.call(this); /* determine if scrolling is required */
  449. _scrollbarVisibility.call(this); /* show/hide scrollbar(s) */
  450. /* auto-adjust scrollbar dragger length analogous to content */
  451. if(o.autoDraggerLength){_setDraggerLength.call(this);}
  452. _scrollRatio.call(this); /* calculate and store scrollbar to content ratio */
  453. _bindEvents.call(this); /* bind scrollbar events */
  454. /* reset scrolling position and/or events */
  455. var to=[Math.abs(mCSB_container[0].offsetTop),Math.abs(mCSB_container[0].offsetLeft)];
  456. if(o.axis!=="x"){ /* y/yx axis */
  457. if(!d.overflowed[0]){ /* y scrolling is not required */
  458. _resetContentPosition.call(this); /* reset content position */
  459. if(o.axis==="y"){
  460. _unbindEvents.call(this);
  461. }else if(o.axis==="yx" && d.overflowed[1]){
  462. _scrollTo($this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
  463. }
  464. }else if(mCSB_dragger[0].height()>mCSB_dragger[0].parent().height()){
  465. _resetContentPosition.call(this); /* reset content position */
  466. }else{ /* y scrolling is required */
  467. _scrollTo($this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
  468. d.contentReset.y=null;
  469. }
  470. }
  471. if(o.axis!=="y"){ /* x/yx axis */
  472. if(!d.overflowed[1]){ /* x scrolling is not required */
  473. _resetContentPosition.call(this); /* reset content position */
  474. if(o.axis==="x"){
  475. _unbindEvents.call(this);
  476. }else if(o.axis==="yx" && d.overflowed[0]){
  477. _scrollTo($this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
  478. }
  479. }else if(mCSB_dragger[1].width()>mCSB_dragger[1].parent().width()){
  480. _resetContentPosition.call(this); /* reset content position */
  481. }else{ /* x scrolling is required */
  482. _scrollTo($this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
  483. d.contentReset.x=null;
  484. }
  485. }
  486. /* callbacks: onImageLoad, onSelectorChange, onUpdate */
  487. if(cb && d){
  488. if(cb===2 && o.callbacks.onImageLoad && typeof o.callbacks.onImageLoad==="function"){
  489. o.callbacks.onImageLoad.call(this);
  490. }else if(cb===3 && o.callbacks.onSelectorChange && typeof o.callbacks.onSelectorChange==="function"){
  491. o.callbacks.onSelectorChange.call(this);
  492. }else if(o.callbacks.onUpdate && typeof o.callbacks.onUpdate==="function"){
  493. o.callbacks.onUpdate.call(this);
  494. }
  495. }
  496. _autoUpdate.call(this); /* initialize automatic updating (for dynamic content, fluid layouts etc.) */
  497. }
  498. });
  499. },
  500. /* ---------------------------------------- */
  501. /*
  502. plugin scrollTo method
  503. triggers a scrolling event to a specific value
  504. ----------------------------------------
  505. usage: $(selector).mCustomScrollbar("scrollTo",value,options);
  506. */
  507. scrollTo:function(val,options){
  508. /* prevent silly things like $(selector).mCustomScrollbar("scrollTo",undefined); */
  509. if(typeof val=="undefined" || val==null){return;}
  510. var selector=_selector.call(this); /* validate selector */
  511. return $(selector).each(function(){
  512. var $this=$(this);
  513. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  514. var d=$this.data(pluginPfx),o=d.opt,
  515. /* method default options */
  516. methodDefaults={
  517. trigger:"external", /* method is by default triggered externally (e.g. from other scripts) */
  518. scrollInertia:o.scrollInertia, /* scrolling inertia (animation duration) */
  519. scrollEasing:"mcsEaseInOut", /* animation easing */
  520. moveDragger:false, /* move dragger instead of content */
  521. timeout:60, /* scroll-to delay */
  522. callbacks:true, /* enable/disable callbacks */
  523. onStart:true,
  524. onUpdate:true,
  525. onComplete:true
  526. },
  527. methodOptions=$.extend(true,{},methodDefaults,options),
  528. to=_arr.call(this,val),dur=methodOptions.scrollInertia>0 && methodOptions.scrollInertia<17 ? 17 : methodOptions.scrollInertia;
  529. /* translate yx values to actual scroll-to positions */
  530. to[0]=_to.call(this,to[0],"y");
  531. to[1]=_to.call(this,to[1],"x");
  532. /*
  533. check if scroll-to value moves the dragger instead of content.
  534. Only pixel values apply on dragger (e.g. 100, "100px", "-=100" etc.)
  535. */
  536. if(methodOptions.moveDragger){
  537. to[0]*=d.scrollRatio.y;
  538. to[1]*=d.scrollRatio.x;
  539. }
  540. methodOptions.dur=dur;
  541. setTimeout(function(){
  542. /* do the scrolling */
  543. if(to[0]!==null && typeof to[0]!=="undefined" && o.axis!=="x" && d.overflowed[0]){ /* scroll y */
  544. methodOptions.dir="y";
  545. methodOptions.overwrite="all";
  546. _scrollTo($this,to[0].toString(),methodOptions);
  547. }
  548. if(to[1]!==null && typeof to[1]!=="undefined" && o.axis!=="y" && d.overflowed[1]){ /* scroll x */
  549. methodOptions.dir="x";
  550. methodOptions.overwrite="none";
  551. _scrollTo($this,to[1].toString(),methodOptions);
  552. }
  553. },methodOptions.timeout);
  554. }
  555. });
  556. },
  557. /* ---------------------------------------- */
  558. /*
  559. plugin stop method
  560. stops scrolling animation
  561. ----------------------------------------
  562. usage: $(selector).mCustomScrollbar("stop");
  563. */
  564. stop:function(){
  565. var selector=_selector.call(this); /* validate selector */
  566. return $(selector).each(function(){
  567. var $this=$(this);
  568. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  569. _stop($this);
  570. }
  571. });
  572. },
  573. /* ---------------------------------------- */
  574. /*
  575. plugin disable method
  576. temporarily disables the scrollbar(s)
  577. ----------------------------------------
  578. usage: $(selector).mCustomScrollbar("disable",reset);
  579. reset (boolean): resets content position to 0
  580. */
  581. disable:function(r){
  582. var selector=_selector.call(this); /* validate selector */
  583. return $(selector).each(function(){
  584. var $this=$(this);
  585. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  586. var d=$this.data(pluginPfx);
  587. _autoUpdate.call(this,"remove"); /* remove automatic updating */
  588. _unbindEvents.call(this); /* unbind events */
  589. if(r){_resetContentPosition.call(this);} /* reset content position */
  590. _scrollbarVisibility.call(this,true); /* show/hide scrollbar(s) */
  591. $this.addClass(classes[3]); /* add disable class */
  592. }
  593. });
  594. },
  595. /* ---------------------------------------- */
  596. /*
  597. plugin destroy method
  598. completely removes the scrollbar(s) and returns the element to its original state
  599. ----------------------------------------
  600. usage: $(selector).mCustomScrollbar("destroy");
  601. */
  602. destroy:function(){
  603. var selector=_selector.call(this); /* validate selector */
  604. return $(selector).each(function(){
  605. var $this=$(this);
  606. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  607. var d=$this.data(pluginPfx),o=d.opt,
  608. mCustomScrollBox=$("#mCSB_"+d.idx),
  609. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  610. scrollbar=$(".mCSB_"+d.idx+"_scrollbar");
  611. if(o.live){removeLiveTimers(o.liveSelector || $(selector).selector);} /* remove live timers */
  612. _autoUpdate.call(this,"remove"); /* remove automatic updating */
  613. _unbindEvents.call(this); /* unbind events */
  614. _resetContentPosition.call(this); /* reset content position */
  615. $this.removeData(pluginPfx); /* remove plugin data object */
  616. _delete(this,"mcs"); /* delete callbacks object */
  617. /* remove plugin markup */
  618. scrollbar.remove(); /* remove scrollbar(s) first (those can be either inside or outside plugin's inner wrapper) */
  619. mCSB_container.find("img."+classes[2]).removeClass(classes[2]); /* remove loaded images flag */
  620. mCustomScrollBox.replaceWith(mCSB_container.contents()); /* replace plugin's inner wrapper with the original content */
  621. /* remove plugin classes from the element and add destroy class */
  622. $this.removeClass(pluginNS+" _"+pluginPfx+"_"+d.idx+" "+classes[6]+" "+classes[7]+" "+classes[5]+" "+classes[3]).addClass(classes[4]);
  623. }
  624. });
  625. }
  626. /* ---------------------------------------- */
  627. },
  628. /*
  629. ----------------------------------------
  630. FUNCTIONS
  631. ----------------------------------------
  632. */
  633. /* validates selector (if selector is invalid or undefined uses the default one) */
  634. _selector=function(){
  635. return (typeof $(this)!=="object" || $(this).length<1) ? defaultSelector : this;
  636. },
  637. /* -------------------- */
  638. /* changes options according to theme */
  639. _theme=function(obj){
  640. var fixedSizeScrollbarThemes=["rounded","rounded-dark","rounded-dots","rounded-dots-dark"],
  641. 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"],
  642. disabledScrollButtonsThemes=["minimal","minimal-dark"],
  643. enabledAutoHideScrollbarThemes=["minimal","minimal-dark"],
  644. scrollbarPositionOutsideThemes=["minimal","minimal-dark"];
  645. obj.autoDraggerLength=$.inArray(obj.theme,fixedSizeScrollbarThemes) > -1 ? false : obj.autoDraggerLength;
  646. obj.autoExpandScrollbar=$.inArray(obj.theme,nonExpandedScrollbarThemes) > -1 ? false : obj.autoExpandScrollbar;
  647. obj.scrollButtons.enable=$.inArray(obj.theme,disabledScrollButtonsThemes) > -1 ? false : obj.scrollButtons.enable;
  648. obj.autoHideScrollbar=$.inArray(obj.theme,enabledAutoHideScrollbarThemes) > -1 ? true : obj.autoHideScrollbar;
  649. obj.scrollbarPosition=$.inArray(obj.theme,scrollbarPositionOutsideThemes) > -1 ? "outside" : obj.scrollbarPosition;
  650. },
  651. /* -------------------- */
  652. /* live option timers removal */
  653. removeLiveTimers=function(selector){
  654. if(liveTimers[selector]){
  655. clearTimeout(liveTimers[selector]);
  656. _delete(liveTimers,selector);
  657. }
  658. },
  659. /* -------------------- */
  660. /* normalizes axis option to valid values: "y", "x", "yx" */
  661. _findAxis=function(val){
  662. return (val==="yx" || val==="xy" || val==="auto") ? "yx" : (val==="x" || val==="horizontal") ? "x" : "y";
  663. },
  664. /* -------------------- */
  665. /* normalizes scrollButtons.scrollType option to valid values: "stepless", "stepped" */
  666. _findScrollButtonsType=function(val){
  667. return (val==="stepped" || val==="pixels" || val==="step" || val==="click") ? "stepped" : "stepless";
  668. },
  669. /* -------------------- */
  670. /* generates plugin markup */
  671. _pluginMarkup=function(){
  672. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  673. expandClass=o.autoExpandScrollbar ? " "+classes[1]+"_expand" : "",
  674. scrollbar=["<div id='mCSB_"+d.idx+"_scrollbar_vertical' class='mCSB_scrollTools mCSB_"+d.idx+"_scrollbar mCS-"+o.theme+" mCSB_scrollTools_vertical"+expandClass+"'><div class='"+classes[12]+"'><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='"+classes[12]+"'><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>"],
  675. wrapperClass=o.axis==="yx" ? "mCSB_vertical_horizontal" : o.axis==="x" ? "mCSB_horizontal" : "mCSB_vertical",
  676. scrollbars=o.axis==="yx" ? scrollbar[0]+scrollbar[1] : o.axis==="x" ? scrollbar[1] : scrollbar[0],
  677. contentWrapper=o.axis==="yx" ? "<div id='mCSB_"+d.idx+"_container_wrapper' class='mCSB_container_wrapper' />" : "",
  678. autoHideClass=o.autoHideScrollbar ? " "+classes[6] : "",
  679. scrollbarDirClass=(o.axis!=="x" && d.langDir==="rtl") ? " "+classes[7] : "";
  680. if(o.setWidth){$this.css("width",o.setWidth);} /* set element width */
  681. if(o.setHeight){$this.css("height",o.setHeight);} /* set element height */
  682. o.setLeft=(o.axis!=="y" && d.langDir==="rtl") ? "989999px" : o.setLeft; /* adjust left position for rtl direction */
  683. $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>");
  684. var mCustomScrollBox=$("#mCSB_"+d.idx),
  685. mCSB_container=$("#mCSB_"+d.idx+"_container");
  686. if(o.axis!=="y" && !o.advanced.autoExpandHorizontalScroll){
  687. mCSB_container.css("width",_contentWidth(mCSB_container.children()));
  688. }
  689. if(o.scrollbarPosition==="outside"){
  690. if($this.css("position")==="static"){ /* requires elements with non-static position */
  691. $this.css("position","relative");
  692. }
  693. $this.css("overflow","visible");
  694. mCustomScrollBox.addClass("mCSB_outside").after(scrollbars);
  695. }else{
  696. mCustomScrollBox.addClass("mCSB_inside").append(scrollbars);
  697. mCSB_container.wrap(contentWrapper);
  698. }
  699. _scrollButtons.call(this); /* add scrollbar buttons */
  700. /* minimum dragger length */
  701. var mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
  702. mCSB_dragger[0].css("min-height",mCSB_dragger[0].height());
  703. mCSB_dragger[1].css("min-width",mCSB_dragger[1].width());
  704. },
  705. /* -------------------- */
  706. /* calculates content width */
  707. _contentWidth=function(el){
  708. return Math.max.apply(Math,el.map(function(){return $(this).outerWidth(true);}).get());
  709. },
  710. /* -------------------- */
  711. /* expands content horizontally */
  712. _expandContentHorizontally=function(){
  713. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  714. mCSB_container=$("#mCSB_"+d.idx+"_container");
  715. if(o.advanced.autoExpandHorizontalScroll && o.axis!=="y"){
  716. /*
  717. wrap content with an infinite width div and set its position to absolute and width to auto.
  718. Setting width to auto before calculating the actual width is important!
  719. We must let the browser set the width as browser zoom values are impossible to calculate.
  720. */
  721. mCSB_container.css({"position":"absolute","width":"auto"})
  722. .wrap("<div class='mCSB_h_wrapper' style='position:relative; left:0; width:999999px;' />")
  723. .css({ /* set actual width, original position and un-wrap */
  724. /*
  725. get the exact width (with decimals) and then round-up.
  726. Using jquery outerWidth() will round the width value which will mess up with inner elements that have non-integer width
  727. */
  728. "width":(Math.ceil(mCSB_container[0].getBoundingClientRect().right+0.4)-Math.floor(mCSB_container[0].getBoundingClientRect().left)),
  729. "position":"relative"
  730. }).unwrap();
  731. }
  732. },
  733. /* -------------------- */
  734. /* adds scrollbar buttons */
  735. _scrollButtons=function(){
  736. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  737. mCSB_scrollTools=$(".mCSB_"+d.idx+"_scrollbar:first"),
  738. tabindex=!_isNumeric(o.scrollButtons.tabindex) ? "" : "tabindex='"+o.scrollButtons.tabindex+"'",
  739. btnHTML=[
  740. "<a href='#' class='"+classes[13]+"' oncontextmenu='return false;' "+tabindex+" />",
  741. "<a href='#' class='"+classes[14]+"' oncontextmenu='return false;' "+tabindex+" />",
  742. "<a href='#' class='"+classes[15]+"' oncontextmenu='return false;' "+tabindex+" />",
  743. "<a href='#' class='"+classes[16]+"' oncontextmenu='return false;' "+tabindex+" />"
  744. ],
  745. btn=[(o.axis==="x" ? btnHTML[2] : btnHTML[0]),(o.axis==="x" ? btnHTML[3] : btnHTML[1]),btnHTML[2],btnHTML[3]];
  746. if(o.scrollButtons.enable){
  747. mCSB_scrollTools.prepend(btn[0]).append(btn[1]).next(".mCSB_scrollTools").prepend(btn[2]).append(btn[3]);
  748. }
  749. },
  750. /* -------------------- */
  751. /* detects/sets css max-height value */
  752. _maxHeight=function(){
  753. var $this=$(this),d=$this.data(pluginPfx),
  754. mCustomScrollBox=$("#mCSB_"+d.idx),
  755. mh=$this.css("max-height") || "none",pct=mh.indexOf("%")!==-1,
  756. bs=$this.css("box-sizing");
  757. if(mh!=="none"){
  758. var val=pct ? $this.parent().height()*parseInt(mh)/100 : parseInt(mh);
  759. /* if element's css box-sizing is "border-box", subtract any paddings and/or borders from max-height value */
  760. if(bs==="border-box"){val-=(($this.innerHeight()-$this.height())+($this.outerHeight()-$this.innerHeight()));}
  761. mCustomScrollBox.css("max-height",Math.round(val));
  762. }
  763. },
  764. /* -------------------- */
  765. /* auto-adjusts scrollbar dragger length */
  766. _setDraggerLength=function(){
  767. var $this=$(this),d=$this.data(pluginPfx),
  768. mCustomScrollBox=$("#mCSB_"+d.idx),
  769. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  770. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  771. ratio=[mCustomScrollBox.height()/mCSB_container.outerHeight(false),mCustomScrollBox.width()/mCSB_container.outerWidth(false)],
  772. l=[
  773. parseInt(mCSB_dragger[0].css("min-height")),Math.round(ratio[0]*mCSB_dragger[0].parent().height()),
  774. parseInt(mCSB_dragger[1].css("min-width")),Math.round(ratio[1]*mCSB_dragger[1].parent().width())
  775. ],
  776. h=oldIE && (l[1]<l[0]) ? l[0] : l[1],w=oldIE && (l[3]<l[2]) ? l[2] : l[3];
  777. mCSB_dragger[0].css({
  778. "height":h,"max-height":(mCSB_dragger[0].parent().height()-10)
  779. }).find(".mCSB_dragger_bar").css({"line-height":l[0]+"px"});
  780. mCSB_dragger[1].css({
  781. "width":w,"max-width":(mCSB_dragger[1].parent().width()-10)
  782. });
  783. },
  784. /* -------------------- */
  785. /* calculates scrollbar to content ratio */
  786. _scrollRatio=function(){
  787. var $this=$(this),d=$this.data(pluginPfx),
  788. mCustomScrollBox=$("#mCSB_"+d.idx),
  789. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  790. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  791. scrollAmount=[mCSB_container.outerHeight(false)-mCustomScrollBox.height(),mCSB_container.outerWidth(false)-mCustomScrollBox.width()],
  792. ratio=[
  793. scrollAmount[0]/(mCSB_dragger[0].parent().height()-mCSB_dragger[0].height()),
  794. scrollAmount[1]/(mCSB_dragger[1].parent().width()-mCSB_dragger[1].width())
  795. ];
  796. d.scrollRatio={y:ratio[0],x:ratio[1]};
  797. },
  798. /* -------------------- */
  799. /* toggles scrolling classes */
  800. _onDragClasses=function(el,action,xpnd){
  801. var expandClass=xpnd ? classes[0]+"_expanded" : "",
  802. scrollbar=el.closest(".mCSB_scrollTools");
  803. if(action==="active"){
  804. el.toggleClass(classes[0]+" "+expandClass); scrollbar.toggleClass(classes[1]);
  805. el[0]._draggable=el[0]._draggable ? 0 : 1;
  806. }else{
  807. if(!el[0]._draggable){
  808. if(action==="hide"){
  809. el.removeClass(classes[0]); scrollbar.removeClass(classes[1]);
  810. }else{
  811. el.addClass(classes[0]); scrollbar.addClass(classes[1]);
  812. }
  813. }
  814. }
  815. },
  816. /* -------------------- */
  817. /* checks if content overflows its container to determine if scrolling is required */
  818. _overflowed=function(){
  819. var $this=$(this),d=$this.data(pluginPfx),
  820. mCustomScrollBox=$("#mCSB_"+d.idx),
  821. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  822. contentHeight=d.overflowed==null ? mCSB_container.height() : mCSB_container.outerHeight(false),
  823. contentWidth=d.overflowed==null ? mCSB_container.width() : mCSB_container.outerWidth(false);
  824. return [contentHeight>mCustomScrollBox.height(),contentWidth>mCustomScrollBox.width()];
  825. },
  826. /* -------------------- */
  827. /* resets content position to 0 */
  828. _resetContentPosition=function(){
  829. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  830. mCustomScrollBox=$("#mCSB_"+d.idx),
  831. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  832. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
  833. _stop($this); /* stop any current scrolling before resetting */
  834. if((o.axis!=="x" && !d.overflowed[0]) || (o.axis==="y" && d.overflowed[0])){ /* reset y */
  835. mCSB_dragger[0].add(mCSB_container).css("top",0);
  836. _scrollTo($this,"_resetY");
  837. }
  838. if((o.axis!=="y" && !d.overflowed[1]) || (o.axis==="x" && d.overflowed[1])){ /* reset x */
  839. var cx=dx=0;
  840. if(d.langDir==="rtl"){ /* adjust left position for rtl direction */
  841. cx=mCustomScrollBox.width()-mCSB_container.outerWidth(false);
  842. dx=Math.abs(cx/d.scrollRatio.x);
  843. }
  844. mCSB_container.css("left",cx);
  845. mCSB_dragger[1].css("left",dx);
  846. _scrollTo($this,"_resetX");
  847. }
  848. },
  849. /* -------------------- */
  850. /* binds scrollbar events */
  851. _bindEvents=function(){
  852. var $this=$(this),d=$this.data(pluginPfx),o=d.opt;
  853. if(!d.bindEvents){ /* check if events are already bound */
  854. _draggable.call(this);
  855. if(o.contentTouchScroll){_contentDraggable.call(this);}
  856. _selectable.call(this);
  857. if(o.mouseWheel.enable){ /* bind mousewheel fn when plugin is available */
  858. function _mwt(){
  859. mousewheelTimeout=setTimeout(function(){
  860. if(!$.event.special.mousewheel){
  861. _mwt();
  862. }else{
  863. clearTimeout(mousewheelTimeout);
  864. _mousewheel.call($this[0]);
  865. }
  866. },100);
  867. }
  868. var mousewheelTimeout;
  869. _mwt();
  870. }
  871. _draggerRail.call(this);
  872. _wrapperScroll.call(this);
  873. if(o.advanced.autoScrollOnFocus){_focus.call(this);}
  874. if(o.scrollButtons.enable){_buttons.call(this);}
  875. if(o.keyboard.enable){_keyboard.call(this);}
  876. d.bindEvents=true;
  877. }
  878. },
  879. /* -------------------- */
  880. /* unbinds scrollbar events */
  881. _unbindEvents=function(){
  882. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  883. namespace=pluginPfx+"_"+d.idx,
  884. sb=".mCSB_"+d.idx+"_scrollbar",
  885. sel=$("#mCSB_"+d.idx+",#mCSB_"+d.idx+"_container,#mCSB_"+d.idx+"_container_wrapper,"+sb+" ."+classes[12]+",#mCSB_"+d.idx+"_dragger_vertical,#mCSB_"+d.idx+"_dragger_horizontal,"+sb+">a"),
  886. mCSB_container=$("#mCSB_"+d.idx+"_container");
  887. if(o.advanced.releaseDraggableSelectors){sel.add($(o.advanced.releaseDraggableSelectors));}
  888. if(d.bindEvents){ /* check if events are bound */
  889. /* unbind namespaced events from document/selectors */
  890. $(document).unbind("."+namespace);
  891. sel.each(function(){
  892. $(this).unbind("."+namespace);
  893. });
  894. /* clear and delete timeouts/objects */
  895. clearTimeout($this[0]._focusTimeout); _delete($this[0],"_focusTimeout");
  896. clearTimeout(d.sequential.step); _delete(d.sequential,"step");
  897. clearTimeout(mCSB_container[0].onCompleteTimeout); _delete(mCSB_container[0],"onCompleteTimeout");
  898. d.bindEvents=false;
  899. }
  900. },
  901. /* -------------------- */
  902. /* toggles scrollbar visibility */
  903. _scrollbarVisibility=function(disabled){
  904. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  905. contentWrapper=$("#mCSB_"+d.idx+"_container_wrapper"),
  906. content=contentWrapper.length ? contentWrapper : $("#mCSB_"+d.idx+"_container"),
  907. scrollbar=[$("#mCSB_"+d.idx+"_scrollbar_vertical"),$("#mCSB_"+d.idx+"_scrollbar_horizontal")],
  908. mCSB_dragger=[scrollbar[0].find(".mCSB_dragger"),scrollbar[1].find(".mCSB_dragger")];
  909. if(o.axis!=="x"){
  910. if(d.overflowed[0] && !disabled){
  911. scrollbar[0].add(mCSB_dragger[0]).add(scrollbar[0].children("a")).css("display","block");
  912. content.removeClass(classes[8]+" "+classes[10]);
  913. }else{
  914. if(o.alwaysShowScrollbar){
  915. if(o.alwaysShowScrollbar!==2){mCSB_dragger[0].css("display","none");}
  916. content.removeClass(classes[10]);
  917. }else{
  918. scrollbar[0].css("display","none");
  919. content.addClass(classes[10]);
  920. }
  921. content.addClass(classes[8]);
  922. }
  923. }
  924. if(o.axis!=="y"){
  925. if(d.overflowed[1] && !disabled){
  926. scrollbar[1].add(mCSB_dragger[1]).add(scrollbar[1].children("a")).css("display","block");
  927. content.removeClass(classes[9]+" "+classes[11]);
  928. }else{
  929. if(o.alwaysShowScrollbar){
  930. if(o.alwaysShowScrollbar!==2){mCSB_dragger[1].css("display","none");}
  931. content.removeClass(classes[11]);
  932. }else{
  933. scrollbar[1].css("display","none");
  934. content.addClass(classes[11]);
  935. }
  936. content.addClass(classes[9]);
  937. }
  938. }
  939. if(!d.overflowed[0] && !d.overflowed[1]){
  940. $this.addClass(classes[5]);
  941. }else{
  942. $this.removeClass(classes[5]);
  943. }
  944. },
  945. /* -------------------- */
  946. /* returns input coordinates of pointer, touch and mouse events (relative to document) */
  947. _coordinates=function(e){
  948. var t=e.type;
  949. switch(t){
  950. case "pointerdown": case "MSPointerDown": case "pointermove": case "MSPointerMove": case "pointerup": case "MSPointerUp":
  951. return [e.originalEvent.pageY,e.originalEvent.pageX,false];
  952. break;
  953. case "touchstart": case "touchmove": case "touchend":
  954. var touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0],
  955. touches=e.originalEvent.touches.length || e.originalEvent.changedTouches.length;
  956. return [touch.pageY,touch.pageX,touches>1];
  957. break;
  958. default:
  959. return [e.pageY,e.pageX,false];
  960. }
  961. },
  962. /* -------------------- */
  963. /*
  964. SCROLLBAR DRAG EVENTS
  965. scrolls content via scrollbar dragging
  966. */
  967. _draggable=function(){
  968. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  969. namespace=pluginPfx+"_"+d.idx,
  970. draggerId=["mCSB_"+d.idx+"_dragger_vertical","mCSB_"+d.idx+"_dragger_horizontal"],
  971. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  972. mCSB_dragger=$("#"+draggerId[0]+",#"+draggerId[1]),
  973. draggable,dragY,dragX,
  974. rds=o.advanced.releaseDraggableSelectors ? mCSB_dragger.add($(o.advanced.releaseDraggableSelectors)) : mCSB_dragger;
  975. mCSB_dragger.bind("mousedown."+namespace+" touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  976. e.stopImmediatePropagation();
  977. e.preventDefault();
  978. if(!_mouseBtnLeft(e)){return;} /* left mouse button only */
  979. touchActive=true;
  980. if(oldIE){document.onselectstart=function(){return false;}} /* disable text selection for IE < 9 */
  981. _iframe(false); /* enable scrollbar dragging over iframes by disabling their events */
  982. _stop($this);
  983. draggable=$(this);
  984. var offset=draggable.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left,
  985. h=draggable.height()+offset.top,w=draggable.width()+offset.left;
  986. if(y<h && y>0 && x<w && x>0){
  987. dragY=y;
  988. dragX=x;
  989. }
  990. _onDragClasses(draggable,"active",o.autoExpandScrollbar);
  991. }).bind("touchmove."+namespace,function(e){
  992. e.stopImmediatePropagation();
  993. e.preventDefault();
  994. var offset=draggable.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left;
  995. _drag(dragY,dragX,y,x);
  996. });
  997. $(document).bind("mousemove."+namespace+" pointermove."+namespace+" MSPointerMove."+namespace,function(e){
  998. if(draggable){
  999. var offset=draggable.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left;
  1000. if(dragY===y){return;} /* has it really moved? */
  1001. _drag(dragY,dragX,y,x);
  1002. }
  1003. }).add(rds).bind("mouseup."+namespace+" touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace,function(e){
  1004. if(draggable){
  1005. _onDragClasses(draggable,"active",o.autoExpandScrollbar);
  1006. draggable=null;
  1007. }
  1008. touchActive=false;
  1009. if(oldIE){document.onselectstart=null;} /* enable text selection for IE < 9 */
  1010. _iframe(true); /* enable iframes events */
  1011. });
  1012. function _iframe(evt){
  1013. var el=mCSB_container.find("iframe");
  1014. if(!el.length){return;} /* check if content contains iframes */
  1015. var val=!evt ? "none" : "auto";
  1016. el.css("pointer-events",val); /* for IE11, iframe's display property should not be "block" */
  1017. }
  1018. function _drag(dragY,dragX,y,x){
  1019. mCSB_container[0].idleTimer=o.scrollInertia<233 ? 250 : 0;
  1020. if(draggable.attr("id")===draggerId[1]){
  1021. var dir="x",to=((draggable[0].offsetLeft-dragX)+x)*d.scrollRatio.x;
  1022. }else{
  1023. var dir="y",to=((draggable[0].offsetTop-dragY)+y)*d.scrollRatio.y;
  1024. }
  1025. _scrollTo($this,to.toString(),{dir:dir,drag:true});
  1026. }
  1027. },
  1028. /* -------------------- */
  1029. /*
  1030. TOUCH SWIPE EVENTS
  1031. scrolls content via touch swipe
  1032. Emulates the native touch-swipe scrolling with momentum found in iOS, Android and WP devices
  1033. */
  1034. _contentDraggable=function(){
  1035. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  1036. namespace=pluginPfx+"_"+d.idx,
  1037. mCustomScrollBox=$("#mCSB_"+d.idx),
  1038. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1039. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  1040. dragY,dragX,touchStartY,touchStartX,touchMoveY=[],touchMoveX=[],startTime,runningTime,endTime,distance,speed,amount,
  1041. durA=0,durB,overwrite=o.axis==="yx" ? "none" : "all",touchIntent=[],touchDrag,docDrag;
  1042. mCSB_container.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  1043. if(!_pointerTouch(e) || touchActive || _coordinates(e)[2]){touchable=0; return;}
  1044. touchable=1; touchDrag=0; docDrag=0;
  1045. var offset=mCSB_container.offset();
  1046. dragY=_coordinates(e)[0]-offset.top;
  1047. dragX=_coordinates(e)[1]-offset.left;
  1048. touchIntent=[_coordinates(e)[0],_coordinates(e)[1]];
  1049. }).bind("touchmove."+namespace+" pointermove."+namespace+" MSPointerMove."+namespace,function(e){
  1050. if(!_pointerTouch(e) || touchActive || _coordinates(e)[2]){return;}
  1051. e.stopImmediatePropagation();
  1052. if(docDrag && !touchDrag){return;}
  1053. runningTime=_getTime();
  1054. var offset=mCustomScrollBox.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left,
  1055. easing="mcsLinearOut";
  1056. touchMoveY.push(y);
  1057. touchMoveX.push(x);
  1058. touchIntent[2]=Math.abs(_coordinates(e)[0]-touchIntent[0]); touchIntent[3]=Math.abs(_coordinates(e)[1]-touchIntent[1]);
  1059. if(d.overflowed[0]){
  1060. var limit=mCSB_dragger[0].parent().height()-mCSB_dragger[0].height(),
  1061. prevent=((dragY-y)>0 && (y-dragY)>-(limit*d.scrollRatio.y) && (touchIntent[3]*2<touchIntent[2] || o.axis==="yx"));
  1062. }
  1063. if(d.overflowed[1]){
  1064. var limitX=mCSB_dragger[1].parent().width()-mCSB_dragger[1].width(),
  1065. preventX=((dragX-x)>0 && (x-dragX)>-(limitX*d.scrollRatio.x) && (touchIntent[2]*2<touchIntent[3] || o.axis==="yx"));
  1066. }
  1067. if(prevent || preventX){e.preventDefault(); touchDrag=1;}else{docDrag=1;} /* prevent native document scrolling */
  1068. amount=o.axis==="yx" ? [(dragY-y),(dragX-x)] : o.axis==="x" ? [null,(dragX-x)] : [(dragY-y),null];
  1069. mCSB_container[0].idleTimer=250;
  1070. if(d.overflowed[0]){_drag(amount[0],durA,easing,"y","all",true);}
  1071. if(d.overflowed[1]){_drag(amount[1],durA,easing,"x",overwrite,true);}
  1072. });
  1073. mCustomScrollBox.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  1074. if(!_pointerTouch(e) || touchActive || _coordinates(e)[2]){touchable=0; return;}
  1075. touchable=1;
  1076. e.stopImmediatePropagation();
  1077. _stop($this);
  1078. startTime=_getTime();
  1079. var offset=mCustomScrollBox.offset();
  1080. touchStartY=_coordinates(e)[0]-offset.top;
  1081. touchStartX=_coordinates(e)[1]-offset.left;
  1082. touchMoveY=[]; touchMoveX=[];
  1083. }).bind("touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace,function(e){
  1084. if(!_pointerTouch(e) || touchActive || _coordinates(e)[2]){return;}
  1085. e.stopImmediatePropagation();
  1086. touchDrag=0; docDrag=0;
  1087. endTime=_getTime();
  1088. var offset=mCustomScrollBox.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left;
  1089. if((endTime-runningTime)>30){return;}
  1090. speed=1000/(endTime-startTime);
  1091. var easing="mcsEaseOut",slow=speed<2.5,
  1092. diff=slow ? [touchMoveY[touchMoveY.length-2],touchMoveX[touchMoveX.length-2]] : [0,0];
  1093. distance=slow ? [(y-diff[0]),(x-diff[1])] : [y-touchStartY,x-touchStartX];
  1094. var absDistance=[Math.abs(distance[0]),Math.abs(distance[1])];
  1095. speed=slow ? [Math.abs(distance[0]/4),Math.abs(distance[1]/4)] : [speed,speed];
  1096. var a=[
  1097. Math.abs(mCSB_container[0].offsetTop)-(distance[0]*_m((absDistance[0]/speed[0]),speed[0])),
  1098. Math.abs(mCSB_container[0].offsetLeft)-(distance[1]*_m((absDistance[1]/speed[1]),speed[1]))
  1099. ];
  1100. amount=o.axis==="yx" ? [a[0],a[1]] : o.axis==="x" ? [null,a[1]] : [a[0],null];
  1101. durB=[(absDistance[0]*4)+o.scrollInertia,(absDistance[1]*4)+o.scrollInertia];
  1102. var md=parseInt(o.contentTouchScroll) || 0; /* absolute minimum distance required */
  1103. amount[0]=absDistance[0]>md ? amount[0] : 0;
  1104. amount[1]=absDistance[1]>md ? amount[1] : 0;
  1105. if(d.overflowed[0]){_drag(amount[0],durB[0],easing,"y",overwrite,false);}
  1106. if(d.overflowed[1]){_drag(amount[1],durB[1],easing,"x",overwrite,false);}
  1107. });
  1108. function _m(ds,s){
  1109. var r=[s*1.5,s*2,s/1.5,s/2];
  1110. if(ds>90){
  1111. return s>4 ? r[0] : r[3];
  1112. }else if(ds>60){
  1113. return s>3 ? r[3] : r[2];
  1114. }else if(ds>30){
  1115. return s>8 ? r[1] : s>6 ? r[0] : s>4 ? s : r[2];
  1116. }else{
  1117. return s>8 ? s : r[3];
  1118. }
  1119. }
  1120. function _drag(amount,dur,easing,dir,overwrite,drag){
  1121. if(!amount){return;}
  1122. _scrollTo($this,amount.toString(),{dur:dur,scrollEasing:easing,dir:dir,overwrite:overwrite,drag:drag});
  1123. }
  1124. },
  1125. /* -------------------- */
  1126. /*
  1127. SELECT TEXT EVENTS
  1128. scrolls content when text is selected
  1129. */
  1130. _selectable=function(){
  1131. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,seq=d.sequential,
  1132. namespace=pluginPfx+"_"+d.idx,
  1133. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1134. wrapper=mCSB_container.parent(),
  1135. action;
  1136. mCSB_container.bind("mousedown."+namespace,function(e){
  1137. if(touchable){return;}
  1138. if(!action){action=1; touchActive=true;}
  1139. }).add(document).bind("mousemove."+namespace,function(e){
  1140. if(!touchable && action && _sel()){
  1141. var offset=mCSB_container.offset(),
  1142. y=_coordinates(e)[0]-offset.top+mCSB_container[0].offsetTop,x=_coordinates(e)[1]-offset.left+mCSB_container[0].offsetLeft;
  1143. if(y>0 && y<wrapper.height() && x>0 && x<wrapper.width()){
  1144. if(seq.step){_seq("off",null,"stepped");}
  1145. }else{
  1146. if(o.axis!=="x" && d.overflowed[0]){
  1147. if(y<0){
  1148. _seq("on",38);
  1149. }else if(y>wrapper.height()){
  1150. _seq("on",40);
  1151. }
  1152. }
  1153. if(o.axis!=="y" && d.overflowed[1]){
  1154. if(x<0){
  1155. _seq("on",37);
  1156. }else if(x>wrapper.width()){
  1157. _seq("on",39);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. }).bind("mouseup."+namespace,function(e){
  1163. if(touchable){return;}
  1164. if(action){action=0; _seq("off",null);}
  1165. touchActive=false;
  1166. });
  1167. function _sel(){
  1168. return window.getSelection ? window.getSelection().toString() :
  1169. document.selection && document.selection.type!="Control" ? document.selection.createRange().text : 0;
  1170. }
  1171. function _seq(a,c,s){
  1172. seq.type=s && action ? "stepped" : "stepless";
  1173. seq.scrollAmount=10;
  1174. _sequentialScroll($this,a,c,"mcsLinearOut",s ? 60 : null);
  1175. }
  1176. },
  1177. /* -------------------- */
  1178. /*
  1179. MOUSE WHEEL EVENT
  1180. scrolls content via mouse-wheel
  1181. via mouse-wheel plugin (https://github.com/brandonaaron/jquery-mousewheel)
  1182. */
  1183. _mousewheel=function(){
  1184. var $this=$(this),d=$this.data(pluginPfx);
  1185. if(d){ /* Check if the scrollbar is ready to use mousewheel events (issue: #185) */
  1186. var o=d.opt,
  1187. namespace=pluginPfx+"_"+d.idx,
  1188. mCustomScrollBox=$("#mCSB_"+d.idx),
  1189. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  1190. iframe=$("#mCSB_"+d.idx+"_container").find("iframe"),
  1191. el=mCustomScrollBox /* mousewheel element selector */;
  1192. /* check for cross domain iframes and bind mousewheel event on them in addition to default mousewheel element selector */
  1193. if(iframe.length){
  1194. iframe.each(function(){
  1195. var iFobj=this;
  1196. if(_canAccessIFrame(iFobj)){ /* check if iframe can be accessed */
  1197. el=el.add($(iFobj).contents().find("body"));
  1198. }
  1199. });
  1200. }
  1201. el.bind("mousewheel."+namespace,function(e,delta){
  1202. _stop($this);
  1203. if(_disableMousewheel($this,e.target)){return;} /* disables mouse-wheel when hovering specific elements */
  1204. var deltaFactor=o.mouseWheel.deltaFactor!=="auto" ? parseInt(o.mouseWheel.deltaFactor) : (oldIE && e.deltaFactor<100) ? 100 : e.deltaFactor || 100;
  1205. if(o.axis==="x" || o.mouseWheel.axis==="x"){
  1206. var dir="x",
  1207. px=[Math.round(deltaFactor*d.scrollRatio.x),parseInt(o.mouseWheel.scrollAmount)],
  1208. amount=o.mouseWheel.scrollAmount!=="auto" ? px[1] : px[0]>=mCustomScrollBox.width() ? mCustomScrollBox.width()*0.9 : px[0],
  1209. contentPos=Math.abs($("#mCSB_"+d.idx+"_container")[0].offsetLeft),
  1210. draggerPos=mCSB_dragger[1][0].offsetLeft,
  1211. limit=mCSB_dragger[1].parent().width()-mCSB_dragger[1].width(),
  1212. dlt=e.deltaX || e.deltaY || delta;
  1213. }else{
  1214. var dir="y",
  1215. px=[Math.round(deltaFactor*d.scrollRatio.y),parseInt(o.mouseWheel.scrollAmount)],
  1216. amount=o.mouseWheel.scrollAmount!=="auto" ? px[1] : px[0]>=mCustomScrollBox.height() ? mCustomScrollBox.height()*0.9 : px[0],
  1217. contentPos=Math.abs($("#mCSB_"+d.idx+"_container")[0].offsetTop),
  1218. draggerPos=mCSB_dragger[0][0].offsetTop,
  1219. limit=mCSB_dragger[0].parent().height()-mCSB_dragger[0].height(),
  1220. dlt=e.deltaY || delta;
  1221. }
  1222. if((dir==="y" && !d.overflowed[0]) || (dir==="x" && !d.overflowed[1])){return;}
  1223. if(o.mouseWheel.invert){dlt=-dlt;}
  1224. if(o.mouseWheel.normalizeDelta){dlt=dlt<0 ? -1 : 1;}
  1225. if((dlt>0 && draggerPos!==0) || (dlt<0 && draggerPos!==limit) || o.mouseWheel.preventDefault){
  1226. e.stopImmediatePropagation();
  1227. e.preventDefault();
  1228. }
  1229. _scrollTo($this,(contentPos-(dlt*amount)).toString(),{dir:dir});
  1230. });
  1231. }
  1232. /* check if iframe can be accessed */
  1233. function _canAccessIFrame(iframe){
  1234. var html=null;
  1235. try{
  1236. var doc=iframe.contentDocument || iframe.contentWindow.document;
  1237. html=doc.body.innerHTML;
  1238. }catch(err){/* do nothing */}
  1239. return(html!==null);
  1240. }
  1241. },
  1242. /* -------------------- */
  1243. /* disables mouse-wheel when hovering specific elements like select, datalist etc. */
  1244. _disableMousewheel=function(el,target){
  1245. var tag=target.nodeName.toLowerCase(),
  1246. tags=el.data(pluginPfx).opt.mouseWheel.disableOver,
  1247. /* elements that require focus */
  1248. focusTags=["select","textarea"];
  1249. return $.inArray(tag,tags) > -1 && !($.inArray(tag,focusTags) > -1 && !$(target).is(":focus"));
  1250. },
  1251. /* -------------------- */
  1252. /*
  1253. DRAGGER RAIL CLICK EVENT
  1254. scrolls content via dragger rail
  1255. */
  1256. _draggerRail=function(){
  1257. var $this=$(this),d=$this.data(pluginPfx),
  1258. namespace=pluginPfx+"_"+d.idx,
  1259. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1260. wrapper=mCSB_container.parent(),
  1261. mCSB_draggerContainer=$(".mCSB_"+d.idx+"_scrollbar ."+classes[12]);
  1262. mCSB_draggerContainer.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  1263. touchActive=true;
  1264. }).bind("touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace,function(e){
  1265. touchActive=false;
  1266. }).bind("click."+namespace,function(e){
  1267. if($(e.target).hasClass(classes[12]) || $(e.target).hasClass("mCSB_draggerRail")){
  1268. _stop($this);
  1269. var el=$(this),mCSB_dragger=el.find(".mCSB_dragger");
  1270. if(el.parent(".mCSB_scrollTools_horizontal").length>0){
  1271. if(!d.overflowed[1]){return;}
  1272. var dir="x",
  1273. clickDir=e.pageX>mCSB_dragger.offset().left ? -1 : 1,
  1274. to=Math.abs(mCSB_container[0].offsetLeft)-(clickDir*(wrapper.width()*0.9));
  1275. }else{
  1276. if(!d.overflowed[0]){return;}
  1277. var dir="y",
  1278. clickDir=e.pageY>mCSB_dragger.offset().top ? -1 : 1,
  1279. to=Math.abs(mCSB_container[0].offsetTop)-(clickDir*(wrapper.height()*0.9));
  1280. }
  1281. _scrollTo($this,to.toString(),{dir:dir,scrollEasing:"mcsEaseInOut"});
  1282. }
  1283. });
  1284. },
  1285. /* -------------------- */
  1286. /*
  1287. FOCUS EVENT
  1288. scrolls content via element focus (e.g. clicking an input, pressing TAB key etc.)
  1289. */
  1290. _focus=function(){
  1291. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  1292. namespace=pluginPfx+"_"+d.idx,
  1293. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1294. wrapper=mCSB_container.parent();
  1295. mCSB_container.bind("focusin."+namespace,function(e){
  1296. var el=$(document.activeElement),
  1297. nested=mCSB_container.find(".mCustomScrollBox").length,
  1298. dur=0;
  1299. if(!el.is(o.advanced.autoScrollOnFocus)){return;}
  1300. _stop($this);
  1301. clearTimeout($this[0]._focusTimeout);
  1302. $this[0]._focusTimer=nested ? (dur+17)*nested : 0;
  1303. $this[0]._focusTimeout=setTimeout(function(){
  1304. var to=[_childPos(el)[0],_childPos(el)[1]],
  1305. contentPos=[mCSB_container[0].offsetTop,mCSB_container[0].offsetLeft],
  1306. isVisible=[
  1307. (contentPos[0]+to[0]>=0 && contentPos[0]+to[0]<wrapper.height()-el.outerHeight(false)),
  1308. (contentPos[1]+to[1]>=0 && contentPos[0]+to[1]<wrapper.width()-el.outerWidth(false))
  1309. ],
  1310. overwrite=(o.axis==="yx" && !isVisible[0] && !isVisible[1]) ? "none" : "all";
  1311. if(o.axis!=="x" && !isVisible[0]){
  1312. _scrollTo($this,to[0].toString(),{dir:"y",scrollEasing:"mcsEaseInOut",overwrite:overwrite,dur:dur});
  1313. }
  1314. if(o.axis!=="y" && !isVisible[1]){
  1315. _scrollTo($this,to[1].toString(),{dir:"x",scrollEasing:"mcsEaseInOut",overwrite:overwrite,dur:dur});
  1316. }
  1317. },$this[0]._focusTimer);
  1318. });
  1319. },
  1320. /* -------------------- */
  1321. /* sets content wrapper scrollTop/scrollLeft always to 0 */
  1322. _wrapperScroll=function(){
  1323. var $this=$(this),d=$this.data(pluginPfx),
  1324. namespace=pluginPfx+"_"+d.idx,
  1325. wrapper=$("#mCSB_"+d.idx+"_container").parent();
  1326. wrapper.bind("scroll."+namespace,function(e){
  1327. if(wrapper.scrollTop()!==0 || wrapper.scrollLeft()!==0){
  1328. $(".mCSB_"+d.idx+"_scrollbar").css("visibility","hidden"); /* hide scrollbar(s) */
  1329. }
  1330. });
  1331. },
  1332. /* -------------------- */
  1333. /*
  1334. BUTTONS EVENTS
  1335. scrolls content via up, down, left and right buttons
  1336. */
  1337. _buttons=function(){
  1338. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,seq=d.sequential,
  1339. namespace=pluginPfx+"_"+d.idx,
  1340. sel=".mCSB_"+d.idx+"_scrollbar",
  1341. btn=$(sel+">a");
  1342. btn.bind("mousedown."+namespace+" touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace+" mouseup."+namespace+" touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace+" mouseout."+namespace+" pointerout."+namespace+" MSPointerOut."+namespace+" click."+namespace,function(e){
  1343. e.preventDefault();
  1344. if(!_mouseBtnLeft(e)){return;} /* left mouse button only */
  1345. var btnClass=$(this).attr("class");
  1346. seq.type=o.scrollButtons.scrollType;
  1347. switch(e.type){
  1348. case "mousedown": case "touchstart": case "pointerdown": case "MSPointerDown":
  1349. if(seq.type==="stepped"){return;}
  1350. touchActive=true;
  1351. d.tweenRunning=false;
  1352. _seq("on",btnClass);
  1353. break;
  1354. case "mouseup": case "touchend": case "pointerup": case "MSPointerUp":
  1355. case "mouseout": case "pointerout": case "MSPointerOut":
  1356. if(seq.type==="stepped"){return;}
  1357. touchActive=false;
  1358. if(seq.dir){_seq("off",btnClass);}
  1359. break;
  1360. case "click":
  1361. if(seq.type!=="stepped" || d.tweenRunning){return;}
  1362. _seq("on",btnClass);
  1363. break;
  1364. }
  1365. function _seq(a,c){
  1366. seq.scrollAmount=o.snapAmount || o.scrollButtons.scrollAmount;
  1367. _sequentialScroll($this,a,c);
  1368. }
  1369. });
  1370. },
  1371. /* -------------------- */
  1372. /*
  1373. KEYBOARD EVENTS
  1374. scrolls content via keyboard
  1375. Keys: up arrow, down arrow, left arrow, right arrow, PgUp, PgDn, Home, End
  1376. */
  1377. _keyboard=function(){
  1378. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,seq=d.sequential,
  1379. namespace=pluginPfx+"_"+d.idx,
  1380. mCustomScrollBox=$("#mCSB_"+d.idx),
  1381. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1382. wrapper=mCSB_container.parent(),
  1383. editables="input,textarea,select,datalist,keygen,[contenteditable='true']";
  1384. mCustomScrollBox.attr("tabindex","0").bind("blur."+namespace+" keydown."+namespace+" keyup."+namespace,function(e){
  1385. switch(e.type){
  1386. case "blur":
  1387. if(d.tweenRunning && seq.dir){_seq("off",null);}
  1388. break;
  1389. case "keydown": case "keyup":
  1390. var code=e.keyCode ? e.keyCode : e.which,action="on";
  1391. if((o.axis!=="x" && (code===38 || code===40)) || (o.axis!=="y" && (code===37 || code===39))){
  1392. /* up (38), down (40), left (37), right (39) arrows */
  1393. if(((code===38 || code===40) && !d.overflowed[0]) || ((code===37 || code===39) && !d.overflowed[1])){return;}
  1394. if(e.type==="keyup"){action="off";}
  1395. if(!$(document.activeElement).is(editables)){
  1396. e.preventDefault();
  1397. e.stopImmediatePropagation();
  1398. _seq(action,code);
  1399. }
  1400. }else if(code===33 || code===34){
  1401. /* PgUp (33), PgDn (34) */
  1402. if(d.overflowed[0] || d.overflowed[1]){
  1403. e.preventDefault();
  1404. e.stopImmediatePropagation();
  1405. }
  1406. if(e.type==="keyup"){
  1407. _stop($this);
  1408. var keyboardDir=code===34 ? -1 : 1;
  1409. if(o.axis==="x" || (o.axis==="yx" && d.overflowed[1] && !d.overflowed[0])){
  1410. var dir="x",to=Math.abs(mCSB_container[0].offsetLeft)-(keyboardDir*(wrapper.width()*0.9));
  1411. }else{
  1412. var dir="y",to=Math.abs(mCSB_container[0].offsetTop)-(keyboardDir*(wrapper.height()*0.9));
  1413. }
  1414. _scrollTo($this,to.toString(),{dir:dir,scrollEasing:"mcsEaseInOut"});
  1415. }
  1416. }else if(code===35 || code===36){
  1417. /* End (35), Home (36) */
  1418. if(!$(document.activeElement).is(editables)){
  1419. if(d.overflowed[0] || d.overflowed[1]){
  1420. e.preventDefault();
  1421. e.stopImmediatePropagation();
  1422. }
  1423. if(e.type==="keyup"){
  1424. if(o.axis==="x" || (o.axis==="yx" && d.overflowed[1] && !d.overflowed[0])){
  1425. var dir="x",to=code===35 ? Math.abs(wrapper.width()-mCSB_container.outerWidth(false)) : 0;
  1426. }else{
  1427. var dir="y",to=code===35 ? Math.abs(wrapper.height()-mCSB_container.outerHeight(false)) : 0;
  1428. }
  1429. _scrollTo($this,to.toString(),{dir:dir,scrollEasing:"mcsEaseInOut"});
  1430. }
  1431. }
  1432. }
  1433. break;
  1434. }
  1435. function _seq(a,c){
  1436. seq.type=o.keyboard.scrollType;
  1437. seq.scrollAmount=o.snapAmount || o.keyboard.scrollAmount;
  1438. if(seq.type==="stepped" && d.tweenRunning){return;}
  1439. _sequentialScroll($this,a,c);
  1440. }
  1441. });
  1442. },
  1443. /* -------------------- */
  1444. /* scrolls content sequentially (used when scrolling via buttons, keyboard arrows etc.) */
  1445. _sequentialScroll=function(el,action,trigger,e,s){
  1446. var d=el.data(pluginPfx),o=d.opt,seq=d.sequential,
  1447. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1448. once=seq.type==="stepped" ? true : false,
  1449. steplessSpeed=o.scrollInertia < 26 ? 26 : o.scrollInertia, /* 26/1.5=17 */
  1450. steppedSpeed=o.scrollInertia < 1 ? 17 : o.scrollInertia;
  1451. switch(action){
  1452. case "on":
  1453. seq.dir=[
  1454. (trigger===classes[16] || trigger===classes[15] || trigger===39 || trigger===37 ? "x" : "y"),
  1455. (trigger===classes[13] || trigger===classes[15] || trigger===38 || trigger===37 ? -1 : 1)
  1456. ];
  1457. _stop(el);
  1458. if(_isNumeric(trigger) && seq.type==="stepped"){return;}
  1459. _on(once);
  1460. break;
  1461. case "off":
  1462. _off();
  1463. if(once || (d.tweenRunning && seq.dir)){
  1464. _on(true);
  1465. }
  1466. break;
  1467. }
  1468. /* starts sequence */
  1469. function _on(once){
  1470. var c=seq.type!=="stepped", /* continuous scrolling */
  1471. t=s ? s : !once ? 1000/60 : c ? steplessSpeed/1.5 : steppedSpeed, /* timer */
  1472. m=!once ? 2.5 : c ? 7.5 : 40, /* multiplier */
  1473. contentPos=[Math.abs(mCSB_container[0].offsetTop),Math.abs(mCSB_container[0].offsetLeft)],
  1474. ratio=[d.scrollRatio.y>10 ? 10 : d.scrollRatio.y,d.scrollRatio.x>10 ? 10 : d.scrollRatio.x],
  1475. amount=seq.dir[0]==="x" ? contentPos[1]+(seq.dir[1]*(ratio[1]*m)) : contentPos[0]+(seq.dir[1]*(ratio[0]*m)),
  1476. px=seq.dir[0]==="x" ? contentPos[1]+(seq.dir[1]*parseInt(seq.scrollAmount)) : contentPos[0]+(seq.dir[1]*parseInt(seq.scrollAmount)),
  1477. to=seq.scrollAmount!=="auto" ? px : amount,
  1478. easing=e ? e : !once ? "mcsLinear" : c ? "mcsLinearOut" : "mcsEaseInOut",
  1479. onComplete=!once ? false : true;
  1480. if(once && t<17){
  1481. to=seq.dir[0]==="x" ? contentPos[1] : contentPos[0];
  1482. }
  1483. _scrollTo(el,to.toString(),{dir:seq.dir[0],scrollEasing:easing,dur:t,onComplete:onComplete});
  1484. if(once){
  1485. seq.dir=false;
  1486. return;
  1487. }
  1488. clearTimeout(seq.step);
  1489. seq.step=setTimeout(function(){
  1490. _on();
  1491. },t);
  1492. }
  1493. /* stops sequence */
  1494. function _off(){
  1495. clearTimeout(seq.step);
  1496. _delete(seq,"step");
  1497. _stop(el);
  1498. }
  1499. },
  1500. /* -------------------- */
  1501. /* returns a yx array from value */
  1502. _arr=function(val){
  1503. var o=$(this).data(pluginPfx).opt,vals=[];
  1504. if(typeof val==="function"){val=val();} /* check if the value is a single anonymous function */
  1505. /* check if value is object or array, its length and create an array with yx values */
  1506. if(!(val instanceof Array)){ /* object value (e.g. {y:"100",x:"100"}, 100 etc.) */
  1507. vals[0]=val.y ? val.y : val.x || o.axis==="x" ? null : val;
  1508. vals[1]=val.x ? val.x : val.y || o.axis==="y" ? null : val;
  1509. }else{ /* array value (e.g. [100,100]) */
  1510. vals=val.length>1 ? [val[0],val[1]] : o.axis==="x" ? [null,val[0]] : [val[0],null];
  1511. }
  1512. /* check if array values are anonymous functions */
  1513. if(typeof vals[0]==="function"){vals[0]=vals[0]();}
  1514. if(typeof vals[1]==="function"){vals[1]=vals[1]();}
  1515. return vals;
  1516. },
  1517. /* -------------------- */
  1518. /* translates values (e.g. "top", 100, "100px", "#id") to actual scroll-to positions */
  1519. _to=function(val,dir){
  1520. if(val==null || typeof val=="undefined"){return;}
  1521. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  1522. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1523. wrapper=mCSB_container.parent(),
  1524. t=typeof val;
  1525. if(!dir){dir=o.axis==="x" ? "x" : "y";}
  1526. var contentLength=dir==="x" ? mCSB_container.outerWidth(false) : mCSB_container.outerHeight(false),
  1527. contentPos=dir==="x" ? mCSB_container[0].offsetLeft : mCSB_container[0].offsetTop,
  1528. cssProp=dir==="x" ? "left" : "top";
  1529. switch(t){
  1530. case "function": /* this currently is not used. Consider removing it */
  1531. return val();
  1532. break;
  1533. case "object": /* js/jquery object */
  1534. var obj=val.jquery ? val : $(val);
  1535. if(!obj.length){return;}
  1536. return dir==="x" ? _childPos(obj)[1] : _childPos(obj)[0];
  1537. break;
  1538. case "string": case "number":
  1539. if(_isNumeric(val)){ /* numeric value */
  1540. return Math.abs(val);
  1541. }else if(val.indexOf("%")!==-1){ /* percentage value */
  1542. return Math.abs(contentLength*parseInt(val)/100);
  1543. }else if(val.indexOf("-=")!==-1){ /* decrease value */
  1544. return Math.abs(contentPos-parseInt(val.split("-=")[1]));
  1545. }else if(val.indexOf("+=")!==-1){ /* inrease value */
  1546. var p=(contentPos+parseInt(val.split("+=")[1]));
  1547. return p>=0 ? 0 : Math.abs(p);
  1548. }else if(val.indexOf("px")!==-1 && _isNumeric(val.split("px")[0])){ /* pixels string value (e.g. "100px") */
  1549. return Math.abs(val.split("px")[0]);
  1550. }else{
  1551. if(val==="top" || val==="left"){ /* special strings */
  1552. return 0;
  1553. }else if(val==="bottom"){
  1554. return Math.abs(wrapper.height()-mCSB_container.outerHeight(false));
  1555. }else if(val==="right"){
  1556. return Math.abs(wrapper.width()-mCSB_container.outerWidth(false));
  1557. }else if(val==="first" || val==="last"){
  1558. var obj=mCSB_container.find(":"+val);
  1559. return dir==="x" ? _childPos(obj)[1] : _childPos(obj)[0];
  1560. }else{
  1561. if($(val).length){ /* jquery selector */
  1562. return dir==="x" ? _childPos($(val))[1] : _childPos($(val))[0];
  1563. }else{ /* other values (e.g. "100em") */
  1564. mCSB_container.css(cssProp,val);
  1565. methods.update.call(null,$this[0]);
  1566. return;
  1567. }
  1568. }
  1569. }
  1570. break;
  1571. }
  1572. },
  1573. /* -------------------- */
  1574. /* calls the update method automatically */
  1575. _autoUpdate=function(rem){
  1576. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  1577. mCSB_container=$("#mCSB_"+d.idx+"_container");
  1578. if(rem){
  1579. /*
  1580. removes autoUpdate timer
  1581. usage: _autoUpdate.call(this,"remove");
  1582. */
  1583. clearTimeout(mCSB_container[0].autoUpdate);
  1584. _delete(mCSB_container[0],"autoUpdate");
  1585. return;
  1586. }
  1587. var wrapper=mCSB_container.parent(),
  1588. scrollbar=[$("#mCSB_"+d.idx+"_scrollbar_vertical"),$("#mCSB_"+d.idx+"_scrollbar_horizontal")],
  1589. scrollbarSize=function(){return [
  1590. scrollbar[0].is(":visible") ? scrollbar[0].outerHeight(true) : 0, /* returns y-scrollbar height */
  1591. scrollbar[1].is(":visible") ? scrollbar[1].outerWidth(true) : 0 /* returns x-scrollbar width */
  1592. ]},
  1593. oldSelSize=sizesSum(),newSelSize,
  1594. os=[mCSB_container.outerHeight(false),mCSB_container.outerWidth(false),wrapper.height(),wrapper.width(),scrollbarSize()[0],scrollbarSize()[1]],ns,
  1595. oldImgsLen=imgSum(),newImgsLen;
  1596. upd();
  1597. function upd(){
  1598. clearTimeout(mCSB_container[0].autoUpdate);
  1599. mCSB_container[0].autoUpdate=setTimeout(function(){
  1600. /* update on specific selector(s) length and size change */
  1601. if(o.advanced.updateOnSelectorChange){
  1602. newSelSize=sizesSum();
  1603. if(newSelSize!==oldSelSize){
  1604. doUpd(3);
  1605. oldSelSize=newSelSize;
  1606. return;
  1607. }
  1608. }
  1609. /* update on main element and scrollbar size changes */
  1610. if(o.advanced.updateOnContentResize){
  1611. ns=[mCSB_container.outerHeight(false),mCSB_container.outerWidth(false),wrapper.height(),wrapper.width(),scrollbarSize()[0],scrollbarSize()[1]];
  1612. if(ns[0]!==os[0] || ns[1]!==os[1] || ns[2]!==os[2] || ns[3]!==os[3] || ns[4]!==os[4] || ns[5]!==os[5]){
  1613. doUpd(ns[0]!==os[0] || ns[1]!==os[1]);
  1614. os=ns;
  1615. }
  1616. }
  1617. /* update on image load */
  1618. if(o.advanced.updateOnImageLoad){
  1619. newImgsLen=imgSum();
  1620. if(newImgsLen!==oldImgsLen){
  1621. mCSB_container.find("img").each(function(){
  1622. imgLoader(this);
  1623. });
  1624. oldImgsLen=newImgsLen;
  1625. }
  1626. }
  1627. if(o.advanced.updateOnSelectorChange || o.advanced.updateOnContentResize || o.advanced.updateOnImageLoad){upd();}
  1628. },60);
  1629. }
  1630. /* returns images amount */
  1631. function imgSum(){
  1632. var total=0
  1633. if(o.advanced.updateOnImageLoad){total=mCSB_container.find("img").length;}
  1634. return total;
  1635. }
  1636. /* a tiny image loader */
  1637. function imgLoader(el){
  1638. if($(el).hasClass(classes[2])){doUpd(); return;}
  1639. var img=new Image();
  1640. function createDelegate(contextObject,delegateMethod){
  1641. return function(){return delegateMethod.apply(contextObject,arguments);}
  1642. }
  1643. function imgOnLoad(){
  1644. this.onload=null;
  1645. $(el).addClass(classes[2]);
  1646. doUpd(2);
  1647. }
  1648. img.onload=createDelegate(img,imgOnLoad);
  1649. img.src=el.src;
  1650. }
  1651. /* returns the total height and width sum of all elements matching the selector */
  1652. function sizesSum(){
  1653. if(o.advanced.updateOnSelectorChange===true){o.advanced.updateOnSelectorChange="*";}
  1654. var total=0,sel=mCSB_container.find(o.advanced.updateOnSelectorChange);
  1655. if(o.advanced.updateOnSelectorChange && sel.length>0){sel.each(function(){total+=$(this).height()+$(this).width();});}
  1656. return total;
  1657. }
  1658. /* calls the update method */
  1659. function doUpd(cb){
  1660. clearTimeout(mCSB_container[0].autoUpdate);
  1661. methods.update.call(null,$this[0],cb);
  1662. }
  1663. },
  1664. /* -------------------- */
  1665. /* snaps scrolling to a multiple of a pixels number */
  1666. _snapAmount=function(to,amount,offset){
  1667. return (Math.round(to/amount)*amount-offset);
  1668. },
  1669. /* -------------------- */
  1670. /* stops content and scrollbar animations */
  1671. _stop=function(el){
  1672. var d=el.data(pluginPfx),
  1673. sel=$("#mCSB_"+d.idx+"_container,#mCSB_"+d.idx+"_container_wrapper,#mCSB_"+d.idx+"_dragger_vertical,#mCSB_"+d.idx+"_dragger_horizontal");
  1674. sel.each(function(){
  1675. _stopTween.call(this);
  1676. });
  1677. },
  1678. /* -------------------- */
  1679. /*
  1680. ANIMATES CONTENT
  1681. This is where the actual scrolling happens
  1682. */
  1683. _scrollTo=function(el,to,options){
  1684. var d=el.data(pluginPfx),o=d.opt,
  1685. defaults={
  1686. trigger:"internal",
  1687. dir:"y",
  1688. scrollEasing:"mcsEaseOut",
  1689. drag:false,
  1690. dur:o.scrollInertia,
  1691. overwrite:"all",
  1692. callbacks:true,
  1693. onStart:true,
  1694. onUpdate:true,
  1695. onComplete:true
  1696. },
  1697. options=$.extend(defaults,options),
  1698. dur=[options.dur,(options.drag ? 0 : options.dur)],
  1699. mCustomScrollBox=$("#mCSB_"+d.idx),
  1700. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1701. wrapper=mCSB_container.parent(),
  1702. totalScrollOffsets=o.callbacks.onTotalScrollOffset ? _arr.call(el,o.callbacks.onTotalScrollOffset) : [0,0],
  1703. totalScrollBackOffsets=o.callbacks.onTotalScrollBackOffset ? _arr.call(el,o.callbacks.onTotalScrollBackOffset) : [0,0];
  1704. d.trigger=options.trigger;
  1705. if(wrapper.scrollTop()!==0 || wrapper.scrollLeft()!==0){ /* always reset scrollTop/Left */
  1706. $(".mCSB_"+d.idx+"_scrollbar").css("visibility","visible");
  1707. wrapper.scrollTop(0).scrollLeft(0);
  1708. }
  1709. if(to==="_resetY" && !d.contentReset.y){
  1710. /* callbacks: onOverflowYNone */
  1711. if(_cb("onOverflowYNone")){o.callbacks.onOverflowYNone.call(el[0]);}
  1712. d.contentReset.y=1;
  1713. }
  1714. if(to==="_resetX" && !d.contentReset.x){
  1715. /* callbacks: onOverflowXNone */
  1716. if(_cb("onOverflowXNone")){o.callbacks.onOverflowXNone.call(el[0]);}
  1717. d.contentReset.x=1;
  1718. }
  1719. if(to==="_resetY" || to==="_resetX"){return;}
  1720. if((d.contentReset.y || !el[0].mcs) && d.overflowed[0]){
  1721. /* callbacks: onOverflowY */
  1722. if(_cb("onOverflowY")){o.callbacks.onOverflowY.call(el[0]);}
  1723. d.contentReset.x=null;
  1724. }
  1725. if((d.contentReset.x || !el[0].mcs) && d.overflowed[1]){
  1726. /* callbacks: onOverflowX */
  1727. if(_cb("onOverflowX")){o.callbacks.onOverflowX.call(el[0]);}
  1728. d.contentReset.x=null;
  1729. }
  1730. if(o.snapAmount){to=_snapAmount(to,o.snapAmount,o.snapOffset);} /* scrolling snapping */
  1731. switch(options.dir){
  1732. case "x":
  1733. var mCSB_dragger=$("#mCSB_"+d.idx+"_dragger_horizontal"),
  1734. property="left",
  1735. contentPos=mCSB_container[0].offsetLeft,
  1736. limit=[
  1737. mCustomScrollBox.width()-mCSB_container.outerWidth(false),
  1738. mCSB_dragger.parent().width()-mCSB_dragger.width()
  1739. ],
  1740. scrollTo=[to,to===0 ? 0 : (to/d.scrollRatio.x)],
  1741. tso=totalScrollOffsets[1],
  1742. tsbo=totalScrollBackOffsets[1],
  1743. totalScrollOffset=tso>0 ? tso/d.scrollRatio.x : 0,
  1744. totalScrollBackOffset=tsbo>0 ? tsbo/d.scrollRatio.x : 0;
  1745. break;
  1746. case "y":
  1747. var mCSB_dragger=$("#mCSB_"+d.idx+"_dragger_vertical"),
  1748. property="top",
  1749. contentPos=mCSB_container[0].offsetTop,
  1750. limit=[
  1751. mCustomScrollBox.height()-mCSB_container.outerHeight(false),
  1752. mCSB_dragger.parent().height()-mCSB_dragger.height()
  1753. ],
  1754. scrollTo=[to,to===0 ? 0 : (to/d.scrollRatio.y)],
  1755. tso=totalScrollOffsets[0],
  1756. tsbo=totalScrollBackOffsets[0],
  1757. totalScrollOffset=tso>0 ? tso/d.scrollRatio.y : 0,
  1758. totalScrollBackOffset=tsbo>0 ? tsbo/d.scrollRatio.y : 0;
  1759. break;
  1760. }
  1761. if(scrollTo[1]<0 || (scrollTo[0]===0 && scrollTo[1]===0)){
  1762. scrollTo=[0,0];
  1763. }else if(scrollTo[1]>=limit[1]){
  1764. scrollTo=[limit[0],limit[1]];
  1765. }else{
  1766. scrollTo[0]=-scrollTo[0];
  1767. }
  1768. if(!el[0].mcs){
  1769. _mcs(); /* init mcs object (once) to make it available before callbacks */
  1770. if(_cb("onInit")){o.callbacks.onInit.call(el[0]);} /* callbacks: onInit */
  1771. }
  1772. clearTimeout(mCSB_container[0].onCompleteTimeout);
  1773. if(!d.tweenRunning && ((contentPos===0 && scrollTo[0]>=0) || (contentPos===limit[0] && scrollTo[0]<=limit[0]))){return;}
  1774. _tweenTo(mCSB_dragger[0],property,Math.round(scrollTo[1]),dur[1],options.scrollEasing);
  1775. _tweenTo(mCSB_container[0],property,Math.round(scrollTo[0]),dur[0],options.scrollEasing,options.overwrite,{
  1776. onStart:function(){
  1777. if(options.callbacks && options.onStart && !d.tweenRunning){
  1778. /* callbacks: onScrollStart */
  1779. if(_cb("onScrollStart")){_mcs(); o.callbacks.onScrollStart.call(el[0]);}
  1780. d.tweenRunning=true;
  1781. _onDragClasses(mCSB_dragger);
  1782. d.cbOffsets=_cbOffsets();
  1783. }
  1784. },onUpdate:function(){
  1785. if(options.callbacks && options.onUpdate){
  1786. /* callbacks: whileScrolling */
  1787. if(_cb("whileScrolling")){_mcs(); o.callbacks.whileScrolling.call(el[0]);}
  1788. }
  1789. },onComplete:function(){
  1790. if(options.callbacks && options.onComplete){
  1791. if(o.axis==="yx"){clearTimeout(mCSB_container[0].onCompleteTimeout);}
  1792. var t=mCSB_container[0].idleTimer || 0;
  1793. mCSB_container[0].onCompleteTimeout=setTimeout(function(){
  1794. /* callbacks: onScroll, onTotalScroll, onTotalScrollBack */
  1795. if(_cb("onScroll")){_mcs(); o.callbacks.onScroll.call(el[0]);}
  1796. if(_cb("onTotalScroll") && scrollTo[1]>=limit[1]-totalScrollOffset && d.cbOffsets[0]){_mcs(); o.callbacks.onTotalScroll.call(el[0]);}
  1797. if(_cb("onTotalScrollBack") && scrollTo[1]<=totalScrollBackOffset && d.cbOffsets[1]){_mcs(); o.callbacks.onTotalScrollBack.call(el[0]);}
  1798. d.tweenRunning=false;
  1799. mCSB_container[0].idleTimer=0;
  1800. _onDragClasses(mCSB_dragger,"hide");
  1801. },t);
  1802. }
  1803. }
  1804. });
  1805. /* checks if callback function exists */
  1806. function _cb(cb){
  1807. return d && o.callbacks[cb] && typeof o.callbacks[cb]==="function";
  1808. }
  1809. /* checks whether callback offsets always trigger */
  1810. function _cbOffsets(){
  1811. return [o.callbacks.alwaysTriggerOffsets || contentPos>=limit[0]+tso,o.callbacks.alwaysTriggerOffsets || contentPos<=-tsbo];
  1812. }
  1813. /*
  1814. populates object with useful values for the user
  1815. values:
  1816. content: this.mcs.content
  1817. content top position: this.mcs.top
  1818. content left position: this.mcs.left
  1819. dragger top position: this.mcs.draggerTop
  1820. dragger left position: this.mcs.draggerLeft
  1821. scrolling y percentage: this.mcs.topPct
  1822. scrolling x percentage: this.mcs.leftPct
  1823. scrolling direction: this.mcs.direction
  1824. */
  1825. function _mcs(){
  1826. var cp=[mCSB_container[0].offsetTop,mCSB_container[0].offsetLeft], /* content position */
  1827. dp=[mCSB_dragger[0].offsetTop,mCSB_dragger[0].offsetLeft], /* dragger position */
  1828. cl=[mCSB_container.outerHeight(false),mCSB_container.outerWidth(false)], /* content length */
  1829. pl=[mCustomScrollBox.height(),mCustomScrollBox.width()]; /* content parent length */
  1830. el[0].mcs={
  1831. content:mCSB_container, /* original content wrapper as jquery object */
  1832. top:cp[0],left:cp[1],draggerTop:dp[0],draggerLeft:dp[1],
  1833. topPct:Math.round((100*Math.abs(cp[0]))/(Math.abs(cl[0])-pl[0])),leftPct:Math.round((100*Math.abs(cp[1]))/(Math.abs(cl[1])-pl[1])),
  1834. direction:options.dir
  1835. };
  1836. /*
  1837. this refers to the original element containing the scrollbar(s)
  1838. usage: this.mcs.top, this.mcs.leftPct etc.
  1839. */
  1840. }
  1841. },
  1842. /* -------------------- */
  1843. /*
  1844. CUSTOM JAVASCRIPT ANIMATION TWEEN
  1845. Lighter and faster than jquery animate() and css transitions
  1846. Animates top/left properties and includes easings
  1847. */
  1848. _tweenTo=function(el,prop,to,duration,easing,overwrite,callbacks){
  1849. if(!el._mTween){el._mTween={top:{},left:{}};}
  1850. var callbacks=callbacks || {},
  1851. onStart=callbacks.onStart || function(){},onUpdate=callbacks.onUpdate || function(){},onComplete=callbacks.onComplete || function(){},
  1852. startTime=_getTime(),_delay,progress=0,from=el.offsetTop,elStyle=el.style,_request,tobj=el._mTween[prop];
  1853. if(prop==="left"){from=el.offsetLeft;}
  1854. var diff=to-from;
  1855. tobj.stop=0;
  1856. if(overwrite!=="none"){_cancelTween();}
  1857. _startTween();
  1858. function _step(){
  1859. if(tobj.stop){return;}
  1860. if(!progress){onStart.call();}
  1861. progress=_getTime()-startTime;
  1862. _tween();
  1863. if(progress>=tobj.time){
  1864. tobj.time=(progress>tobj.time) ? progress+_delay-(progress-tobj.time) : progress+_delay-1;
  1865. if(tobj.time<progress+1){tobj.time=progress+1;}
  1866. }
  1867. if(tobj.time<duration){tobj.id=_request(_step);}else{onComplete.call();}
  1868. }
  1869. function _tween(){
  1870. if(duration>0){
  1871. tobj.currVal=_ease(tobj.time,from,diff,duration,easing);
  1872. elStyle[prop]=Math.round(tobj.currVal)+"px";
  1873. }else{
  1874. elStyle[prop]=to+"px";
  1875. }
  1876. onUpdate.call();
  1877. }
  1878. function _startTween(){
  1879. _delay=1000/60;
  1880. tobj.time=progress+_delay;
  1881. _request=(!window.requestAnimationFrame) ? function(f){_tween(); return setTimeout(f,0.01);} : window.requestAnimationFrame;
  1882. tobj.id=_request(_step);
  1883. }
  1884. function _cancelTween(){
  1885. if(tobj.id==null){return;}
  1886. if(!window.requestAnimationFrame){clearTimeout(tobj.id);
  1887. }else{window.cancelAnimationFrame(tobj.id);}
  1888. tobj.id=null;
  1889. }
  1890. function _ease(t,b,c,d,type){
  1891. switch(type){
  1892. case "linear": case "mcsLinear":
  1893. return c*t/d + b;
  1894. break;
  1895. case "mcsLinearOut":
  1896. t/=d; t--; return c * Math.sqrt(1 - t*t) + b;
  1897. break;
  1898. case "easeInOutSmooth":
  1899. t/=d/2;
  1900. if(t<1) return c/2*t*t + b;
  1901. t--;
  1902. return -c/2 * (t*(t-2) - 1) + b;
  1903. break;
  1904. case "easeInOutStrong":
  1905. t/=d/2;
  1906. if(t<1) return c/2 * Math.pow( 2, 10 * (t - 1) ) + b;
  1907. t--;
  1908. return c/2 * ( -Math.pow( 2, -10 * t) + 2 ) + b;
  1909. break;
  1910. case "easeInOut": case "mcsEaseInOut":
  1911. t/=d/2;
  1912. if(t<1) return c/2*t*t*t + b;
  1913. t-=2;
  1914. return c/2*(t*t*t + 2) + b;
  1915. break;
  1916. case "easeOutSmooth":
  1917. t/=d; t--;
  1918. return -c * (t*t*t*t - 1) + b;
  1919. break;
  1920. case "easeOutStrong":
  1921. return c * ( -Math.pow( 2, -10 * t/d ) + 1 ) + b;
  1922. break;
  1923. case "easeOut": case "mcsEaseOut": default:
  1924. var ts=(t/=d)*t,tc=ts*t;
  1925. return b+c*(0.499999999999997*tc*ts + -2.5*ts*ts + 5.5*tc + -6.5*ts + 4*t);
  1926. }
  1927. }
  1928. },
  1929. /* -------------------- */
  1930. /* returns current time */
  1931. _getTime=function(){
  1932. if(window.performance && window.performance.now){
  1933. return window.performance.now();
  1934. }else{
  1935. if(window.performance && window.performance.webkitNow){
  1936. return window.performance.webkitNow();
  1937. }else{
  1938. if(Date.now){return Date.now();}else{return allGetServerTime().getTime();}
  1939. }
  1940. }
  1941. },
  1942. /* -------------------- */
  1943. /* stops a tween */
  1944. _stopTween=function(){
  1945. var el=this;
  1946. if(!el._mTween){el._mTween={top:{},left:{}};}
  1947. var props=["top","left"];
  1948. for(var i=0; i<props.length; i++){
  1949. var prop=props[i];
  1950. if(el._mTween[prop].id){
  1951. if(!window.requestAnimationFrame){clearTimeout(el._mTween[prop].id);
  1952. }else{window.cancelAnimationFrame(el._mTween[prop].id);}
  1953. el._mTween[prop].id=null;
  1954. el._mTween[prop].stop=1;
  1955. }
  1956. }
  1957. },
  1958. /* -------------------- */
  1959. /* deletes a property (avoiding the exception thrown by IE) */
  1960. _delete=function(c,m){
  1961. try{delete c[m];}catch(e){c[m]=null;}
  1962. },
  1963. /* -------------------- */
  1964. /* detects left mouse button */
  1965. _mouseBtnLeft=function(e){
  1966. return !(e.which && e.which!==1);
  1967. },
  1968. /* -------------------- */
  1969. /* detects if pointer type event is touch */
  1970. _pointerTouch=function(e){
  1971. var t=e.originalEvent.pointerType;
  1972. return !(t && t!=="touch" && t!==2);
  1973. },
  1974. /* -------------------- */
  1975. /* checks if value is numeric */
  1976. _isNumeric=function(val){
  1977. return !isNaN(parseFloat(val)) && isFinite(val);
  1978. },
  1979. /* -------------------- */
  1980. /* returns element position according to content */
  1981. _childPos=function(el){
  1982. var p=el.parents(".mCSB_container");
  1983. return [el.offset().top-p.offset().top,el.offset().left-p.offset().left];
  1984. };
  1985. /* -------------------- */
  1986. /*
  1987. ----------------------------------------
  1988. PLUGIN SETUP
  1989. ----------------------------------------
  1990. */
  1991. /* plugin constructor functions */
  1992. $.fn[pluginNS]=function(method){ /* usage: $(selector).mCustomScrollbar(); */
  1993. if(methods[method]){
  1994. return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
  1995. }else if(typeof method==="object" || !method){
  1996. return methods.init.apply(this,arguments);
  1997. }else{
  1998. $.error("Method "+method+" does not exist");
  1999. }
  2000. };
  2001. $[pluginNS]=function(method){ /* usage: $.mCustomScrollbar(); */
  2002. if(methods[method]){
  2003. return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
  2004. }else if(typeof method==="object" || !method){
  2005. return methods.init.apply(this,arguments);
  2006. }else{
  2007. $.error("Method "+method+" does not exist");
  2008. }
  2009. };
  2010. /*
  2011. allow setting plugin default options.
  2012. usage: $.mCustomScrollbar.defaults.scrollInertia=500;
  2013. to apply any changed default options on default selectors (below), use inside document ready fn
  2014. e.g.: $(document).ready(function(){ $.mCustomScrollbar.defaults.scrollInertia=500; });
  2015. */
  2016. $[pluginNS].defaults=defaults;
  2017. /*
  2018. add window object (window.mCustomScrollbar)
  2019. usage: if(window.mCustomScrollbar){console.log("custom scrollbar plugin loaded");}
  2020. */
  2021. window[pluginNS]=true;
  2022. $(window).load(function(){
  2023. $(defaultSelector)[pluginNS](); /* add scrollbars automatically on default selector */
  2024. /* extend jQuery expressions */
  2025. $.extend($.expr[":"],{
  2026. /* checks if element is within scrollable viewport */
  2027. mcsInView:$.expr[":"].mcsInView || function(el){
  2028. var $el=$(el),content=$el.parents(".mCSB_container"),wrapper,cPos;
  2029. if(!content.length){return;}
  2030. wrapper=content.parent();
  2031. cPos=[content[0].offsetTop,content[0].offsetLeft];
  2032. return cPos[0]+_childPos($el)[0]>=0 && cPos[0]+_childPos($el)[0]<wrapper.height()-$el.outerHeight(false) &&
  2033. cPos[1]+_childPos($el)[1]>=0 && cPos[1]+_childPos($el)[1]<wrapper.width()-$el.outerWidth(false);
  2034. },
  2035. /* checks if element is overflowed having visible scrollbar(s) */
  2036. mcsOverflow:$.expr[":"].mcsOverflow || function(el){
  2037. var d=$(el).data(pluginPfx);
  2038. if(!d){return;}
  2039. return d.overflowed[0] || d.overflowed[1];
  2040. }
  2041. });
  2042. });
  2043. }))}(jQuery,window,document));