jquery.mCustomScrollbar.js 89 KB

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