jquery.mCustomScrollbar.js 88 KB

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