jquery.nicescroll.js 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745
  1. /* jquery.nicescroll
  2. -- version 3.7.6
  3. -- copyright 2017-07-19 InuYaksa*2017
  4. -- licensed under the MIT
  5. --
  6. -- https://nicescroll.areaaperta.com/
  7. -- https://github.com/inuyaksa/jquery.nicescroll
  8. --
  9. */
  10. /* jshint expr: true */
  11. (function (factory) {
  12. if (typeof define === 'function' && define.amd) {
  13. // AMD. Register as anonymous module.
  14. define(['jquery'], factory);
  15. } else if (typeof exports === 'object') {
  16. // Node/CommonJS.
  17. module.exports = factory(require('jquery'));
  18. } else {
  19. // Browser globals.
  20. factory(jQuery);
  21. }
  22. }(function (jQuery) {
  23. "use strict";
  24. // globals
  25. var domfocus = false,
  26. mousefocus = false,
  27. tabindexcounter = 0,
  28. ascrailcounter = 2000,
  29. globalmaxzindex = 0;
  30. var $ = jQuery, // sandbox
  31. _doc = document,
  32. _win = window,
  33. $window = $(_win);
  34. var delegatevents = [];
  35. // http://stackoverflow.com/questions/2161159/get-script-path
  36. function getScriptPath() {
  37. var scripts = _doc.currentScript || (function () { var s = _doc.getElementsByTagName('script'); return (s.length) ? s[s.length - 1] : false; })();
  38. var path = scripts ? scripts.src.split('?')[0] : '';
  39. return (path.split('/').length > 0) ? path.split('/').slice(0, -1).join('/') + '/' : '';
  40. }
  41. // based on code by Paul Irish https://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
  42. var setAnimationFrame = _win.requestAnimationFrame || _win.webkitRequestAnimationFrame || _win.mozRequestAnimationFrame || false;
  43. var clearAnimationFrame = _win.cancelAnimationFrame || _win.webkitCancelAnimationFrame || _win.mozCancelAnimationFrame || false;
  44. if (!setAnimationFrame) {
  45. var anilasttime = 0;
  46. setAnimationFrame = function (callback, element) {
  47. var currTime = new Date().getTime();
  48. var timeToCall = Math.max(0, 16 - (currTime - anilasttime));
  49. var id = _win.setTimeout(function () { callback(currTime + timeToCall); },
  50. timeToCall);
  51. anilasttime = currTime + timeToCall;
  52. return id;
  53. };
  54. clearAnimationFrame = function (id) {
  55. _win.clearTimeout(id);
  56. };
  57. } else {
  58. if (!_win.cancelAnimationFrame) clearAnimationFrame = function (id) { };
  59. }
  60. var ClsMutationObserver = _win.MutationObserver || _win.WebKitMutationObserver || false;
  61. var now = Date.now || function () { return new Date().getTime(); };
  62. var _globaloptions = {
  63. zindex: "auto",
  64. cursoropacitymin: 0,
  65. cursoropacitymax: 1,
  66. cursorcolor: "#424242",
  67. cursorwidth: "6px",
  68. cursorborder: "1px solid #fff",
  69. cursorborderradius: "5px",
  70. scrollspeed: 40,
  71. mousescrollstep: 9 * 3,
  72. touchbehavior: false, // deprecated
  73. emulatetouch: false, // replacing touchbehavior
  74. hwacceleration: true,
  75. usetransition: true,
  76. boxzoom: false,
  77. dblclickzoom: true,
  78. gesturezoom: true,
  79. grabcursorenabled: true,
  80. autohidemode: true,
  81. background: "",
  82. iframeautoresize: true,
  83. cursorminheight: 32,
  84. preservenativescrolling: true,
  85. railoffset: false,
  86. railhoffset: false,
  87. bouncescroll: true,
  88. spacebarenabled: true,
  89. railpadding: {
  90. top: 0,
  91. right: 0,
  92. left: 0,
  93. bottom: 0
  94. },
  95. disableoutline: true,
  96. horizrailenabled: true,
  97. railalign: "right",
  98. railvalign: "bottom",
  99. enabletranslate3d: true,
  100. enablemousewheel: true,
  101. enablekeyboard: true,
  102. smoothscroll: true,
  103. sensitiverail: true,
  104. enablemouselockapi: true,
  105. // cursormaxheight:false,
  106. cursorfixedheight: false,
  107. directionlockdeadzone: 6,
  108. hidecursordelay: 400,
  109. nativeparentscrolling: true,
  110. enablescrollonselection: true,
  111. overflowx: true,
  112. overflowy: true,
  113. cursordragspeed: 0.3,
  114. rtlmode: "auto",
  115. cursordragontouch: false,
  116. oneaxismousemode: "auto",
  117. scriptpath: getScriptPath(),
  118. preventmultitouchscrolling: true,
  119. disablemutationobserver: false,
  120. enableobserver: true,
  121. scrollbarid: false,
  122. scrollCLass: false
  123. };
  124. var browserdetected = false;
  125. var getBrowserDetection = function () {
  126. if (browserdetected) return browserdetected;
  127. var _el = _doc.createElement('DIV'),
  128. _style = _el.style,
  129. _agent = navigator.userAgent,
  130. _platform = navigator.platform,
  131. d = {};
  132. d.haspointerlock = "pointerLockElement" in _doc || "webkitPointerLockElement" in _doc || "mozPointerLockElement" in _doc;
  133. d.isopera = ("opera" in _win); // 12-
  134. d.isopera12 = (d.isopera && ("getUserMedia" in navigator));
  135. d.isoperamini = (Object.prototype.toString.call(_win.operamini) === "[object OperaMini]");
  136. d.isie = (("all" in _doc) && ("attachEvent" in _el) && !d.isopera); //IE10-
  137. d.isieold = (d.isie && !("msInterpolationMode" in _style)); // IE6 and older
  138. d.isie7 = d.isie && !d.isieold && (!("documentMode" in _doc) || (_doc.documentMode === 7));
  139. d.isie8 = d.isie && ("documentMode" in _doc) && (_doc.documentMode === 8);
  140. d.isie9 = d.isie && ("performance" in _win) && (_doc.documentMode === 9);
  141. d.isie10 = d.isie && ("performance" in _win) && (_doc.documentMode === 10);
  142. d.isie11 = ("msRequestFullscreen" in _el) && (_doc.documentMode >= 11); // IE11+
  143. d.ismsedge = ("msCredentials" in _win); // MS Edge 14+
  144. d.ismozilla = ("MozAppearance" in _style);
  145. d.iswebkit = !d.ismsedge && ("WebkitAppearance" in _style);
  146. d.ischrome = d.iswebkit && ("chrome" in _win);
  147. d.ischrome38 = (d.ischrome && ("touchAction" in _style)); // behavior changed in touch emulation
  148. d.ischrome22 = (!d.ischrome38) && (d.ischrome && d.haspointerlock);
  149. d.ischrome26 = (!d.ischrome38) && (d.ischrome && ("transition" in _style)); // issue with transform detection (maintain prefix)
  150. d.cantouch = ("ontouchstart" in _doc.documentElement) || ("ontouchstart" in _win); // with detection for Chrome Touch Emulation
  151. d.hasw3ctouch = (_win.PointerEvent || false) && ((navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)); //IE11 pointer events, following W3C Pointer Events spec
  152. d.hasmstouch = (!d.hasw3ctouch) && (_win.MSPointerEvent || false); // IE10 pointer events
  153. d.ismac = /^mac$/i.test(_platform);
  154. d.isios = d.cantouch && /iphone|ipad|ipod/i.test(_platform);
  155. d.isios4 = d.isios && !("seal" in Object);
  156. d.isios7 = d.isios && ("webkitHidden" in _doc); //iOS 7+
  157. d.isios8 = d.isios && ("hidden" in _doc); //iOS 8+
  158. d.isios10 = d.isios && _win.Proxy; //iOS 10+
  159. d.isandroid = (/android/i.test(_agent));
  160. d.haseventlistener = ("addEventListener" in _el);
  161. d.trstyle = false;
  162. d.hastransform = false;
  163. d.hastranslate3d = false;
  164. d.transitionstyle = false;
  165. d.hastransition = false;
  166. d.transitionend = false;
  167. d.trstyle = "transform";
  168. d.hastransform = ("transform" in _style) || (function () {
  169. var check = ['msTransform', 'webkitTransform', 'MozTransform', 'OTransform'];
  170. for (var a = 0, c = check.length; a < c; a++) {
  171. if (_style[check[a]] !== undefined) {
  172. d.trstyle = check[a];
  173. break;
  174. }
  175. }
  176. d.hastransform = (!!d.trstyle);
  177. })();
  178. if (d.hastransform) {
  179. _style[d.trstyle] = "translate3d(1px,2px,3px)";
  180. d.hastranslate3d = /translate3d/.test(_style[d.trstyle]);
  181. }
  182. d.transitionstyle = "transition";
  183. d.prefixstyle = '';
  184. d.transitionend = "transitionend";
  185. d.hastransition = ("transition" in _style) || (function () {
  186. d.transitionend = false;
  187. var check = ['webkitTransition', 'msTransition', 'MozTransition', 'OTransition', 'OTransition', 'KhtmlTransition'];
  188. var prefix = ['-webkit-', '-ms-', '-moz-', '-o-', '-o', '-khtml-'];
  189. var evs = ['webkitTransitionEnd', 'msTransitionEnd', 'transitionend', 'otransitionend', 'oTransitionEnd', 'KhtmlTransitionEnd'];
  190. for (var a = 0, c = check.length; a < c; a++) {
  191. if (check[a] in _style) {
  192. d.transitionstyle = check[a];
  193. d.prefixstyle = prefix[a];
  194. d.transitionend = evs[a];
  195. break;
  196. }
  197. }
  198. if (d.ischrome26) d.prefixstyle = prefix[1]; // always use prefix
  199. d.hastransition = (d.transitionstyle);
  200. })();
  201. function detectCursorGrab() {
  202. var lst = ['grab', '-webkit-grab', '-moz-grab'];
  203. if ((d.ischrome && !d.ischrome38) || d.isie) lst = []; // force setting for IE returns false positive and chrome cursor bug
  204. for (var a = 0, l = lst.length; a < l; a++) {
  205. var p = lst[a];
  206. _style.cursor = p;
  207. if (_style.cursor == p) return p;
  208. }
  209. return 'url(https://cdnjs.cloudflare.com/ajax/libs/slider-pro/1.3.0/css/images/openhand.cur),n-resize'; // thanks to https://cdnjs.com/ for the openhand cursor!
  210. }
  211. d.cursorgrabvalue = detectCursorGrab();
  212. d.hasmousecapture = ("setCapture" in _el);
  213. d.hasMutationObserver = (ClsMutationObserver !== false);
  214. _el = null; //memory released
  215. browserdetected = d;
  216. return d;
  217. };
  218. var NiceScrollClass = function (myopt, me) {
  219. var self = this;
  220. this.version = '3.7.6';
  221. this.name = 'nicescroll';
  222. this.me = me;
  223. var $body = $("body");
  224. var opt = this.opt = {
  225. doc: $body,
  226. win: false
  227. };
  228. $.extend(opt, _globaloptions); // clone opts
  229. // Options for internal use
  230. opt.snapbackspeed = 80;
  231. if (myopt || false) {
  232. for (var a in opt) {
  233. if (myopt[a] !== undefined) opt[a] = myopt[a];
  234. }
  235. }
  236. if (opt.disablemutationobserver) ClsMutationObserver = false;
  237. this.doc = opt.doc;
  238. this.iddoc = (this.doc && this.doc[0]) ? this.doc[0].id || '' : '';
  239. this.ispage = /^BODY|HTML/.test((opt.win) ? opt.win[0].nodeName : this.doc[0].nodeName);
  240. this.haswrapper = (opt.win !== false);
  241. this.win = opt.win || (this.ispage ? $window : this.doc);
  242. this.docscroll = (this.ispage && !this.haswrapper) ? $window : this.win;
  243. this.body = $body;
  244. this.viewport = false;
  245. this.isfixed = false;
  246. this.iframe = false;
  247. this.isiframe = ((this.doc[0].nodeName == 'IFRAME') && (this.win[0].nodeName == 'IFRAME'));
  248. this.istextarea = (this.win[0].nodeName == 'TEXTAREA');
  249. this.forcescreen = false; //force to use screen position on events
  250. this.canshowonmouseevent = (opt.autohidemode != "scroll");
  251. // Events jump table
  252. this.onmousedown = false;
  253. this.onmouseup = false;
  254. this.onmousemove = false;
  255. this.onmousewheel = false;
  256. this.onkeypress = false;
  257. this.ongesturezoom = false;
  258. this.onclick = false;
  259. // Nicescroll custom events
  260. this.onscrollstart = false;
  261. this.onscrollend = false;
  262. this.onscrollcancel = false;
  263. this.onzoomin = false;
  264. this.onzoomout = false;
  265. // Let's start!
  266. this.view = false;
  267. this.page = false;
  268. this.scroll = {
  269. x: 0,
  270. y: 0
  271. };
  272. this.scrollratio = {
  273. x: 0,
  274. y: 0
  275. };
  276. this.cursorheight = 20;
  277. this.scrollvaluemax = 0;
  278. // http://dev.w3.org/csswg/css-writing-modes-3/#logical-to-physical
  279. // http://dev.w3.org/csswg/css-writing-modes-3/#svg-writing-mode
  280. if (opt.rtlmode == "auto") {
  281. var target = this.win[0] == _win ? this.body : this.win;
  282. var writingMode = target.css("writing-mode") || target.css("-webkit-writing-mode") || target.css("-ms-writing-mode") || target.css("-moz-writing-mode");
  283. if (writingMode == "horizontal-tb" || writingMode == "lr-tb" || writingMode === "") {
  284. this.isrtlmode = (target.css("direction") == "rtl");
  285. this.isvertical = false;
  286. } else {
  287. this.isrtlmode = (writingMode == "vertical-rl" || writingMode == "tb" || writingMode == "tb-rl" || writingMode == "rl-tb");
  288. this.isvertical = (writingMode == "vertical-rl" || writingMode == "tb" || writingMode == "tb-rl");
  289. }
  290. } else {
  291. this.isrtlmode = (opt.rtlmode === true);
  292. this.isvertical = false;
  293. }
  294. // this.checkrtlmode = false;
  295. this.scrollrunning = false;
  296. this.scrollmom = false;
  297. this.observer = false; // observer div changes
  298. this.observerremover = false; // observer on parent for remove detection
  299. this.observerbody = false; // observer on body for position change
  300. if (opt.scrollbarid !== false) {
  301. this.id = opt.scrollbarid;
  302. } else {
  303. do {
  304. this.id = "ascrail" + (ascrailcounter++);
  305. } while (_doc.getElementById(this.id));
  306. }
  307. this.rail = false;
  308. this.cursor = false;
  309. this.cursorfreezed = false;
  310. this.selectiondrag = false;
  311. this.zoom = false;
  312. this.zoomactive = false;
  313. this.hasfocus = false;
  314. this.hasmousefocus = false;
  315. //this.visibility = true;
  316. this.railslocked = false; // locked by resize
  317. this.locked = false; // prevent lost of locked status sets by user
  318. this.hidden = false; // rails always hidden
  319. this.cursoractive = true; // user can interact with cursors
  320. this.wheelprevented = false; //prevent mousewheel event
  321. this.overflowx = opt.overflowx;
  322. this.overflowy = opt.overflowy;
  323. this.nativescrollingarea = false;
  324. this.checkarea = 0;
  325. this.events = []; // event list for unbind
  326. this.saved = {}; // style saved
  327. this.delaylist = {};
  328. this.synclist = {};
  329. this.lastdeltax = 0;
  330. this.lastdeltay = 0;
  331. this.detected = getBrowserDetection();
  332. var cap = $.extend({}, this.detected);
  333. this.canhwscroll = (cap.hastransform && opt.hwacceleration);
  334. this.ishwscroll = (this.canhwscroll && self.haswrapper);
  335. if (!this.isrtlmode) {
  336. this.hasreversehr = false;
  337. } else if (this.isvertical) { // RTL mode with reverse horizontal axis
  338. this.hasreversehr = !(cap.iswebkit || cap.isie || cap.isie11);
  339. } else {
  340. this.hasreversehr = !(cap.iswebkit || (cap.isie && !cap.isie10 && !cap.isie11));
  341. }
  342. this.istouchcapable = false; // desktop devices with touch screen support
  343. //## Check WebKit-based desktop with touch support
  344. //## + Firefox 18 nightly build (desktop) false positive (or desktop with touch support)
  345. if (!cap.cantouch && (cap.hasw3ctouch || cap.hasmstouch)) { // desktop device with multiple input
  346. this.istouchcapable = true;
  347. } else if (cap.cantouch && !cap.isios && !cap.isandroid && (cap.iswebkit || cap.ismozilla)) {
  348. this.istouchcapable = true;
  349. }
  350. //## disable MouseLock API on user request
  351. if (!opt.enablemouselockapi) {
  352. cap.hasmousecapture = false;
  353. cap.haspointerlock = false;
  354. }
  355. this.debounced = function (name, fn, tm) {
  356. if (!self) return;
  357. var dd = self.delaylist[name] || false;
  358. if (!dd) {
  359. self.delaylist[name] = {
  360. h: setAnimationFrame(function () {
  361. self.delaylist[name].fn.call(self);
  362. self.delaylist[name] = false;
  363. }, tm)
  364. };
  365. fn.call(self);
  366. }
  367. self.delaylist[name].fn = fn;
  368. };
  369. this.synched = function (name, fn) {
  370. if (self.synclist[name]) self.synclist[name] = fn;
  371. else {
  372. self.synclist[name] = fn;
  373. setAnimationFrame(function () {
  374. if (!self) return;
  375. self.synclist[name] && self.synclist[name].call(self);
  376. self.synclist[name] = null;
  377. });
  378. }
  379. };
  380. this.unsynched = function (name) {
  381. if (self.synclist[name]) self.synclist[name] = false;
  382. };
  383. this.css = function (el, pars) { // save & set
  384. for (var n in pars) {
  385. self.saved.css.push([el, n, el.css(n)]);
  386. el.css(n, pars[n]);
  387. }
  388. };
  389. this.scrollTop = function (val) {
  390. return (val === undefined) ? self.getScrollTop() : self.setScrollTop(val);
  391. };
  392. this.scrollLeft = function (val) {
  393. return (val === undefined) ? self.getScrollLeft() : self.setScrollLeft(val);
  394. };
  395. // derived by by Dan Pupius www.pupius.net
  396. var BezierClass = function (st, ed, spd, p1, p2, p3, p4) {
  397. this.st = st;
  398. this.ed = ed;
  399. this.spd = spd;
  400. this.p1 = p1 || 0;
  401. this.p2 = p2 || 1;
  402. this.p3 = p3 || 0;
  403. this.p4 = p4 || 1;
  404. this.ts = now();
  405. this.df = ed - st;
  406. };
  407. BezierClass.prototype = {
  408. B2: function (t) {
  409. return 3 * (1 - t) * (1 - t) * t;
  410. },
  411. B3: function (t) {
  412. return 3 * (1 - t) * t * t;
  413. },
  414. B4: function (t) {
  415. return t * t * t;
  416. },
  417. getPos: function () {
  418. return (now() - this.ts) / this.spd;
  419. },
  420. getNow: function () {
  421. var pc = (now() - this.ts) / this.spd;
  422. var bz = this.B2(pc) + this.B3(pc) + this.B4(pc);
  423. return (pc >= 1) ? this.ed : this.st + (this.df * bz) | 0;
  424. },
  425. update: function (ed, spd) {
  426. this.st = this.getNow();
  427. this.ed = ed;
  428. this.spd = spd;
  429. this.ts = now();
  430. this.df = this.ed - this.st;
  431. return this;
  432. }
  433. };
  434. //derived from http://stackoverflow.com/questions/11236090/
  435. function getMatrixValues() {
  436. var tr = self.doc.css(cap.trstyle);
  437. if (tr && (tr.substr(0, 6) == "matrix")) {
  438. return tr.replace(/^.*\((.*)\)$/g, "$1").replace(/px/g, '').split(/, +/);
  439. }
  440. return false;
  441. }
  442. if (this.ishwscroll) { // hw accelerated scroll
  443. this.doc.translate = {
  444. x: 0,
  445. y: 0,
  446. tx: "0px",
  447. ty: "0px"
  448. };
  449. //this one can help to enable hw accel on ios6 http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/
  450. if (cap.hastranslate3d && cap.isios) this.doc.css("-webkit-backface-visibility", "hidden"); // prevent flickering http://stackoverflow.com/questions/3461441/
  451. this.getScrollTop = function (last) {
  452. if (!last) {
  453. var mtx = getMatrixValues();
  454. if (mtx) return (mtx.length == 16) ? -mtx[13] : -mtx[5]; //matrix3d 16 on IE10
  455. if (self.timerscroll && self.timerscroll.bz) return self.timerscroll.bz.getNow();
  456. }
  457. return self.doc.translate.y;
  458. };
  459. this.getScrollLeft = function (last) {
  460. if (!last) {
  461. var mtx = getMatrixValues();
  462. if (mtx) return (mtx.length == 16) ? -mtx[12] : -mtx[4]; //matrix3d 16 on IE10
  463. if (self.timerscroll && self.timerscroll.bh) return self.timerscroll.bh.getNow();
  464. }
  465. return self.doc.translate.x;
  466. };
  467. this.notifyScrollEvent = function (el) {
  468. var e = _doc.createEvent("UIEvents");
  469. e.initUIEvent("scroll", false, false, _win, 1);
  470. e.niceevent = true;
  471. el.dispatchEvent(e);
  472. };
  473. var cxscrollleft = (this.isrtlmode) ? 1 : -1;
  474. if (cap.hastranslate3d && opt.enabletranslate3d) {
  475. this.setScrollTop = function (val, silent) {
  476. self.doc.translate.y = val;
  477. self.doc.translate.ty = (val * -1) + "px";
  478. self.doc.css(cap.trstyle, "translate3d(" + self.doc.translate.tx + "," + self.doc.translate.ty + ",0)");
  479. if (!silent) self.notifyScrollEvent(self.win[0]);
  480. };
  481. this.setScrollLeft = function (val, silent) {
  482. self.doc.translate.x = val;
  483. self.doc.translate.tx = (val * cxscrollleft) + "px";
  484. self.doc.css(cap.trstyle, "translate3d(" + self.doc.translate.tx + "," + self.doc.translate.ty + ",0)");
  485. if (!silent) self.notifyScrollEvent(self.win[0]);
  486. };
  487. } else {
  488. this.setScrollTop = function (val, silent) {
  489. self.doc.translate.y = val;
  490. self.doc.translate.ty = (val * -1) + "px";
  491. self.doc.css(cap.trstyle, "translate(" + self.doc.translate.tx + "," + self.doc.translate.ty + ")");
  492. if (!silent) self.notifyScrollEvent(self.win[0]);
  493. };
  494. this.setScrollLeft = function (val, silent) {
  495. self.doc.translate.x = val;
  496. self.doc.translate.tx = (val * cxscrollleft) + "px";
  497. self.doc.css(cap.trstyle, "translate(" + self.doc.translate.tx + "," + self.doc.translate.ty + ")");
  498. if (!silent) self.notifyScrollEvent(self.win[0]);
  499. };
  500. }
  501. } else { // native scroll
  502. this.getScrollTop = function () {
  503. return self.docscroll.scrollTop();
  504. };
  505. this.setScrollTop = function (val) {
  506. self.docscroll.scrollTop(val);
  507. };
  508. this.getScrollLeft = function () {
  509. var val;
  510. if (!self.hasreversehr) {
  511. val = self.docscroll.scrollLeft();
  512. } else if (self.detected.ismozilla) {
  513. val = self.page.maxw - Math.abs(self.docscroll.scrollLeft());
  514. } else {
  515. val = self.page.maxw - self.docscroll.scrollLeft();
  516. }
  517. return val;
  518. };
  519. this.setScrollLeft = function (val) {
  520. return setTimeout(function () {
  521. if (!self) return;
  522. if (self.hasreversehr) {
  523. if (self.detected.ismozilla) {
  524. val = -(self.page.maxw - val);
  525. } else {
  526. val = self.page.maxw - val;
  527. }
  528. }
  529. return self.docscroll.scrollLeft(val);
  530. }, 1);
  531. };
  532. }
  533. this.getTarget = function (e) {
  534. if (!e) return false;
  535. if (e.target) return e.target;
  536. if (e.srcElement) return e.srcElement;
  537. return false;
  538. };
  539. this.hasParent = function (e, id) {
  540. if (!e) return false;
  541. var el = e.target || e.srcElement || e || false;
  542. while (el && el.id != id) {
  543. el = el.parentNode || false;
  544. }
  545. return (el !== false);
  546. };
  547. function getZIndex() {
  548. var dom = self.win;
  549. if ("zIndex" in dom) return dom.zIndex(); // use jQuery UI method when available
  550. while (dom.length > 0) {
  551. if (dom[0].nodeType == 9) return false;
  552. var zi = dom.css('zIndex');
  553. if (!isNaN(zi) && zi !== 0) return parseInt(zi);
  554. dom = dom.parent();
  555. }
  556. return false;
  557. }
  558. //inspired by http://forum.jquery.com/topic/width-includes-border-width-when-set-to-thin-medium-thick-in-ie
  559. var _convertBorderWidth = {
  560. "thin": 1,
  561. "medium": 3,
  562. "thick": 5
  563. };
  564. function getWidthToPixel(dom, prop, chkheight) {
  565. var wd = dom.css(prop);
  566. var px = parseFloat(wd);
  567. if (isNaN(px)) {
  568. px = _convertBorderWidth[wd] || 0;
  569. var brd = (px == 3) ? ((chkheight) ? (self.win.outerHeight() - self.win.innerHeight()) : (self.win.outerWidth() - self.win.innerWidth())) : 1; //DON'T TRUST CSS
  570. if (self.isie8 && px) px += 1;
  571. return (brd) ? px : 0;
  572. }
  573. return px;
  574. }
  575. this.getDocumentScrollOffset = function () {
  576. return {
  577. top: _win.pageYOffset || _doc.documentElement.scrollTop,
  578. left: _win.pageXOffset || _doc.documentElement.scrollLeft
  579. };
  580. };
  581. this.getOffset = function () {
  582. if (self.isfixed) {
  583. var ofs = self.win.offset(); // fix Chrome auto issue (when right/bottom props only)
  584. var scrl = self.getDocumentScrollOffset();
  585. ofs.top -= scrl.top;
  586. ofs.left -= scrl.left;
  587. return ofs;
  588. }
  589. var ww = self.win.offset();
  590. if (!self.viewport) return ww;
  591. var vp = self.viewport.offset();
  592. return {
  593. top: ww.top - vp.top,
  594. left: ww.left - vp.left
  595. };
  596. };
  597. this.updateScrollBar = function (len) {
  598. var pos, off;
  599. if (self.ishwscroll) {
  600. self.rail.css({
  601. height: self.win.innerHeight() - (opt.railpadding.top + opt.railpadding.bottom)
  602. });
  603. if (self.railh) self.railh.css({
  604. width: self.win.innerWidth() - (opt.railpadding.left + opt.railpadding.right)
  605. });
  606. } else {
  607. var wpos = self.getOffset();
  608. pos = {
  609. top: wpos.top,
  610. left: wpos.left - (opt.railpadding.left + opt.railpadding.right)
  611. };
  612. pos.top += getWidthToPixel(self.win, 'border-top-width', true);
  613. pos.left += (self.rail.align) ? self.win.outerWidth() - getWidthToPixel(self.win, 'border-right-width') - self.rail.width : getWidthToPixel(self.win, 'border-left-width');
  614. off = opt.railoffset;
  615. if (off) {
  616. if (off.top) pos.top += off.top;
  617. if (off.left) pos.left += off.left;
  618. }
  619. if (!self.railslocked) self.rail.css({
  620. top: pos.top,
  621. left: pos.left,
  622. height: ((len) ? len.h : self.win.innerHeight()) - (opt.railpadding.top + opt.railpadding.bottom)
  623. });
  624. if (self.zoom) {
  625. self.zoom.css({
  626. top: pos.top + 1,
  627. left: (self.rail.align == 1) ? pos.left - 20 : pos.left + self.rail.width + 4
  628. });
  629. }
  630. if (self.railh && !self.railslocked) {
  631. pos = {
  632. top: wpos.top,
  633. left: wpos.left
  634. };
  635. off = opt.railhoffset;
  636. if (off) {
  637. if (off.top) pos.top += off.top;
  638. if (off.left) pos.left += off.left;
  639. }
  640. var y = (self.railh.align) ? pos.top + getWidthToPixel(self.win, 'border-top-width', true) + self.win.innerHeight() - self.railh.height : pos.top + getWidthToPixel(self.win, 'border-top-width', true);
  641. var x = pos.left + getWidthToPixel(self.win, 'border-left-width');
  642. self.railh.css({
  643. top: y - (opt.railpadding.top + opt.railpadding.bottom),
  644. left: x,
  645. width: self.railh.width
  646. });
  647. }
  648. }
  649. };
  650. this.doRailClick = function (e, dbl, hr) {
  651. var fn, pg, cur, pos;
  652. if (self.railslocked) return;
  653. self.cancelEvent(e);
  654. if (!("pageY" in e)) {
  655. e.pageX = e.clientX + _doc.documentElement.scrollLeft;
  656. e.pageY = e.clientY + _doc.documentElement.scrollTop;
  657. }
  658. if (dbl) {
  659. fn = (hr) ? self.doScrollLeft : self.doScrollTop;
  660. cur = (hr) ? ((e.pageX - self.railh.offset().left - (self.cursorwidth / 2)) * self.scrollratio.x) : ((e.pageY - self.rail.offset().top - (self.cursorheight / 2)) * self.scrollratio.y);
  661. self.unsynched("relativexy");
  662. fn(cur|0);
  663. } else {
  664. fn = (hr) ? self.doScrollLeftBy : self.doScrollBy;
  665. cur = (hr) ? self.scroll.x : self.scroll.y;
  666. pos = (hr) ? e.pageX - self.railh.offset().left : e.pageY - self.rail.offset().top;
  667. pg = (hr) ? self.view.w : self.view.h;
  668. fn((cur >= pos) ? pg : -pg);
  669. }
  670. };
  671. self.newscrolly = self.newscrollx = 0;
  672. self.hasanimationframe = ("requestAnimationFrame" in _win);
  673. self.hascancelanimationframe = ("cancelAnimationFrame" in _win);
  674. self.hasborderbox = false;
  675. this.init = function () {
  676. self.saved.css = [];
  677. if (cap.isoperamini) return true; // SORRY, DO NOT WORK!
  678. if (cap.isandroid && !("hidden" in _doc)) return true; // Android 3- SORRY, DO NOT WORK!
  679. opt.emulatetouch = opt.emulatetouch || opt.touchbehavior; // mantain compatibility with "touchbehavior"
  680. self.hasborderbox = _win.getComputedStyle && (_win.getComputedStyle(_doc.body)['box-sizing'] === "border-box");
  681. var _scrollyhidden = { 'overflow-y': 'hidden' };
  682. if (cap.isie11 || cap.isie10) _scrollyhidden['-ms-overflow-style'] = 'none'; // IE 10 & 11 is always a world apart!
  683. if (self.ishwscroll) {
  684. this.doc.css(cap.transitionstyle, cap.prefixstyle + 'transform 0ms ease-out');
  685. if (cap.transitionend) self.bind(self.doc, cap.transitionend, self.onScrollTransitionEnd, false); //I have got to do something usefull!!
  686. }
  687. self.zindex = "auto";
  688. if (!self.ispage && opt.zindex == "auto") {
  689. self.zindex = getZIndex() || "auto";
  690. } else {
  691. self.zindex = opt.zindex;
  692. }
  693. if (!self.ispage && self.zindex != "auto" && self.zindex > globalmaxzindex) {
  694. globalmaxzindex = self.zindex;
  695. }
  696. if (self.isie && self.zindex === 0 && opt.zindex == "auto") { // fix IE auto == 0
  697. self.zindex = "auto";
  698. }
  699. if (!self.ispage || !cap.isieold) {
  700. var cont = self.docscroll;
  701. if (self.ispage) cont = (self.haswrapper) ? self.win : self.doc;
  702. self.css(cont, _scrollyhidden);
  703. if (self.ispage && (cap.isie11 || cap.isie)) { // IE 7-11
  704. self.css($("html"), _scrollyhidden);
  705. }
  706. if (cap.isios && !self.ispage && !self.haswrapper) self.css($body, {
  707. "-webkit-overflow-scrolling": "touch"
  708. }); //force hw acceleration
  709. var cursor = $(_doc.createElement('div'));
  710. cursor.css({
  711. position: "relative",
  712. top: 0,
  713. "float": "right",
  714. width: opt.cursorwidth,
  715. height: 0,
  716. 'background-color': opt.cursorcolor,
  717. border: opt.cursorborder,
  718. 'background-clip': 'padding-box',
  719. '-webkit-border-radius': opt.cursorborderradius,
  720. '-moz-border-radius': opt.cursorborderradius,
  721. 'border-radius': opt.cursorborderradius
  722. });
  723. cursor.addClass('nicescroll-cursors');
  724. self.cursor = cursor;
  725. var rail = $(_doc.createElement('div'));
  726. rail.attr('id', self.id);
  727. rail.addClass('nicescroll-rails nicescroll-rails-vr');
  728. if (opt.scrollCLass) {
  729. rail.addClass(opt.scrollCLass);
  730. }
  731. var v, a, kp = ["left", "right", "top", "bottom"]; //**
  732. for (var n in kp) {
  733. a = kp[n];
  734. v = opt.railpadding[a] || 0;
  735. v && rail.css("padding-" + a, v + "px");
  736. }
  737. rail.append(cursor);
  738. rail.width = Math.max(parseFloat(opt.cursorwidth), cursor.outerWidth());
  739. rail.css({
  740. width: rail.width + "px",
  741. zIndex: self.zindex,
  742. background: opt.background,
  743. cursor: "default"
  744. });
  745. rail.visibility = true;
  746. rail.scrollable = true;
  747. rail.align = (opt.railalign == "left") ? 0 : 1;
  748. self.rail = rail;
  749. self.rail.drag = false;
  750. var zoom = false;
  751. if (opt.boxzoom && !self.ispage && !cap.isieold) {
  752. zoom = _doc.createElement('div');
  753. self.bind(zoom, "click", self.doZoom);
  754. self.bind(zoom, "mouseenter", function () {
  755. self.zoom.css('opacity', opt.cursoropacitymax);
  756. });
  757. self.bind(zoom, "mouseleave", function () {
  758. self.zoom.css('opacity', opt.cursoropacitymin);
  759. });
  760. self.zoom = $(zoom);
  761. self.zoom.css({
  762. cursor: "pointer",
  763. zIndex: self.zindex,
  764. backgroundImage: 'url(' + opt.scriptpath + 'zoomico.png)',
  765. height: 18,
  766. width: 18,
  767. backgroundPosition: '0 0'
  768. });
  769. if (opt.dblclickzoom) self.bind(self.win, "dblclick", self.doZoom);
  770. if (cap.cantouch && opt.gesturezoom) {
  771. self.ongesturezoom = function (e) {
  772. if (e.scale > 1.5) self.doZoomIn(e);
  773. if (e.scale < 0.8) self.doZoomOut(e);
  774. return self.cancelEvent(e);
  775. };
  776. self.bind(self.win, "gestureend", self.ongesturezoom);
  777. }
  778. }
  779. // init HORIZ
  780. self.railh = false;
  781. var railh;
  782. if (opt.horizrailenabled) {
  783. self.css(cont, {
  784. overflowX: 'hidden'
  785. });
  786. cursor = $(_doc.createElement('div'));
  787. cursor.css({
  788. position: "absolute",
  789. top: 0,
  790. height: opt.cursorwidth,
  791. width: 0,
  792. backgroundColor: opt.cursorcolor,
  793. border: opt.cursorborder,
  794. backgroundClip: 'padding-box',
  795. '-webkit-border-radius': opt.cursorborderradius,
  796. '-moz-border-radius': opt.cursorborderradius,
  797. 'border-radius': opt.cursorborderradius
  798. });
  799. if (cap.isieold) cursor.css('overflow', 'hidden'); //IE6 horiz scrollbar issue
  800. cursor.addClass('nicescroll-cursors');
  801. self.cursorh = cursor;
  802. railh = $(_doc.createElement('div'));
  803. railh.attr('id', self.id + '-hr');
  804. railh.addClass('nicescroll-rails nicescroll-rails-hr');
  805. if (opt.scrollCLass) {
  806. railh.addClass(opt.scrollCLass);
  807. }
  808. railh.height = Math.max(parseFloat(opt.cursorwidth), cursor.outerHeight());
  809. railh.css({
  810. height: railh.height + "px",
  811. 'zIndex': self.zindex,
  812. "background": opt.background
  813. });
  814. railh.append(cursor);
  815. railh.visibility = true;
  816. railh.scrollable = true;
  817. railh.align = (opt.railvalign == "top") ? 0 : 1;
  818. self.railh = railh;
  819. self.railh.drag = false;
  820. }
  821. if (self.ispage) {
  822. rail.css({
  823. position: "fixed",
  824. top: 0,
  825. height: "100%"
  826. });
  827. rail.css((rail.align) ? { right: 0 } : { left: 0 });
  828. self.body.append(rail);
  829. if (self.railh) {
  830. railh.css({
  831. position: "fixed",
  832. left: 0,
  833. width: "100%"
  834. });
  835. railh.css((railh.align) ? { bottom: 0 } : { top: 0 });
  836. self.body.append(railh);
  837. }
  838. } else {
  839. if (self.ishwscroll) {
  840. if (self.win.css('position') == 'static') self.css(self.win, { 'position': 'relative' });
  841. var bd = (self.win[0].nodeName == 'HTML') ? self.body : self.win;
  842. $(bd).scrollTop(0).scrollLeft(0); // fix rail position if content already scrolled
  843. if (self.zoom) {
  844. self.zoom.css({
  845. position: "absolute",
  846. top: 1,
  847. right: 0,
  848. "margin-right": rail.width + 4
  849. });
  850. bd.append(self.zoom);
  851. }
  852. rail.css({
  853. position: "absolute",
  854. top: 0
  855. });
  856. rail.css((rail.align) ? { right: 0 } : { left: 0 });
  857. bd.append(rail);
  858. if (railh) {
  859. railh.css({
  860. position: "absolute",
  861. left: 0,
  862. bottom: 0
  863. });
  864. railh.css((railh.align) ? { bottom: 0 } : { top: 0 });
  865. bd.append(railh);
  866. }
  867. } else {
  868. self.isfixed = (self.win.css("position") == "fixed");
  869. var rlpos = (self.isfixed) ? "fixed" : "absolute";
  870. if (!self.isfixed) self.viewport = self.getViewport(self.win[0]);
  871. if (self.viewport) {
  872. self.body = self.viewport;
  873. if (!(/fixed|absolute/.test(self.viewport.css("position")))) self.css(self.viewport, {
  874. "position": "relative"
  875. });
  876. }
  877. rail.css({
  878. position: rlpos
  879. });
  880. if (self.zoom) self.zoom.css({
  881. position: rlpos
  882. });
  883. self.updateScrollBar();
  884. self.body.append(rail);
  885. if (self.zoom) self.body.append(self.zoom);
  886. if (self.railh) {
  887. railh.css({
  888. position: rlpos
  889. });
  890. self.body.append(railh);
  891. }
  892. }
  893. if (cap.isios) self.css(self.win, {
  894. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)',
  895. '-webkit-touch-callout': 'none'
  896. }); // prevent grey layer on click
  897. if (opt.disableoutline) {
  898. if (cap.isie) self.win.attr("hideFocus", "true"); // IE, prevent dotted rectangle on focused div
  899. if (cap.iswebkit) self.win.css('outline', 'none'); // Webkit outline
  900. }
  901. }
  902. if (opt.autohidemode === false) {
  903. self.autohidedom = false;
  904. self.rail.css({
  905. opacity: opt.cursoropacitymax
  906. });
  907. if (self.railh) self.railh.css({
  908. opacity: opt.cursoropacitymax
  909. });
  910. } else if ((opt.autohidemode === true) || (opt.autohidemode === "leave")) {
  911. self.autohidedom = $().add(self.rail);
  912. if (cap.isie8) self.autohidedom = self.autohidedom.add(self.cursor);
  913. if (self.railh) self.autohidedom = self.autohidedom.add(self.railh);
  914. if (self.railh && cap.isie8) self.autohidedom = self.autohidedom.add(self.cursorh);
  915. } else if (opt.autohidemode == "scroll") {
  916. self.autohidedom = $().add(self.rail);
  917. if (self.railh) self.autohidedom = self.autohidedom.add(self.railh);
  918. } else if (opt.autohidemode == "cursor") {
  919. self.autohidedom = $().add(self.cursor);
  920. if (self.railh) self.autohidedom = self.autohidedom.add(self.cursorh);
  921. } else if (opt.autohidemode == "hidden") {
  922. self.autohidedom = false;
  923. self.hide();
  924. self.railslocked = false;
  925. }
  926. if (cap.cantouch || self.istouchcapable || opt.emulatetouch || cap.hasmstouch) {
  927. self.scrollmom = new ScrollMomentumClass2D(self);
  928. var delayedclick = null;
  929. self.ontouchstart = function (e) {
  930. if (self.locked) return false;
  931. //if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false;
  932. if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType === e.MSPOINTER_TYPE_MOUSE)) return false; // need test on surface!!
  933. self.hasmoving = false;
  934. if (self.scrollmom.timer) {
  935. self.triggerScrollEnd();
  936. self.scrollmom.stop();
  937. }
  938. if (!self.railslocked) {
  939. var tg = self.getTarget(e);
  940. if (tg) {
  941. var skp = (/INPUT/i.test(tg.nodeName)) && (/range/i.test(tg.type));
  942. if (skp) return self.stopPropagation(e);
  943. }
  944. var ismouse = (e.type === "mousedown");
  945. if (!("clientX" in e) && ("changedTouches" in e)) {
  946. e.clientX = e.changedTouches[0].clientX;
  947. e.clientY = e.changedTouches[0].clientY;
  948. }
  949. if (self.forcescreen) {
  950. var le = e;
  951. e = {
  952. "original": (e.original) ? e.original : e
  953. };
  954. e.clientX = le.screenX;
  955. e.clientY = le.screenY;
  956. }
  957. self.rail.drag = {
  958. x: e.clientX,
  959. y: e.clientY,
  960. sx: self.scroll.x,
  961. sy: self.scroll.y,
  962. st: self.getScrollTop(),
  963. sl: self.getScrollLeft(),
  964. pt: 2,
  965. dl: false,
  966. tg: tg
  967. };
  968. if (self.ispage || !opt.directionlockdeadzone) {
  969. self.rail.drag.dl = "f";
  970. } else {
  971. var view = {
  972. w: $window.width(),
  973. h: $window.height()
  974. };
  975. var page = self.getContentSize();
  976. var maxh = page.h - view.h;
  977. var maxw = page.w - view.w;
  978. if (self.rail.scrollable && !self.railh.scrollable) self.rail.drag.ck = (maxh > 0) ? "v" : false;
  979. else if (!self.rail.scrollable && self.railh.scrollable) self.rail.drag.ck = (maxw > 0) ? "h" : false;
  980. else self.rail.drag.ck = false;
  981. }
  982. if (opt.emulatetouch && self.isiframe && cap.isie) {
  983. var wp = self.win.position();
  984. self.rail.drag.x += wp.left;
  985. self.rail.drag.y += wp.top;
  986. }
  987. self.hasmoving = false;
  988. self.lastmouseup = false;
  989. self.scrollmom.reset(e.clientX, e.clientY);
  990. if (tg&&ismouse) {
  991. var ip = /INPUT|SELECT|BUTTON|TEXTAREA/i.test(tg.nodeName);
  992. if (!ip) {
  993. if (cap.hasmousecapture) tg.setCapture();
  994. if (opt.emulatetouch) {
  995. if (tg.onclick && !(tg._onclick || false)) { // intercept DOM0 onclick event
  996. tg._onclick = tg.onclick;
  997. tg.onclick = function (e) {
  998. if (self.hasmoving) return false;
  999. tg._onclick.call(this, e);
  1000. };
  1001. }
  1002. return self.cancelEvent(e);
  1003. }
  1004. return self.stopPropagation(e);
  1005. }
  1006. if (/SUBMIT|CANCEL|BUTTON/i.test($(tg).attr('type'))) {
  1007. self.preventclick = {
  1008. "tg": tg,
  1009. "click": false
  1010. };
  1011. }
  1012. }
  1013. }
  1014. };
  1015. self.ontouchend = function (e) {
  1016. if (!self || !self.rail.drag) return true;
  1017. if (self.rail.drag.pt == 2) {
  1018. //if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false;
  1019. if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType === e.MSPOINTER_TYPE_MOUSE)) return false;
  1020. self.rail.drag = false;
  1021. var ismouse = (e.type === "mouseup");
  1022. if (self.hasmoving) {
  1023. self.scrollmom.doMomentum();
  1024. self.lastmouseup = true;
  1025. self.hideCursor();
  1026. if (cap.hasmousecapture) _doc.releaseCapture();
  1027. if (ismouse) return self.cancelEvent(e);
  1028. }
  1029. }
  1030. else if (self.rail.drag.pt == 1) {
  1031. return self.onmouseup(e);
  1032. }
  1033. };
  1034. var moveneedoffset = (opt.emulatetouch && self.isiframe && !cap.hasmousecapture);
  1035. var locktollerance = opt.directionlockdeadzone * 0.3 | 0;
  1036. self.ontouchmove = function (e, byiframe) {
  1037. if (!self || !self.rail.drag) return true;
  1038. if (e.targetTouches && opt.preventmultitouchscrolling) {
  1039. if (e.targetTouches.length > 1) return true; // multitouch
  1040. }
  1041. //if (e.pointerType && e.pointerType != 2 && e.pointerType != "touch") return false;
  1042. if (e.pointerType && (e.pointerType === 'mouse' || e.pointerType === e.MSPOINTER_TYPE_MOUSE)) return true;
  1043. if (self.rail.drag.pt == 2) {
  1044. if (("changedTouches" in e)) {
  1045. e.clientX = e.changedTouches[0].clientX;
  1046. e.clientY = e.changedTouches[0].clientY;
  1047. }
  1048. var ofy, ofx;
  1049. ofx = ofy = 0;
  1050. if (moveneedoffset && !byiframe) {
  1051. var wp = self.win.position();
  1052. ofx = -wp.left;
  1053. ofy = -wp.top;
  1054. }
  1055. var fy = e.clientY + ofy;
  1056. var my = (fy - self.rail.drag.y);
  1057. var fx = e.clientX + ofx;
  1058. var mx = (fx - self.rail.drag.x);
  1059. var ny = self.rail.drag.st - my;
  1060. if (self.ishwscroll && opt.bouncescroll) {
  1061. if (ny < 0) {
  1062. ny = Math.round(ny / 2);
  1063. } else if (ny > self.page.maxh) {
  1064. ny = self.page.maxh + Math.round((ny - self.page.maxh) / 2);
  1065. }
  1066. } else {
  1067. if (ny < 0) {
  1068. ny = 0;
  1069. fy = 0;
  1070. }
  1071. else if (ny > self.page.maxh) {
  1072. ny = self.page.maxh;
  1073. fy = 0;
  1074. }
  1075. if (fy === 0 && !self.hasmoving) {
  1076. if (!self.ispage) self.rail.drag = false;
  1077. return true;
  1078. }
  1079. }
  1080. var nx = self.getScrollLeft();
  1081. if (self.railh && self.railh.scrollable) {
  1082. nx = (self.isrtlmode) ? mx - self.rail.drag.sl : self.rail.drag.sl - mx;
  1083. if (self.ishwscroll && opt.bouncescroll) {
  1084. if (nx < 0) {
  1085. nx = Math.round(nx / 2);
  1086. } else if (nx > self.page.maxw) {
  1087. nx = self.page.maxw + Math.round((nx - self.page.maxw) / 2);
  1088. }
  1089. } else {
  1090. if (nx < 0) {
  1091. nx = 0;
  1092. fx = 0;
  1093. }
  1094. if (nx > self.page.maxw) {
  1095. nx = self.page.maxw;
  1096. fx = 0;
  1097. }
  1098. }
  1099. }
  1100. if (!self.hasmoving) {
  1101. if (self.rail.drag.y === e.clientY && self.rail.drag.x === e.clientX) return self.cancelEvent(e); // prevent first useless move event
  1102. var ay = Math.abs(my);
  1103. var ax = Math.abs(mx);
  1104. var dz = opt.directionlockdeadzone;
  1105. if (!self.rail.drag.ck) {
  1106. if (ay > dz && ax > dz) self.rail.drag.dl = "f";
  1107. else if (ay > dz) self.rail.drag.dl = (ax > locktollerance) ? "f" : "v";
  1108. else if (ax > dz) self.rail.drag.dl = (ay > locktollerance) ? "f" : "h";
  1109. }
  1110. else if (self.rail.drag.ck == "v") {
  1111. if (ax > dz && ay <= locktollerance) {
  1112. self.rail.drag = false;
  1113. }
  1114. else if (ay > dz) self.rail.drag.dl = "v";
  1115. }
  1116. else if (self.rail.drag.ck == "h") {
  1117. if (ay > dz && ax <= locktollerance) {
  1118. self.rail.drag = false;
  1119. }
  1120. else if (ax > dz) self.rail.drag.dl = "h";
  1121. }
  1122. if (!self.rail.drag.dl) return self.cancelEvent(e);
  1123. self.triggerScrollStart(e.clientX, e.clientY, 0, 0, 0);
  1124. self.hasmoving = true;
  1125. }
  1126. if (self.preventclick && !self.preventclick.click) {
  1127. self.preventclick.click = self.preventclick.tg.onclick || false;
  1128. self.preventclick.tg.onclick = self.onpreventclick;
  1129. }
  1130. if (self.rail.drag.dl) {
  1131. if (self.rail.drag.dl == "v") nx = self.rail.drag.sl;
  1132. else if (self.rail.drag.dl == "h") ny = self.rail.drag.st;
  1133. }
  1134. self.synched("touchmove", function () {
  1135. if (self.rail.drag && (self.rail.drag.pt == 2)) {
  1136. if (self.prepareTransition) self.resetTransition();
  1137. if (self.rail.scrollable) self.setScrollTop(ny);
  1138. self.scrollmom.update(fx, fy);
  1139. if (self.railh && self.railh.scrollable) {
  1140. self.setScrollLeft(nx);
  1141. self.showCursor(ny, nx);
  1142. } else {
  1143. self.showCursor(ny);
  1144. }
  1145. if (cap.isie10) _doc.selection.clear();
  1146. }
  1147. });
  1148. return self.cancelEvent(e);
  1149. }
  1150. else if (self.rail.drag.pt == 1) { // drag on cursor
  1151. return self.onmousemove(e);
  1152. }
  1153. };
  1154. self.ontouchstartCursor = function (e, hronly) {
  1155. if (self.rail.drag && self.rail.drag.pt != 3) return;
  1156. if (self.locked) return self.cancelEvent(e);
  1157. self.cancelScroll();
  1158. self.rail.drag = {
  1159. x: e.touches[0].clientX,
  1160. y: e.touches[0].clientY,
  1161. sx: self.scroll.x,
  1162. sy: self.scroll.y,
  1163. pt: 3,
  1164. hr: (!!hronly)
  1165. };
  1166. var tg = self.getTarget(e);
  1167. if (!self.ispage && cap.hasmousecapture) tg.setCapture();
  1168. if (self.isiframe && !cap.hasmousecapture) {
  1169. self.saved.csspointerevents = self.doc.css("pointer-events");
  1170. self.css(self.doc, { "pointer-events": "none" });
  1171. }
  1172. return self.cancelEvent(e);
  1173. };
  1174. self.ontouchendCursor = function (e) {
  1175. if (self.rail.drag) {
  1176. if (cap.hasmousecapture) _doc.releaseCapture();
  1177. if (self.isiframe && !cap.hasmousecapture) self.doc.css("pointer-events", self.saved.csspointerevents);
  1178. if (self.rail.drag.pt != 3) return;
  1179. self.rail.drag = false;
  1180. return self.cancelEvent(e);
  1181. }
  1182. };
  1183. self.ontouchmoveCursor = function (e) {
  1184. if (self.rail.drag) {
  1185. if (self.rail.drag.pt != 3) return;
  1186. self.cursorfreezed = true;
  1187. if (self.rail.drag.hr) {
  1188. self.scroll.x = self.rail.drag.sx + (e.touches[0].clientX - self.rail.drag.x);
  1189. if (self.scroll.x < 0) self.scroll.x = 0;
  1190. var mw = self.scrollvaluemaxw;
  1191. if (self.scroll.x > mw) self.scroll.x = mw;
  1192. } else {
  1193. self.scroll.y = self.rail.drag.sy + (e.touches[0].clientY - self.rail.drag.y);
  1194. if (self.scroll.y < 0) self.scroll.y = 0;
  1195. var my = self.scrollvaluemax;
  1196. if (self.scroll.y > my) self.scroll.y = my;
  1197. }
  1198. self.synched('touchmove', function () {
  1199. if (self.rail.drag && (self.rail.drag.pt == 3)) {
  1200. self.showCursor();
  1201. if (self.rail.drag.hr) self.doScrollLeft(Math.round(self.scroll.x * self.scrollratio.x), opt.cursordragspeed);
  1202. else self.doScrollTop(Math.round(self.scroll.y * self.scrollratio.y), opt.cursordragspeed);
  1203. }
  1204. });
  1205. return self.cancelEvent(e);
  1206. }
  1207. };
  1208. }
  1209. self.onmousedown = function (e, hronly) {
  1210. if (self.rail.drag && self.rail.drag.pt != 1) return;
  1211. if (self.railslocked) return self.cancelEvent(e);
  1212. self.cancelScroll();
  1213. self.rail.drag = {
  1214. x: e.clientX,
  1215. y: e.clientY,
  1216. sx: self.scroll.x,
  1217. sy: self.scroll.y,
  1218. pt: 1,
  1219. hr: hronly || false
  1220. };
  1221. var tg = self.getTarget(e);
  1222. if (cap.hasmousecapture) tg.setCapture();
  1223. if (self.isiframe && !cap.hasmousecapture) {
  1224. self.saved.csspointerevents = self.doc.css("pointer-events");
  1225. self.css(self.doc, {
  1226. "pointer-events": "none"
  1227. });
  1228. }
  1229. self.hasmoving = false;
  1230. return self.cancelEvent(e);
  1231. };
  1232. self.onmouseup = function (e) {
  1233. if (self.rail.drag) {
  1234. if (self.rail.drag.pt != 1) return true;
  1235. if (cap.hasmousecapture) _doc.releaseCapture();
  1236. if (self.isiframe && !cap.hasmousecapture) self.doc.css("pointer-events", self.saved.csspointerevents);
  1237. self.rail.drag = false;
  1238. self.cursorfreezed = false;
  1239. if (self.hasmoving) self.triggerScrollEnd();
  1240. return self.cancelEvent(e);
  1241. }
  1242. };
  1243. self.onmousemove = function (e) {
  1244. if (self.rail.drag) {
  1245. if (self.rail.drag.pt !== 1) return;
  1246. if (cap.ischrome && e.which === 0) return self.onmouseup(e);
  1247. self.cursorfreezed = true;
  1248. if (!self.hasmoving) self.triggerScrollStart(e.clientX, e.clientY, 0, 0, 0);
  1249. self.hasmoving = true;
  1250. if (self.rail.drag.hr) {
  1251. self.scroll.x = self.rail.drag.sx + (e.clientX - self.rail.drag.x);
  1252. if (self.scroll.x < 0) self.scroll.x = 0;
  1253. var mw = self.scrollvaluemaxw;
  1254. if (self.scroll.x > mw) self.scroll.x = mw;
  1255. } else {
  1256. self.scroll.y = self.rail.drag.sy + (e.clientY - self.rail.drag.y);
  1257. if (self.scroll.y < 0) self.scroll.y = 0;
  1258. var my = self.scrollvaluemax;
  1259. if (self.scroll.y > my) self.scroll.y = my;
  1260. }
  1261. self.synched('mousemove', function () {
  1262. if (self.cursorfreezed) {
  1263. self.showCursor();
  1264. if (self.rail.drag.hr) {
  1265. self.scrollLeft(Math.round(self.scroll.x * self.scrollratio.x));
  1266. } else {
  1267. self.scrollTop(Math.round(self.scroll.y * self.scrollratio.y));
  1268. }
  1269. }
  1270. });
  1271. return self.cancelEvent(e);
  1272. }
  1273. else {
  1274. self.checkarea = 0;
  1275. }
  1276. };
  1277. if (cap.cantouch || opt.emulatetouch) {
  1278. self.onpreventclick = function (e) {
  1279. if (self.preventclick) {
  1280. self.preventclick.tg.onclick = self.preventclick.click;
  1281. self.preventclick = false;
  1282. return self.cancelEvent(e);
  1283. }
  1284. };
  1285. self.onclick = (cap.isios) ? false : function (e) { // it needs to check IE11 ???
  1286. if (self.lastmouseup) {
  1287. self.lastmouseup = false;
  1288. return self.cancelEvent(e);
  1289. } else {
  1290. return true;
  1291. }
  1292. };
  1293. if (opt.grabcursorenabled && cap.cursorgrabvalue) {
  1294. self.css((self.ispage) ? self.doc : self.win, {
  1295. 'cursor': cap.cursorgrabvalue
  1296. });
  1297. self.css(self.rail, {
  1298. 'cursor': cap.cursorgrabvalue
  1299. });
  1300. }
  1301. } else {
  1302. var checkSelectionScroll = function (e) {
  1303. if (!self.selectiondrag) return;
  1304. if (e) {
  1305. var ww = self.win.outerHeight();
  1306. var df = (e.pageY - self.selectiondrag.top);
  1307. if (df > 0 && df < ww) df = 0;
  1308. if (df >= ww) df -= ww;
  1309. self.selectiondrag.df = df;
  1310. }
  1311. if (self.selectiondrag.df === 0) return;
  1312. var rt = -(self.selectiondrag.df*2/6)|0;
  1313. self.doScrollBy(rt);
  1314. self.debounced("doselectionscroll", function () {
  1315. checkSelectionScroll();
  1316. }, 50);
  1317. };
  1318. if ("getSelection" in _doc) { // A grade - Major browsers
  1319. self.hasTextSelected = function () {
  1320. return (_doc.getSelection().rangeCount > 0);
  1321. };
  1322. } else if ("selection" in _doc) { //IE9-
  1323. self.hasTextSelected = function () {
  1324. return (_doc.selection.type != "None");
  1325. };
  1326. } else {
  1327. self.hasTextSelected = function () { // no support
  1328. return false;
  1329. };
  1330. }
  1331. self.onselectionstart = function (e) {
  1332. // More testing - severe chrome issues
  1333. /*
  1334. if (!self.haswrapper&&(e.which&&e.which==2)) { // fool browser to manage middle button scrolling
  1335. self.win.css({'overflow':'auto'});
  1336. setTimeout(function(){
  1337. self.win.css({'overflow':'hidden'});
  1338. },10);
  1339. return true;
  1340. }
  1341. */
  1342. if (self.ispage) return;
  1343. self.selectiondrag = self.win.offset();
  1344. };
  1345. self.onselectionend = function (e) {
  1346. self.selectiondrag = false;
  1347. };
  1348. self.onselectiondrag = function (e) {
  1349. if (!self.selectiondrag) return;
  1350. if (self.hasTextSelected()) self.debounced("selectionscroll", function () {
  1351. checkSelectionScroll(e);
  1352. }, 250);
  1353. };
  1354. }
  1355. if (cap.hasw3ctouch) { //IE11+
  1356. self.css((self.ispage) ? $("html") : self.win, { 'touch-action': 'none' });
  1357. self.css(self.rail, {
  1358. 'touch-action': 'none'
  1359. });
  1360. self.css(self.cursor, {
  1361. 'touch-action': 'none'
  1362. });
  1363. self.bind(self.win, "pointerdown", self.ontouchstart);
  1364. self.bind(_doc, "pointerup", self.ontouchend);
  1365. self.delegate(_doc, "pointermove", self.ontouchmove);
  1366. } else if (cap.hasmstouch) { //IE10
  1367. self.css((self.ispage) ? $("html") : self.win, { '-ms-touch-action': 'none' });
  1368. self.css(self.rail, {
  1369. '-ms-touch-action': 'none'
  1370. });
  1371. self.css(self.cursor, {
  1372. '-ms-touch-action': 'none'
  1373. });
  1374. self.bind(self.win, "MSPointerDown", self.ontouchstart);
  1375. self.bind(_doc, "MSPointerUp", self.ontouchend);
  1376. self.delegate(_doc, "MSPointerMove", self.ontouchmove);
  1377. self.bind(self.cursor, "MSGestureHold", function (e) {
  1378. e.preventDefault();
  1379. });
  1380. self.bind(self.cursor, "contextmenu", function (e) {
  1381. e.preventDefault();
  1382. });
  1383. } else if (cap.cantouch) { // smartphones/touch devices
  1384. self.bind(self.win, "touchstart", self.ontouchstart, false, true);
  1385. self.bind(_doc, "touchend", self.ontouchend, false, true);
  1386. self.bind(_doc, "touchcancel", self.ontouchend, false, true);
  1387. self.delegate(_doc, "touchmove", self.ontouchmove, false, true);
  1388. }
  1389. if (opt.emulatetouch) {
  1390. self.bind(self.win, "mousedown", self.ontouchstart, false, true);
  1391. self.bind(_doc, "mouseup", self.ontouchend, false, true);
  1392. self.bind(_doc, "mousemove", self.ontouchmove, false, true);
  1393. }
  1394. if (opt.cursordragontouch || (!cap.cantouch && !opt.emulatetouch)) {
  1395. self.rail.css({
  1396. cursor: "default"
  1397. });
  1398. self.railh && self.railh.css({
  1399. cursor: "default"
  1400. });
  1401. self.jqbind(self.rail, "mouseenter", function () {
  1402. if (!self.ispage && !self.win.is(":visible")) return false;
  1403. if (self.canshowonmouseevent) self.showCursor();
  1404. self.rail.active = true;
  1405. });
  1406. self.jqbind(self.rail, "mouseleave", function () {
  1407. self.rail.active = false;
  1408. if (!self.rail.drag) self.hideCursor();
  1409. });
  1410. if (opt.sensitiverail) {
  1411. self.bind(self.rail, "click", function (e) {
  1412. self.doRailClick(e, false, false);
  1413. });
  1414. self.bind(self.rail, "dblclick", function (e) {
  1415. self.doRailClick(e, true, false);
  1416. });
  1417. self.bind(self.cursor, "click", function (e) {
  1418. self.cancelEvent(e);
  1419. });
  1420. self.bind(self.cursor, "dblclick", function (e) {
  1421. self.cancelEvent(e);
  1422. });
  1423. }
  1424. if (self.railh) {
  1425. self.jqbind(self.railh, "mouseenter", function () {
  1426. if (!self.ispage && !self.win.is(":visible")) return false;
  1427. if (self.canshowonmouseevent) self.showCursor();
  1428. self.rail.active = true;
  1429. });
  1430. self.jqbind(self.railh, "mouseleave", function () {
  1431. self.rail.active = false;
  1432. if (!self.rail.drag) self.hideCursor();
  1433. });
  1434. if (opt.sensitiverail) {
  1435. self.bind(self.railh, "click", function (e) {
  1436. self.doRailClick(e, false, true);
  1437. });
  1438. self.bind(self.railh, "dblclick", function (e) {
  1439. self.doRailClick(e, true, true);
  1440. });
  1441. self.bind(self.cursorh, "click", function (e) {
  1442. self.cancelEvent(e);
  1443. });
  1444. self.bind(self.cursorh, "dblclick", function (e) {
  1445. self.cancelEvent(e);
  1446. });
  1447. }
  1448. }
  1449. }
  1450. if (opt.cursordragontouch && (this.istouchcapable || cap.cantouch)) {
  1451. self.bind(self.cursor, "touchstart", self.ontouchstartCursor);
  1452. self.bind(self.cursor, "touchmove", self.ontouchmoveCursor);
  1453. self.bind(self.cursor, "touchend", self.ontouchendCursor);
  1454. self.cursorh && self.bind(self.cursorh, "touchstart", function (e) {
  1455. self.ontouchstartCursor(e, true);
  1456. });
  1457. self.cursorh && self.bind(self.cursorh, "touchmove", self.ontouchmoveCursor);
  1458. self.cursorh && self.bind(self.cursorh, "touchend", self.ontouchendCursor);
  1459. }
  1460. // if (!cap.cantouch && !opt.emulatetouch) {
  1461. if (!opt.emulatetouch && !cap.isandroid && !cap.isios) {
  1462. self.bind((cap.hasmousecapture) ? self.win : _doc, "mouseup", self.onmouseup);
  1463. self.bind(_doc, "mousemove", self.onmousemove);
  1464. if (self.onclick) self.bind(_doc, "click", self.onclick);
  1465. self.bind(self.cursor, "mousedown", self.onmousedown);
  1466. self.bind(self.cursor, "mouseup", self.onmouseup);
  1467. if (self.railh) {
  1468. self.bind(self.cursorh, "mousedown", function (e) {
  1469. self.onmousedown(e, true);
  1470. });
  1471. self.bind(self.cursorh, "mouseup", self.onmouseup);
  1472. }
  1473. if (!self.ispage && opt.enablescrollonselection) {
  1474. self.bind(self.win[0], "mousedown", self.onselectionstart);
  1475. self.bind(_doc, "mouseup", self.onselectionend);
  1476. self.bind(self.cursor, "mouseup", self.onselectionend);
  1477. if (self.cursorh) self.bind(self.cursorh, "mouseup", self.onselectionend);
  1478. self.bind(_doc, "mousemove", self.onselectiondrag);
  1479. }
  1480. if (self.zoom) {
  1481. self.jqbind(self.zoom, "mouseenter", function () {
  1482. if (self.canshowonmouseevent) self.showCursor();
  1483. self.rail.active = true;
  1484. });
  1485. self.jqbind(self.zoom, "mouseleave", function () {
  1486. self.rail.active = false;
  1487. if (!self.rail.drag) self.hideCursor();
  1488. });
  1489. }
  1490. } else {
  1491. self.bind((cap.hasmousecapture) ? self.win : _doc, "mouseup", self.ontouchend);
  1492. if (self.onclick) self.bind(_doc, "click", self.onclick);
  1493. if (opt.cursordragontouch) {
  1494. self.bind(self.cursor, "mousedown", self.onmousedown);
  1495. self.bind(self.cursor, "mouseup", self.onmouseup);
  1496. self.cursorh && self.bind(self.cursorh, "mousedown", function (e) {
  1497. self.onmousedown(e, true);
  1498. });
  1499. self.cursorh && self.bind(self.cursorh, "mouseup", self.onmouseup);
  1500. } else {
  1501. self.bind(self.rail, "mousedown", function (e) { e.preventDefault(); }); // prevent text selection
  1502. self.railh && self.bind(self.railh, "mousedown", function (e) { e.preventDefault(); });
  1503. }
  1504. }
  1505. if (opt.enablemousewheel) {
  1506. if (!self.isiframe) self.mousewheel((cap.isie && self.ispage) ? _doc : self.win, self.onmousewheel);
  1507. self.mousewheel(self.rail, self.onmousewheel);
  1508. if (self.railh) self.mousewheel(self.railh, self.onmousewheelhr);
  1509. }
  1510. if (!self.ispage && !cap.cantouch && !(/HTML|^BODY/.test(self.win[0].nodeName))) {
  1511. if (!self.win.attr("tabindex")) self.win.attr({
  1512. "tabindex": ++tabindexcounter
  1513. });
  1514. self.bind(self.win, "focus", function (e) { // better using native events
  1515. domfocus = (self.getTarget(e)).id || self.getTarget(e) || false;
  1516. self.hasfocus = true;
  1517. if (self.canshowonmouseevent) self.noticeCursor();
  1518. });
  1519. self.bind(self.win, "blur", function (e) { // *
  1520. domfocus = false;
  1521. self.hasfocus = false;
  1522. });
  1523. self.bind(self.win, "mouseenter", function (e) { // *
  1524. mousefocus = (self.getTarget(e)).id || self.getTarget(e) || false;
  1525. self.hasmousefocus = true;
  1526. if (self.canshowonmouseevent) self.noticeCursor();
  1527. });
  1528. self.bind(self.win, "mouseleave", function (e) { // *
  1529. mousefocus = false;
  1530. self.hasmousefocus = false;
  1531. if (!self.rail.drag) self.hideCursor();
  1532. });
  1533. }
  1534. //Thanks to http://www.quirksmode.org !!
  1535. self.onkeypress = function (e) {
  1536. if (self.railslocked && self.page.maxh === 0) return true;
  1537. e = e || _win.event;
  1538. var tg = self.getTarget(e);
  1539. if (tg && /INPUT|TEXTAREA|SELECT|OPTION/.test(tg.nodeName)) {
  1540. var tp = tg.getAttribute('type') || tg.type || false;
  1541. if ((!tp) || !(/submit|button|cancel/i.tp)) return true;
  1542. }
  1543. if ($(tg).attr('contenteditable')) return true;
  1544. if (self.hasfocus || (self.hasmousefocus && !domfocus) || (self.ispage && !domfocus && !mousefocus)) {
  1545. var key = e.keyCode;
  1546. if (self.railslocked && key != 27) return self.cancelEvent(e);
  1547. var ctrl = e.ctrlKey || false;
  1548. var shift = e.shiftKey || false;
  1549. var ret = false;
  1550. switch (key) {
  1551. case 38:
  1552. case 63233: //safari
  1553. self.doScrollBy(24 * 3);
  1554. ret = true;
  1555. break;
  1556. case 40:
  1557. case 63235: //safari
  1558. self.doScrollBy(-24 * 3);
  1559. ret = true;
  1560. break;
  1561. case 37:
  1562. case 63232: //safari
  1563. if (self.railh) {
  1564. (ctrl) ? self.doScrollLeft(0) : self.doScrollLeftBy(24 * 3);
  1565. ret = true;
  1566. }
  1567. break;
  1568. case 39:
  1569. case 63234: //safari
  1570. if (self.railh) {
  1571. (ctrl) ? self.doScrollLeft(self.page.maxw) : self.doScrollLeftBy(-24 * 3);
  1572. ret = true;
  1573. }
  1574. break;
  1575. case 33:
  1576. case 63276: // safari
  1577. self.doScrollBy(self.view.h);
  1578. ret = true;
  1579. break;
  1580. case 34:
  1581. case 63277: // safari
  1582. self.doScrollBy(-self.view.h);
  1583. ret = true;
  1584. break;
  1585. case 36:
  1586. case 63273: // safari
  1587. (self.railh && ctrl) ? self.doScrollPos(0, 0) : self.doScrollTo(0);
  1588. ret = true;
  1589. break;
  1590. case 35:
  1591. case 63275: // safari
  1592. (self.railh && ctrl) ? self.doScrollPos(self.page.maxw, self.page.maxh) : self.doScrollTo(self.page.maxh);
  1593. ret = true;
  1594. break;
  1595. case 32:
  1596. if (opt.spacebarenabled) {
  1597. (shift) ? self.doScrollBy(self.view.h) : self.doScrollBy(-self.view.h);
  1598. ret = true;
  1599. }
  1600. break;
  1601. case 27: // ESC
  1602. if (self.zoomactive) {
  1603. self.doZoom();
  1604. ret = true;
  1605. }
  1606. break;
  1607. }
  1608. if (ret) return self.cancelEvent(e);
  1609. }
  1610. };
  1611. if (opt.enablekeyboard) self.bind(_doc, (cap.isopera && !cap.isopera12) ? "keypress" : "keydown", self.onkeypress);
  1612. self.bind(_doc, "keydown", function (e) {
  1613. var ctrl = e.ctrlKey || false;
  1614. if (ctrl) self.wheelprevented = true;
  1615. });
  1616. self.bind(_doc, "keyup", function (e) {
  1617. var ctrl = e.ctrlKey || false;
  1618. if (!ctrl) self.wheelprevented = false;
  1619. });
  1620. self.bind(_win, "blur", function (e) {
  1621. self.wheelprevented = false;
  1622. });
  1623. self.bind(_win, 'resize', self.onscreenresize);
  1624. self.bind(_win, 'orientationchange', self.onscreenresize);
  1625. self.bind(_win, "load", self.lazyResize);
  1626. if (cap.ischrome && !self.ispage && !self.haswrapper) { //chrome void scrollbar bug - it persists in version 26
  1627. var tmp = self.win.attr("style");
  1628. var ww = parseFloat(self.win.css("width")) + 1;
  1629. self.win.css('width', ww);
  1630. self.synched("chromefix", function () {
  1631. self.win.attr("style", tmp);
  1632. });
  1633. }
  1634. // Trying a cross-browser implementation - good luck!
  1635. self.onAttributeChange = function (e) {
  1636. self.lazyResize(self.isieold ? 250 : 30);
  1637. };
  1638. if (opt.enableobserver) {
  1639. if ((!self.isie11) && (ClsMutationObserver !== false)) { // IE11 crashes #568
  1640. self.observerbody = new ClsMutationObserver(function (mutations) {
  1641. mutations.forEach(function (mut) {
  1642. if (mut.type == "attributes") {
  1643. return ($body.hasClass("modal-open") && $body.hasClass("modal-dialog") && !$.contains($('.modal-dialog')[0], self.doc[0])) ? self.hide() : self.show(); // Support for Bootstrap modal; Added check if the nice scroll element is inside a modal
  1644. }
  1645. });
  1646. if (self.me.clientWidth != self.page.width || self.me.clientHeight != self.page.height) return self.lazyResize(30);
  1647. });
  1648. self.observerbody.observe(_doc.body, {
  1649. childList: true,
  1650. subtree: true,
  1651. characterData: false,
  1652. attributes: true,
  1653. attributeFilter: ['class']
  1654. });
  1655. }
  1656. if (!self.ispage && !self.haswrapper) {
  1657. var _dom = self.win[0];
  1658. // redesigned MutationObserver for Chrome18+/Firefox14+/iOS6+ with support for: remove div, add/remove content
  1659. if (ClsMutationObserver !== false) {
  1660. self.observer = new ClsMutationObserver(function (mutations) {
  1661. mutations.forEach(self.onAttributeChange);
  1662. });
  1663. self.observer.observe(_dom, {
  1664. childList: true,
  1665. characterData: false,
  1666. attributes: true,
  1667. subtree: false
  1668. });
  1669. self.observerremover = new ClsMutationObserver(function (mutations) {
  1670. mutations.forEach(function (mo) {
  1671. if (mo.removedNodes.length > 0) {
  1672. for (var dd in mo.removedNodes) {
  1673. if (!!self && (mo.removedNodes[dd] === _dom)) return self.remove();
  1674. }
  1675. }
  1676. });
  1677. });
  1678. self.observerremover.observe(_dom.parentNode, {
  1679. childList: true,
  1680. characterData: false,
  1681. attributes: false,
  1682. subtree: false
  1683. });
  1684. } else {
  1685. self.bind(_dom, (cap.isie && !cap.isie9) ? "propertychange" : "DOMAttrModified", self.onAttributeChange);
  1686. if (cap.isie9) _dom.attachEvent("onpropertychange", self.onAttributeChange); //IE9 DOMAttrModified bug
  1687. self.bind(_dom, "DOMNodeRemoved", function (e) {
  1688. if (e.target === _dom) self.remove();
  1689. });
  1690. }
  1691. }
  1692. }
  1693. //
  1694. if (!self.ispage && opt.boxzoom) self.bind(_win, "resize", self.resizeZoom);
  1695. if (self.istextarea) {
  1696. self.bind(self.win, "keydown", self.lazyResize);
  1697. self.bind(self.win, "mouseup", self.lazyResize);
  1698. }
  1699. self.lazyResize(30);
  1700. }
  1701. if (this.doc[0].nodeName == 'IFRAME') {
  1702. var oniframeload = function () {
  1703. self.iframexd = false;
  1704. var doc;
  1705. try {
  1706. doc = 'contentDocument' in this ? this.contentDocument : this.contentWindow._doc;
  1707. var a = doc.domain;
  1708. } catch (e) {
  1709. self.iframexd = true;
  1710. doc = false;
  1711. }
  1712. if (self.iframexd) {
  1713. if ("console" in _win) console.log('NiceScroll error: policy restriced iframe');
  1714. return true; //cross-domain - I can't manage this
  1715. }
  1716. self.forcescreen = true;
  1717. if (self.isiframe) {
  1718. self.iframe = {
  1719. "doc": $(doc),
  1720. "html": self.doc.contents().find('html')[0],
  1721. "body": self.doc.contents().find('body')[0]
  1722. };
  1723. self.getContentSize = function () {
  1724. return {
  1725. w: Math.max(self.iframe.html.scrollWidth, self.iframe.body.scrollWidth),
  1726. h: Math.max(self.iframe.html.scrollHeight, self.iframe.body.scrollHeight)
  1727. };
  1728. };
  1729. self.docscroll = $(self.iframe.body);
  1730. }
  1731. if (!cap.isios && opt.iframeautoresize && !self.isiframe) {
  1732. self.win.scrollTop(0); // reset position
  1733. self.doc.height(""); //reset height to fix browser bug
  1734. var hh = Math.max(doc.getElementsByTagName('html')[0].scrollHeight, doc.body.scrollHeight);
  1735. self.doc.height(hh);
  1736. }
  1737. self.lazyResize(30);
  1738. self.css($(self.iframe.body), _scrollyhidden);
  1739. if (cap.isios && self.haswrapper) {
  1740. self.css($(doc.body), {
  1741. '-webkit-transform': 'translate3d(0,0,0)'
  1742. }); // avoid iFrame content clipping - thanks to http://blog.derraab.com/2012/04/02/avoid-iframe-content-clipping-with-css-transform-on-ios/
  1743. }
  1744. if ('contentWindow' in this) {
  1745. self.bind(this.contentWindow, "scroll", self.onscroll); //IE8 & minor
  1746. } else {
  1747. self.bind(doc, "scroll", self.onscroll);
  1748. }
  1749. if (opt.enablemousewheel) {
  1750. self.mousewheel(doc, self.onmousewheel);
  1751. }
  1752. if (opt.enablekeyboard) self.bind(doc, (cap.isopera) ? "keypress" : "keydown", self.onkeypress);
  1753. if (cap.cantouch) {
  1754. self.bind(doc, "touchstart", self.ontouchstart);
  1755. self.bind(doc, "touchmove", self.ontouchmove);
  1756. }
  1757. else if (opt.emulatetouch) {
  1758. self.bind(doc, "mousedown", self.ontouchstart);
  1759. self.bind(doc, "mousemove", function (e) {
  1760. return self.ontouchmove(e, true);
  1761. });
  1762. if (opt.grabcursorenabled && cap.cursorgrabvalue) self.css($(doc.body), {
  1763. 'cursor': cap.cursorgrabvalue
  1764. });
  1765. }
  1766. self.bind(doc, "mouseup", self.ontouchend);
  1767. if (self.zoom) {
  1768. if (opt.dblclickzoom) self.bind(doc, 'dblclick', self.doZoom);
  1769. if (self.ongesturezoom) self.bind(doc, "gestureend", self.ongesturezoom);
  1770. }
  1771. };
  1772. if (this.doc[0].readyState && this.doc[0].readyState === "complete") {
  1773. setTimeout(function () {
  1774. oniframeload.call(self.doc[0], false);
  1775. }, 500);
  1776. }
  1777. self.bind(this.doc, "load", oniframeload);
  1778. }
  1779. };
  1780. this.showCursor = function (py, px) {
  1781. if (self.cursortimeout) {
  1782. clearTimeout(self.cursortimeout);
  1783. self.cursortimeout = 0;
  1784. }
  1785. if (!self.rail) return;
  1786. if (self.autohidedom) {
  1787. self.autohidedom.stop().css({
  1788. opacity: opt.cursoropacitymax
  1789. });
  1790. self.cursoractive = true;
  1791. }
  1792. if (!self.rail.drag || self.rail.drag.pt != 1) {
  1793. if (py !== undefined && py !== false) {
  1794. self.scroll.y = (py / self.scrollratio.y) | 0;
  1795. }
  1796. if (px !== undefined) {
  1797. self.scroll.x = (px / self.scrollratio.x) | 0;
  1798. }
  1799. }
  1800. self.cursor.css({
  1801. height: self.cursorheight,
  1802. top: self.scroll.y
  1803. });
  1804. if (self.cursorh) {
  1805. var lx = (self.hasreversehr) ? self.scrollvaluemaxw - self.scroll.x : self.scroll.x;
  1806. self.cursorh.css({
  1807. width: self.cursorwidth,
  1808. left: (!self.rail.align && self.rail.visibility) ? lx + self.rail.width : lx
  1809. });
  1810. self.cursoractive = true;
  1811. }
  1812. if (self.zoom) self.zoom.stop().css({
  1813. opacity: opt.cursoropacitymax
  1814. });
  1815. };
  1816. this.hideCursor = function (tm) {
  1817. if (self.cursortimeout) return;
  1818. if (!self.rail) return;
  1819. if (!self.autohidedom) return;
  1820. if (self.hasmousefocus && opt.autohidemode === "leave") return;
  1821. self.cursortimeout = setTimeout(function () {
  1822. if (!self.rail.active || !self.showonmouseevent) {
  1823. self.autohidedom.stop().animate({
  1824. opacity: opt.cursoropacitymin
  1825. });
  1826. if (self.zoom) self.zoom.stop().animate({
  1827. opacity: opt.cursoropacitymin
  1828. });
  1829. self.cursoractive = false;
  1830. }
  1831. self.cursortimeout = 0;
  1832. }, tm || opt.hidecursordelay);
  1833. };
  1834. this.noticeCursor = function (tm, py, px) {
  1835. self.showCursor(py, px);
  1836. if (!self.rail.active) self.hideCursor(tm);
  1837. };
  1838. this.getContentSize =
  1839. (self.ispage) ?
  1840. function () {
  1841. return {
  1842. w: Math.max(_doc.body.scrollWidth, _doc.documentElement.scrollWidth),
  1843. h: Math.max(_doc.body.scrollHeight, _doc.documentElement.scrollHeight)
  1844. };
  1845. } : (self.haswrapper) ?
  1846. function () {
  1847. return {
  1848. w: self.doc[0].offsetWidth,
  1849. h: self.doc[0].offsetHeight
  1850. };
  1851. } : function () {
  1852. return {
  1853. w: self.docscroll[0].scrollWidth,
  1854. h: self.docscroll[0].scrollHeight
  1855. };
  1856. };
  1857. this.onResize = function (e, page) {
  1858. if (!self || !self.win) return false;
  1859. var premaxh = self.page.maxh,
  1860. premaxw = self.page.maxw,
  1861. previewh = self.view.h,
  1862. previeww = self.view.w;
  1863. self.view = {
  1864. w: (self.ispage) ? self.win.width() : self.win[0].clientWidth,
  1865. h: (self.ispage) ? self.win.height() : self.win[0].clientHeight
  1866. };
  1867. self.page = (page) ? page : self.getContentSize();
  1868. self.page.maxh = Math.max(0, self.page.h - self.view.h);
  1869. self.page.maxw = Math.max(0, self.page.w - self.view.w);
  1870. if ((self.page.maxh == premaxh) && (self.page.maxw == premaxw) && (self.view.w == previeww) && (self.view.h == previewh)) {
  1871. // test position
  1872. if (!self.ispage) {
  1873. var pos = self.win.offset();
  1874. if (self.lastposition) {
  1875. var lst = self.lastposition;
  1876. if ((lst.top == pos.top) && (lst.left == pos.left)) return self; //nothing to do
  1877. }
  1878. self.lastposition = pos;
  1879. } else {
  1880. return self; //nothing to do
  1881. }
  1882. }
  1883. if (self.page.maxh === 0) {
  1884. self.hideRail();
  1885. self.scrollvaluemax = 0;
  1886. self.scroll.y = 0;
  1887. self.scrollratio.y = 0;
  1888. self.cursorheight = 0;
  1889. self.setScrollTop(0);
  1890. if (self.rail) self.rail.scrollable = false;
  1891. } else {
  1892. self.page.maxh -= (opt.railpadding.top + opt.railpadding.bottom);
  1893. self.rail.scrollable = true;
  1894. }
  1895. if (self.page.maxw === 0) {
  1896. self.hideRailHr();
  1897. self.scrollvaluemaxw = 0;
  1898. self.scroll.x = 0;
  1899. self.scrollratio.x = 0;
  1900. self.cursorwidth = 0;
  1901. self.setScrollLeft(0);
  1902. if (self.railh) {
  1903. self.railh.scrollable = false;
  1904. }
  1905. } else {
  1906. self.page.maxw -= (opt.railpadding.left + opt.railpadding.right);
  1907. if (self.railh) self.railh.scrollable = (opt.horizrailenabled);
  1908. }
  1909. self.railslocked = (self.locked) || ((self.page.maxh === 0) && (self.page.maxw === 0));
  1910. if (self.railslocked) {
  1911. if (!self.ispage) self.updateScrollBar(self.view);
  1912. return false;
  1913. }
  1914. if (!self.hidden) {
  1915. if (!self.rail.visibility) self.showRail();
  1916. if (self.railh && !self.railh.visibility) self.showRailHr();
  1917. }
  1918. if (self.istextarea && self.win.css('resize') && self.win.css('resize') != 'none') self.view.h -= 20;
  1919. self.cursorheight = Math.min(self.view.h, Math.round(self.view.h * (self.view.h / self.page.h)));
  1920. self.cursorheight = (opt.cursorfixedheight) ? opt.cursorfixedheight : Math.max(opt.cursorminheight, self.cursorheight);
  1921. self.cursorwidth = Math.min(self.view.w, Math.round(self.view.w * (self.view.w / self.page.w)));
  1922. self.cursorwidth = (opt.cursorfixedheight) ? opt.cursorfixedheight : Math.max(opt.cursorminheight, self.cursorwidth);
  1923. self.scrollvaluemax = self.view.h - self.cursorheight - (opt.railpadding.top + opt.railpadding.bottom);
  1924. if (!self.hasborderbox) self.scrollvaluemax -= self.cursor[0].offsetHeight - self.cursor[0].clientHeight;
  1925. if (self.railh) {
  1926. self.railh.width = (self.page.maxh > 0) ? (self.view.w - self.rail.width) : self.view.w;
  1927. self.scrollvaluemaxw = self.railh.width - self.cursorwidth - (opt.railpadding.left + opt.railpadding.right);
  1928. }
  1929. if (!self.ispage) self.updateScrollBar(self.view);
  1930. self.scrollratio = {
  1931. x: (self.page.maxw / self.scrollvaluemaxw),
  1932. y: (self.page.maxh / self.scrollvaluemax)
  1933. };
  1934. var sy = self.getScrollTop();
  1935. if (sy > self.page.maxh) {
  1936. self.doScrollTop(self.page.maxh);
  1937. } else {
  1938. self.scroll.y = (self.getScrollTop() / self.scrollratio.y) | 0;
  1939. self.scroll.x = (self.getScrollLeft() / self.scrollratio.x) | 0;
  1940. if (self.cursoractive) self.noticeCursor();
  1941. }
  1942. if (self.scroll.y && (self.getScrollTop() === 0)) self.doScrollTo((self.scroll.y * self.scrollratio.y)|0);
  1943. return self;
  1944. };
  1945. this.resize = self.onResize;
  1946. var hlazyresize = 0;
  1947. this.onscreenresize = function(e) {
  1948. clearTimeout(hlazyresize);
  1949. var hiderails = (!self.ispage && !self.haswrapper);
  1950. if (hiderails) self.hideRails();
  1951. hlazyresize = setTimeout(function () {
  1952. if (self) {
  1953. if (hiderails) self.showRails();
  1954. self.resize();
  1955. }
  1956. hlazyresize=0;
  1957. }, 120);
  1958. };
  1959. this.lazyResize = function (tm) { // event debounce
  1960. clearTimeout(hlazyresize);
  1961. tm = isNaN(tm) ? 240 : tm;
  1962. hlazyresize = setTimeout(function () {
  1963. self && self.resize();
  1964. hlazyresize=0;
  1965. }, tm);
  1966. return self;
  1967. };
  1968. // derived by MDN https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/wheel
  1969. function _modernWheelEvent(dom, name, fn, bubble) {
  1970. self._bind(dom, name, function (e) {
  1971. e = e || _win.event;
  1972. var event = {
  1973. original: e,
  1974. target: e.target || e.srcElement,
  1975. type: "wheel",
  1976. deltaMode: e.type == "MozMousePixelScroll" ? 0 : 1,
  1977. deltaX: 0,
  1978. deltaZ: 0,
  1979. preventDefault: function () {
  1980. e.preventDefault ? e.preventDefault() : e.returnValue = false;
  1981. return false;
  1982. },
  1983. stopImmediatePropagation: function () {
  1984. (e.stopImmediatePropagation) ? e.stopImmediatePropagation() : e.cancelBubble = true;
  1985. }
  1986. };
  1987. if (name == "mousewheel") {
  1988. e.wheelDeltaX && (event.deltaX = -1 / 40 * e.wheelDeltaX);
  1989. e.wheelDeltaY && (event.deltaY = -1 / 40 * e.wheelDeltaY);
  1990. !event.deltaY && !event.deltaX && (event.deltaY = -1 / 40 * e.wheelDelta);
  1991. } else {
  1992. event.deltaY = e.detail;
  1993. }
  1994. return fn.call(dom, event);
  1995. }, bubble);
  1996. }
  1997. this.jqbind = function (dom, name, fn) { // use jquery bind for non-native events (mouseenter/mouseleave)
  1998. self.events.push({
  1999. e: dom,
  2000. n: name,
  2001. f: fn,
  2002. q: true
  2003. });
  2004. $(dom).on(name, fn);
  2005. };
  2006. this.mousewheel = function (dom, fn, bubble) { // bind mousewheel
  2007. var el = ("jquery" in dom) ? dom[0] : dom;
  2008. if ("onwheel" in _doc.createElement("div")) { // Modern browsers support "wheel"
  2009. self._bind(el, "wheel", fn, bubble || false);
  2010. } else {
  2011. var wname = (_doc.onmousewheel !== undefined) ? "mousewheel" : "DOMMouseScroll"; // older Webkit+IE support or older Firefox
  2012. _modernWheelEvent(el, wname, fn, bubble || false);
  2013. if (wname == "DOMMouseScroll") _modernWheelEvent(el, "MozMousePixelScroll", fn, bubble || false); // Firefox legacy
  2014. }
  2015. };
  2016. var passiveSupported = false;
  2017. if (cap.haseventlistener) { // W3C standard event model
  2018. // thanks to https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
  2019. try { var options = Object.defineProperty({}, "passive", { get: function () { passiveSupported = !0; } }); _win.addEventListener("test", null, options); } catch (err) { }
  2020. this.stopPropagation = function (e) {
  2021. if (!e) return false;
  2022. e = (e.original) ? e.original : e;
  2023. e.stopPropagation();
  2024. return false;
  2025. };
  2026. this.cancelEvent = function(e) {
  2027. if (e.cancelable) e.preventDefault();
  2028. e.stopImmediatePropagation();
  2029. if (e.preventManipulation) e.preventManipulation(); // IE10+
  2030. return false;
  2031. };
  2032. } else {
  2033. // inspired from https://gist.github.com/jonathantneal/2415137
  2034. Event.prototype.preventDefault = function () {
  2035. this.returnValue = false;
  2036. };
  2037. Event.prototype.stopPropagation = function () {
  2038. this.cancelBubble = true;
  2039. };
  2040. _win.constructor.prototype.addEventListener = _doc.constructor.prototype.addEventListener = Element.prototype.addEventListener = function (type, listener, useCapture) {
  2041. this.attachEvent("on" + type, listener);
  2042. };
  2043. _win.constructor.prototype.removeEventListener = _doc.constructor.prototype.removeEventListener = Element.prototype.removeEventListener = function (type, listener, useCapture) {
  2044. this.detachEvent("on" + type, listener);
  2045. };
  2046. // Thanks to http://www.switchonthecode.com !!
  2047. this.cancelEvent = function (e) {
  2048. e = e || _win.event;
  2049. if (e) {
  2050. e.cancelBubble = true;
  2051. e.cancel = true;
  2052. e.returnValue = false;
  2053. }
  2054. return false;
  2055. };
  2056. this.stopPropagation = function (e) {
  2057. e = e || _win.event;
  2058. if (e) e.cancelBubble = true;
  2059. return false;
  2060. };
  2061. }
  2062. this.delegate = function (dom, name, fn, bubble, active) {
  2063. var de = delegatevents[name] || false;
  2064. if (!de) {
  2065. de = {
  2066. a: [],
  2067. l: [],
  2068. f: function (e) {
  2069. var lst = de.l, l = lst.length - 1;
  2070. var r = false;
  2071. for (var a = l; a >= 0; a--) {
  2072. r = lst[a].call(e.target, e);
  2073. if (r === false) return false;
  2074. }
  2075. return r;
  2076. }
  2077. };
  2078. self.bind(dom, name, de.f, bubble, active);
  2079. delegatevents[name] = de;
  2080. } else {
  2081. self.events.push({
  2082. e: dom,
  2083. n: name,
  2084. f: fn,
  2085. b: bubble,
  2086. q: false
  2087. });
  2088. }
  2089. if (self.ispage) {
  2090. de.a = [self.id].concat(de.a);
  2091. de.l = [fn].concat(de.l);
  2092. } else {
  2093. de.a.push(self.id);
  2094. de.l.push(fn);
  2095. }
  2096. };
  2097. this.undelegate = function (dom, name, fn, bubble, active) {
  2098. var de = delegatevents[name]||false;
  2099. if (de&&de.l) { // quick fix #683
  2100. for (var a=0,l=de.l.length;a<l;a++) {
  2101. if (de.a[a] === self.id) {
  2102. de.a.splice(a);
  2103. de.l.splice(a);
  2104. if (de.a.length===0) {
  2105. self._unbind(dom,name,de.l.f);
  2106. delegatevents[name] = null;
  2107. }
  2108. }
  2109. }
  2110. }
  2111. };
  2112. this.bind = function (dom, name, fn, bubble, active) {
  2113. var el = ("jquery" in dom) ? dom[0] : dom;
  2114. self._bind(el, name, fn, bubble || false, active || false);
  2115. };
  2116. this._bind = function (el, name, fn, bubble, active) { // primitive bind
  2117. self.events.push({
  2118. e: el,
  2119. n: name,
  2120. f: fn,
  2121. b: bubble,
  2122. q: false
  2123. });
  2124. (passiveSupported && (active || el == window.document || el == window.document.body || el == window)) ? el.addEventListener(name, fn, { passive: false, capture: bubble }) : el.addEventListener(name, fn, bubble || false);
  2125. };
  2126. this._unbind = function (el, name, fn, bub) { // primitive unbind
  2127. if (delegatevents[name]) self.undelegate(el, name, fn, bub);
  2128. else el.removeEventListener(name, fn, bub);
  2129. };
  2130. this.unbindAll = function () {
  2131. for (var a = 0; a < self.events.length; a++) {
  2132. var r = self.events[a];
  2133. (r.q) ? r.e.unbind(r.n, r.f) : self._unbind(r.e, r.n, r.f, r.b);
  2134. }
  2135. };
  2136. this.showRails = function () {
  2137. return self.showRail().showRailHr();
  2138. };
  2139. this.showRail = function () {
  2140. if ((self.page.maxh !== 0) && (self.ispage || self.win.css('display') != 'none')) {
  2141. //self.visibility = true;
  2142. self.rail.visibility = true;
  2143. self.rail.css('display', 'block');
  2144. }
  2145. return self;
  2146. };
  2147. this.showRailHr = function () {
  2148. if (self.railh) {
  2149. if ((self.page.maxw !== 0) && (self.ispage || self.win.css('display') != 'none')) {
  2150. self.railh.visibility = true;
  2151. self.railh.css('display', 'block');
  2152. }
  2153. }
  2154. return self;
  2155. };
  2156. this.hideRails = function () {
  2157. return self.hideRail().hideRailHr();
  2158. };
  2159. this.hideRail = function () {
  2160. //self.visibility = false;
  2161. self.rail.visibility = false;
  2162. self.rail.css('display', 'none');
  2163. return self;
  2164. };
  2165. this.hideRailHr = function () {
  2166. if (self.railh) {
  2167. self.railh.visibility = false;
  2168. self.railh.css('display', 'none');
  2169. }
  2170. return self;
  2171. };
  2172. this.show = function () {
  2173. self.hidden = false;
  2174. self.railslocked = false;
  2175. return self.showRails();
  2176. };
  2177. this.hide = function () {
  2178. self.hidden = true;
  2179. self.railslocked = true;
  2180. return self.hideRails();
  2181. };
  2182. this.toggle = function () {
  2183. return (self.hidden) ? self.show() : self.hide();
  2184. };
  2185. this.remove = function () {
  2186. self.stop();
  2187. if (self.cursortimeout) clearTimeout(self.cursortimeout);
  2188. for (var n in self.delaylist) if (self.delaylist[n]) clearAnimationFrame(self.delaylist[n].h);
  2189. self.doZoomOut();
  2190. if (cap.hasw3ctouch) { //IE11+
  2191. self.undelegate(_doc, "pointermove", self.ontouchmove);
  2192. } else if (cap.hasmstouch) { //IE10
  2193. self.undelegate(_doc, "MSPointerMove", self.ontouchmove);
  2194. } else if (cap.cantouch) { // smartphones/touch devices
  2195. self.undelegate(_doc, "touchmove", self.ontouchmove, false, true);
  2196. }
  2197. self.unbindAll();
  2198. if (cap.isie9) self.win[0].detachEvent("onpropertychange", self.onAttributeChange); //IE9 DOMAttrModified bug
  2199. if (self.observer !== false) self.observer.disconnect();
  2200. if (self.observerremover !== false) self.observerremover.disconnect();
  2201. if (self.observerbody !== false) self.observerbody.disconnect();
  2202. self.events = null;
  2203. if (self.cursor) {
  2204. self.cursor.remove();
  2205. }
  2206. if (self.cursorh) {
  2207. self.cursorh.remove();
  2208. }
  2209. if (self.rail) {
  2210. self.rail.remove();
  2211. }
  2212. if (self.railh) {
  2213. self.railh.remove();
  2214. }
  2215. if (self.zoom) {
  2216. self.zoom.remove();
  2217. }
  2218. for (var a = 0; a < self.saved.css.length; a++) {
  2219. var d = self.saved.css[a];
  2220. d[0].css(d[1], (d[2] === undefined) ? '' : d[2]);
  2221. }
  2222. self.saved = false;
  2223. self.me.data('__nicescroll', ''); //erase all traces
  2224. // memory leak fixed by GianlucaGuarini - thanks a lot!
  2225. // remove the current nicescroll from the $.nicescroll array & normalize array
  2226. var lst = $.nicescroll;
  2227. lst.each(function (i) {
  2228. if (!this) return;
  2229. if (this.id === self.id) {
  2230. delete lst[i];
  2231. for (var b = ++i; b < lst.length; b++ , i++) lst[i] = lst[b];
  2232. lst.length--;
  2233. if (lst.length) delete lst[lst.length];
  2234. }
  2235. });
  2236. for (var i in self) {
  2237. self[i] = null;
  2238. delete self[i];
  2239. }
  2240. self = null;
  2241. };
  2242. this.scrollstart = function (fn) {
  2243. this.onscrollstart = fn;
  2244. return self;
  2245. };
  2246. this.scrollend = function (fn) {
  2247. this.onscrollend = fn;
  2248. return self;
  2249. };
  2250. this.scrollcancel = function (fn) {
  2251. this.onscrollcancel = fn;
  2252. return self;
  2253. };
  2254. this.zoomin = function (fn) {
  2255. this.onzoomin = fn;
  2256. return self;
  2257. };
  2258. this.zoomout = function (fn) {
  2259. this.onzoomout = fn;
  2260. return self;
  2261. };
  2262. this.isScrollable = function (e) {
  2263. var dom = (e.target) ? e.target : e;
  2264. if (dom.nodeName == 'OPTION') return true;
  2265. while (dom && (dom.nodeType == 1) && (dom !== this.me[0]) && !(/^BODY|HTML/.test(dom.nodeName))) {
  2266. var dd = $(dom);
  2267. var ov = dd.css('overflowY') || dd.css('overflowX') || dd.css('overflow') || '';
  2268. if (/scroll|auto/.test(ov)) return (dom.clientHeight != dom.scrollHeight);
  2269. dom = (dom.parentNode) ? dom.parentNode : false;
  2270. }
  2271. return false;
  2272. };
  2273. this.getViewport = function (me) {
  2274. var dom = (me && me.parentNode) ? me.parentNode : false;
  2275. while (dom && (dom.nodeType == 1) && !(/^BODY|HTML/.test(dom.nodeName))) {
  2276. var dd = $(dom);
  2277. if (/fixed|absolute/.test(dd.css("position"))) return dd;
  2278. var ov = dd.css('overflowY') || dd.css('overflowX') || dd.css('overflow') || '';
  2279. if ((/scroll|auto/.test(ov)) && (dom.clientHeight != dom.scrollHeight)) return dd;
  2280. if (dd.getNiceScroll().length > 0) return dd;
  2281. dom = (dom.parentNode) ? dom.parentNode : false;
  2282. }
  2283. return false;
  2284. };
  2285. this.triggerScrollStart = function (cx, cy, rx, ry, ms) {
  2286. if (self.onscrollstart) {
  2287. var info = {
  2288. type: "scrollstart",
  2289. current: {
  2290. x: cx,
  2291. y: cy
  2292. },
  2293. request: {
  2294. x: rx,
  2295. y: ry
  2296. },
  2297. end: {
  2298. x: self.newscrollx,
  2299. y: self.newscrolly
  2300. },
  2301. speed: ms
  2302. };
  2303. self.onscrollstart.call(self, info);
  2304. }
  2305. };
  2306. this.triggerScrollEnd = function () {
  2307. if (self.onscrollend) {
  2308. var px = self.getScrollLeft();
  2309. var py = self.getScrollTop();
  2310. var info = {
  2311. type: "scrollend",
  2312. current: {
  2313. x: px,
  2314. y: py
  2315. },
  2316. end: {
  2317. x: px,
  2318. y: py
  2319. }
  2320. };
  2321. self.onscrollend.call(self, info);
  2322. }
  2323. };
  2324. var scrolldiry = 0, scrolldirx = 0, scrolltmr = 0, scrollspd = 1;
  2325. function doScrollRelative(px, py, chkscroll, iswheel) {
  2326. if (!self.scrollrunning) {
  2327. self.newscrolly = self.getScrollTop();
  2328. self.newscrollx = self.getScrollLeft();
  2329. scrolltmr = now();
  2330. }
  2331. var gap = (now() - scrolltmr);
  2332. scrolltmr = now();
  2333. if (gap > 350) {
  2334. scrollspd = 1;
  2335. } else {
  2336. scrollspd += (2 - scrollspd) / 10;
  2337. }
  2338. px = px * scrollspd | 0;
  2339. py = py * scrollspd | 0;
  2340. if (px) {
  2341. if (iswheel) { // mouse-only
  2342. if (px < 0) { // fix apple magic mouse swipe back/forward
  2343. if (self.getScrollLeft() >= self.page.maxw) return true;
  2344. } else {
  2345. if (self.getScrollLeft() <= 0) return true;
  2346. }
  2347. }
  2348. var dx = px > 0 ? 1 : -1;
  2349. if (scrolldirx !== dx) {
  2350. if (self.scrollmom) self.scrollmom.stop();
  2351. self.newscrollx = self.getScrollLeft();
  2352. scrolldirx = dx;
  2353. }
  2354. self.lastdeltax -= px;
  2355. }
  2356. if (py) {
  2357. var chk = (function () {
  2358. var top = self.getScrollTop();
  2359. if (py < 0) {
  2360. if (top >= self.page.maxh) return true;
  2361. } else {
  2362. if (top <= 0) return true;
  2363. }
  2364. })();
  2365. if (chk) {
  2366. if (opt.nativeparentscrolling && chkscroll && !self.ispage && !self.zoomactive) return true;
  2367. var ny = self.view.h >> 1;
  2368. if (self.newscrolly < -ny) { self.newscrolly = -ny; py = -1; }
  2369. else if (self.newscrolly > self.page.maxh + ny) { self.newscrolly = self.page.maxh + ny; py = 1; }
  2370. else py = 0;
  2371. }
  2372. var dy = py > 0 ? 1 : -1;
  2373. if (scrolldiry !== dy) {
  2374. if (self.scrollmom) self.scrollmom.stop();
  2375. self.newscrolly = self.getScrollTop();
  2376. scrolldiry = dy;
  2377. }
  2378. self.lastdeltay -= py;
  2379. }
  2380. if (py || px) {
  2381. self.synched("relativexy", function () {
  2382. var dty = self.lastdeltay + self.newscrolly;
  2383. self.lastdeltay = 0;
  2384. var dtx = self.lastdeltax + self.newscrollx;
  2385. self.lastdeltax = 0;
  2386. if (!self.rail.drag) self.doScrollPos(dtx, dty);
  2387. });
  2388. }
  2389. }
  2390. var hasparentscrollingphase = false;
  2391. function execScrollWheel(e, hr, chkscroll) {
  2392. var px, py;
  2393. if (!chkscroll && hasparentscrollingphase) return true;
  2394. if (e.deltaMode === 0) { // PIXEL
  2395. px = -(e.deltaX * (opt.mousescrollstep / (18 * 3))) | 0;
  2396. py = -(e.deltaY * (opt.mousescrollstep / (18 * 3))) | 0;
  2397. } else if (e.deltaMode === 1) { // LINE
  2398. px = -(e.deltaX * opt.mousescrollstep * 50 / 80) | 0;
  2399. py = -(e.deltaY * opt.mousescrollstep * 50 / 80) | 0;
  2400. }
  2401. if (hr && opt.oneaxismousemode && (px === 0) && py) { // classic vertical-only mousewheel + browser with x/y support
  2402. px = py;
  2403. py = 0;
  2404. if (chkscroll) {
  2405. var hrend = (px < 0) ? (self.getScrollLeft() >= self.page.maxw) : (self.getScrollLeft() <= 0);
  2406. if (hrend) { // preserve vertical scrolling
  2407. py = px;
  2408. px = 0;
  2409. }
  2410. }
  2411. }
  2412. // invert horizontal direction for rtl mode
  2413. if (self.isrtlmode) px = -px;
  2414. var chk = doScrollRelative(px, py, chkscroll, true);
  2415. if (chk) {
  2416. if (chkscroll) hasparentscrollingphase = true;
  2417. } else {
  2418. hasparentscrollingphase = false;
  2419. e.stopImmediatePropagation();
  2420. return e.preventDefault();
  2421. }
  2422. }
  2423. this.onmousewheel = function (e) {
  2424. if (self.wheelprevented||self.locked) return false;
  2425. if (self.railslocked) {
  2426. self.debounced("checkunlock", self.resize, 250);
  2427. return false;
  2428. }
  2429. if (self.rail.drag) return self.cancelEvent(e);
  2430. if (opt.oneaxismousemode === "auto" && e.deltaX !== 0) opt.oneaxismousemode = false; // check two-axis mouse support (not very elegant)
  2431. if (opt.oneaxismousemode && e.deltaX === 0) {
  2432. if (!self.rail.scrollable) {
  2433. if (self.railh && self.railh.scrollable) {
  2434. return self.onmousewheelhr(e);
  2435. } else {
  2436. return true;
  2437. }
  2438. }
  2439. }
  2440. var nw = now();
  2441. var chk = false;
  2442. if (opt.preservenativescrolling && ((self.checkarea + 600) < nw)) {
  2443. self.nativescrollingarea = self.isScrollable(e);
  2444. chk = true;
  2445. }
  2446. self.checkarea = nw;
  2447. if (self.nativescrollingarea) return true; // this isn't my business
  2448. var ret = execScrollWheel(e, false, chk);
  2449. if (ret) self.checkarea = 0;
  2450. return ret;
  2451. };
  2452. this.onmousewheelhr = function (e) {
  2453. if (self.wheelprevented) return;
  2454. if (self.railslocked || !self.railh.scrollable) return true;
  2455. if (self.rail.drag) return self.cancelEvent(e);
  2456. var nw = now();
  2457. var chk = false;
  2458. if (opt.preservenativescrolling && ((self.checkarea + 600) < nw)) {
  2459. self.nativescrollingarea = self.isScrollable(e);
  2460. chk = true;
  2461. }
  2462. self.checkarea = nw;
  2463. if (self.nativescrollingarea) return true; // this is not my business
  2464. if (self.railslocked) return self.cancelEvent(e);
  2465. return execScrollWheel(e, true, chk);
  2466. };
  2467. this.stop = function () {
  2468. self.cancelScroll();
  2469. if (self.scrollmon) self.scrollmon.stop();
  2470. self.cursorfreezed = false;
  2471. self.scroll.y = Math.round(self.getScrollTop() * (1 / self.scrollratio.y));
  2472. self.noticeCursor();
  2473. return self;
  2474. };
  2475. this.getTransitionSpeed = function (dif) {
  2476. return 80 + (dif / 72) * opt.scrollspeed |0;
  2477. };
  2478. if (!opt.smoothscroll) {
  2479. this.doScrollLeft = function (x, spd) { //direct
  2480. var y = self.getScrollTop();
  2481. self.doScrollPos(x, y, spd);
  2482. };
  2483. this.doScrollTop = function (y, spd) { //direct
  2484. var x = self.getScrollLeft();
  2485. self.doScrollPos(x, y, spd);
  2486. };
  2487. this.doScrollPos = function (x, y, spd) { //direct
  2488. var nx = (x > self.page.maxw) ? self.page.maxw : x;
  2489. if (nx < 0) nx = 0;
  2490. var ny = (y > self.page.maxh) ? self.page.maxh : y;
  2491. if (ny < 0) ny = 0;
  2492. self.synched('scroll', function () {
  2493. self.setScrollTop(ny);
  2494. self.setScrollLeft(nx);
  2495. });
  2496. };
  2497. this.cancelScroll = function () { }; // direct
  2498. } else if (self.ishwscroll && cap.hastransition && opt.usetransition && !!opt.smoothscroll) {
  2499. var lasttransitionstyle = '';
  2500. this.resetTransition = function () {
  2501. lasttransitionstyle = '';
  2502. self.doc.css(cap.prefixstyle + 'transition-duration', '0ms');
  2503. };
  2504. this.prepareTransition = function (dif, istime) {
  2505. var ex = (istime) ? dif : self.getTransitionSpeed(dif);
  2506. var trans = ex + 'ms';
  2507. if (lasttransitionstyle !== trans) {
  2508. lasttransitionstyle = trans;
  2509. self.doc.css(cap.prefixstyle + 'transition-duration', trans);
  2510. }
  2511. return ex;
  2512. };
  2513. this.doScrollLeft = function (x, spd) { //trans
  2514. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  2515. self.doScrollPos(x, y, spd);
  2516. };
  2517. this.doScrollTop = function (y, spd) { //trans
  2518. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  2519. self.doScrollPos(x, y, spd);
  2520. };
  2521. this.cursorupdate = {
  2522. running: false,
  2523. start: function () {
  2524. var m = this;
  2525. if (m.running) return;
  2526. m.running = true;
  2527. var loop = function () {
  2528. if (m.running) setAnimationFrame(loop);
  2529. self.showCursor(self.getScrollTop(), self.getScrollLeft());
  2530. self.notifyScrollEvent(self.win[0]);
  2531. };
  2532. setAnimationFrame(loop);
  2533. },
  2534. stop: function () {
  2535. this.running = false;
  2536. }
  2537. };
  2538. this.doScrollPos = function (x, y, spd) { //trans
  2539. var py = self.getScrollTop();
  2540. var px = self.getScrollLeft();
  2541. if (((self.newscrolly - py) * (y - py) < 0) || ((self.newscrollx - px) * (x - px) < 0)) self.cancelScroll(); //inverted movement detection
  2542. if (!opt.bouncescroll) {
  2543. if (y < 0) y = 0;
  2544. else if (y > self.page.maxh) y = self.page.maxh;
  2545. if (x < 0) x = 0;
  2546. else if (x > self.page.maxw) x = self.page.maxw;
  2547. } else {
  2548. if (y < 0) y = y / 2 | 0;
  2549. else if (y > self.page.maxh) y = self.page.maxh + (y - self.page.maxh) / 2 | 0;
  2550. if (x < 0) x = x / 2 | 0;
  2551. else if (x > self.page.maxw) x = self.page.maxw + (x - self.page.maxw) / 2 | 0;
  2552. }
  2553. if (self.scrollrunning && x == self.newscrollx && y == self.newscrolly) return false;
  2554. self.newscrolly = y;
  2555. self.newscrollx = x;
  2556. var top = self.getScrollTop();
  2557. var lft = self.getScrollLeft();
  2558. var dst = {};
  2559. dst.x = x - lft;
  2560. dst.y = y - top;
  2561. var dd = Math.sqrt((dst.x * dst.x) + (dst.y * dst.y)) | 0;
  2562. var ms = self.prepareTransition(dd);
  2563. if (!self.scrollrunning) {
  2564. self.scrollrunning = true;
  2565. self.triggerScrollStart(lft, top, x, y, ms);
  2566. self.cursorupdate.start();
  2567. }
  2568. self.scrollendtrapped = true;
  2569. if (!cap.transitionend) {
  2570. if (self.scrollendtrapped) clearTimeout(self.scrollendtrapped);
  2571. self.scrollendtrapped = setTimeout(self.onScrollTransitionEnd, ms); // simulate transitionend event
  2572. }
  2573. self.setScrollTop(self.newscrolly);
  2574. self.setScrollLeft(self.newscrollx);
  2575. };
  2576. this.cancelScroll = function () {
  2577. if (!self.scrollendtrapped) return true;
  2578. var py = self.getScrollTop();
  2579. var px = self.getScrollLeft();
  2580. self.scrollrunning = false;
  2581. if (!cap.transitionend) clearTimeout(cap.transitionend);
  2582. self.scrollendtrapped = false;
  2583. self.resetTransition();
  2584. self.setScrollTop(py); // fire event onscroll
  2585. if (self.railh) self.setScrollLeft(px);
  2586. if (self.timerscroll && self.timerscroll.tm) clearInterval(self.timerscroll.tm);
  2587. self.timerscroll = false;
  2588. self.cursorfreezed = false;
  2589. self.cursorupdate.stop();
  2590. self.showCursor(py, px);
  2591. return self;
  2592. };
  2593. this.onScrollTransitionEnd = function () {
  2594. if (!self.scrollendtrapped) return;
  2595. var py = self.getScrollTop();
  2596. var px = self.getScrollLeft();
  2597. if (py < 0) py = 0;
  2598. else if (py > self.page.maxh) py = self.page.maxh;
  2599. if (px < 0) px = 0;
  2600. else if (px > self.page.maxw) px = self.page.maxw;
  2601. if ((py != self.newscrolly) || (px != self.newscrollx)) return self.doScrollPos(px, py, opt.snapbackspeed);
  2602. if (self.scrollrunning) self.triggerScrollEnd();
  2603. self.scrollrunning = false;
  2604. self.scrollendtrapped = false;
  2605. self.resetTransition();
  2606. self.timerscroll = false;
  2607. self.setScrollTop(py); // fire event onscroll
  2608. if (self.railh) self.setScrollLeft(px); // fire event onscroll left
  2609. self.cursorupdate.stop();
  2610. self.noticeCursor(false, py, px);
  2611. self.cursorfreezed = false;
  2612. };
  2613. } else {
  2614. this.doScrollLeft = function (x, spd) { //no-trans
  2615. var y = (self.scrollrunning) ? self.newscrolly : self.getScrollTop();
  2616. self.doScrollPos(x, y, spd);
  2617. };
  2618. this.doScrollTop = function (y, spd) { //no-trans
  2619. var x = (self.scrollrunning) ? self.newscrollx : self.getScrollLeft();
  2620. self.doScrollPos(x, y, spd);
  2621. };
  2622. this.doScrollPos = function (x, y, spd) { //no-trans
  2623. var py = self.getScrollTop();
  2624. var px = self.getScrollLeft();
  2625. if (((self.newscrolly - py) * (y - py) < 0) || ((self.newscrollx - px) * (x - px) < 0)) self.cancelScroll(); //inverted movement detection
  2626. var clipped = false;
  2627. if (!self.bouncescroll || !self.rail.visibility) {
  2628. if (y < 0) {
  2629. y = 0;
  2630. clipped = true;
  2631. } else if (y > self.page.maxh) {
  2632. y = self.page.maxh;
  2633. clipped = true;
  2634. }
  2635. }
  2636. if (!self.bouncescroll || !self.railh.visibility) {
  2637. if (x < 0) {
  2638. x = 0;
  2639. clipped = true;
  2640. } else if (x > self.page.maxw) {
  2641. x = self.page.maxw;
  2642. clipped = true;
  2643. }
  2644. }
  2645. if (self.scrollrunning && (self.newscrolly === y) && (self.newscrollx === x)) return true;
  2646. self.newscrolly = y;
  2647. self.newscrollx = x;
  2648. self.dst = {};
  2649. self.dst.x = x - px;
  2650. self.dst.y = y - py;
  2651. self.dst.px = px;
  2652. self.dst.py = py;
  2653. var dd = Math.sqrt((self.dst.x * self.dst.x) + (self.dst.y * self.dst.y)) | 0;
  2654. var ms = self.getTransitionSpeed(dd);
  2655. self.bzscroll = {};
  2656. var p3 = (clipped) ? 1 : 0.58;
  2657. self.bzscroll.x = new BezierClass(px, self.newscrollx, ms, 0, 0, p3, 1);
  2658. self.bzscroll.y = new BezierClass(py, self.newscrolly, ms, 0, 0, p3, 1);
  2659. var loopid = now();
  2660. var loop = function () {
  2661. if (!self.scrollrunning) return;
  2662. var x = self.bzscroll.y.getPos();
  2663. self.setScrollLeft(self.bzscroll.x.getNow());
  2664. self.setScrollTop(self.bzscroll.y.getNow());
  2665. if (x <= 1) {
  2666. self.timer = setAnimationFrame(loop);
  2667. } else {
  2668. self.scrollrunning = false;
  2669. self.timer = 0;
  2670. self.triggerScrollEnd();
  2671. }
  2672. };
  2673. if (!self.scrollrunning) {
  2674. self.triggerScrollStart(px, py, x, y, ms);
  2675. self.scrollrunning = true;
  2676. self.timer = setAnimationFrame(loop);
  2677. }
  2678. };
  2679. this.cancelScroll = function () {
  2680. if (self.timer) clearAnimationFrame(self.timer);
  2681. self.timer = 0;
  2682. self.bzscroll = false;
  2683. self.scrollrunning = false;
  2684. return self;
  2685. };
  2686. }
  2687. this.doScrollBy = function (stp, relative) {
  2688. doScrollRelative(0, stp);
  2689. };
  2690. this.doScrollLeftBy = function (stp, relative) {
  2691. doScrollRelative(stp, 0);
  2692. };
  2693. this.doScrollTo = function (pos, relative) {
  2694. var ny = (relative) ? Math.round(pos * self.scrollratio.y) : pos;
  2695. if (ny < 0) ny = 0;
  2696. else if (ny > self.page.maxh) ny = self.page.maxh;
  2697. self.cursorfreezed = false;
  2698. self.doScrollTop(pos);
  2699. };
  2700. this.checkContentSize = function () {
  2701. var pg = self.getContentSize();
  2702. if ((pg.h != self.page.h) || (pg.w != self.page.w)) self.resize(false, pg);
  2703. };
  2704. self.onscroll = function (e) {
  2705. if (self.rail.drag) return;
  2706. if (!self.cursorfreezed) {
  2707. self.synched('scroll', function () {
  2708. self.scroll.y = Math.round(self.getScrollTop() / self.scrollratio.y);
  2709. if (self.railh) self.scroll.x = Math.round(self.getScrollLeft() / self.scrollratio.x);
  2710. self.noticeCursor();
  2711. });
  2712. }
  2713. };
  2714. self.bind(self.docscroll, "scroll", self.onscroll);
  2715. this.doZoomIn = function (e) {
  2716. if (self.zoomactive) return;
  2717. self.zoomactive = true;
  2718. self.zoomrestore = {
  2719. style: {}
  2720. };
  2721. var lst = ['position', 'top', 'left', 'zIndex', 'backgroundColor', 'marginTop', 'marginBottom', 'marginLeft', 'marginRight'];
  2722. var win = self.win[0].style;
  2723. for (var a in lst) {
  2724. var pp = lst[a];
  2725. self.zoomrestore.style[pp] = (win[pp] !== undefined) ? win[pp] : '';
  2726. }
  2727. self.zoomrestore.style.width = self.win.css('width');
  2728. self.zoomrestore.style.height = self.win.css('height');
  2729. self.zoomrestore.padding = {
  2730. w: self.win.outerWidth() - self.win.width(),
  2731. h: self.win.outerHeight() - self.win.height()
  2732. };
  2733. if (cap.isios4) {
  2734. self.zoomrestore.scrollTop = $window.scrollTop();
  2735. $window.scrollTop(0);
  2736. }
  2737. self.win.css({
  2738. position: (cap.isios4) ? "absolute" : "fixed",
  2739. top: 0,
  2740. left: 0,
  2741. zIndex: globalmaxzindex + 100,
  2742. margin: 0
  2743. });
  2744. var bkg = self.win.css("backgroundColor");
  2745. if ("" === bkg || /transparent|rgba\(0, 0, 0, 0\)|rgba\(0,0,0,0\)/.test(bkg)) self.win.css("backgroundColor", "#fff");
  2746. self.rail.css({
  2747. zIndex: globalmaxzindex + 101
  2748. });
  2749. self.zoom.css({
  2750. zIndex: globalmaxzindex + 102
  2751. });
  2752. self.zoom.css('backgroundPosition', '0 -18px');
  2753. self.resizeZoom();
  2754. if (self.onzoomin) self.onzoomin.call(self);
  2755. return self.cancelEvent(e);
  2756. };
  2757. this.doZoomOut = function (e) {
  2758. if (!self.zoomactive) return;
  2759. self.zoomactive = false;
  2760. self.win.css("margin", "");
  2761. self.win.css(self.zoomrestore.style);
  2762. if (cap.isios4) {
  2763. $window.scrollTop(self.zoomrestore.scrollTop);
  2764. }
  2765. self.rail.css({
  2766. "z-index": self.zindex
  2767. });
  2768. self.zoom.css({
  2769. "z-index": self.zindex
  2770. });
  2771. self.zoomrestore = false;
  2772. self.zoom.css('backgroundPosition', '0 0');
  2773. self.onResize();
  2774. if (self.onzoomout) self.onzoomout.call(self);
  2775. return self.cancelEvent(e);
  2776. };
  2777. this.doZoom = function (e) {
  2778. return (self.zoomactive) ? self.doZoomOut(e) : self.doZoomIn(e);
  2779. };
  2780. this.resizeZoom = function () {
  2781. if (!self.zoomactive) return;
  2782. var py = self.getScrollTop(); //preserve scrolling position
  2783. self.win.css({
  2784. width: $window.width() - self.zoomrestore.padding.w + "px",
  2785. height: $window.height() - self.zoomrestore.padding.h + "px"
  2786. });
  2787. self.onResize();
  2788. self.setScrollTop(Math.min(self.page.maxh, py));
  2789. };
  2790. this.init();
  2791. $.nicescroll.push(this);
  2792. };
  2793. // Inspired by the work of Kin Blas
  2794. // http://webpro.host.adobe.com/people/jblas/momentum/includes/jquery.momentum.0.7.js
  2795. var ScrollMomentumClass2D = function (nc) {
  2796. var self = this;
  2797. this.nc = nc;
  2798. this.lastx = 0;
  2799. this.lasty = 0;
  2800. this.speedx = 0;
  2801. this.speedy = 0;
  2802. this.lasttime = 0;
  2803. this.steptime = 0;
  2804. this.snapx = false;
  2805. this.snapy = false;
  2806. this.demulx = 0;
  2807. this.demuly = 0;
  2808. this.lastscrollx = -1;
  2809. this.lastscrolly = -1;
  2810. this.chkx = 0;
  2811. this.chky = 0;
  2812. this.timer = 0;
  2813. this.reset = function (px, py) {
  2814. self.stop();
  2815. self.steptime = 0;
  2816. self.lasttime = now();
  2817. self.speedx = 0;
  2818. self.speedy = 0;
  2819. self.lastx = px;
  2820. self.lasty = py;
  2821. self.lastscrollx = -1;
  2822. self.lastscrolly = -1;
  2823. };
  2824. this.update = function (px, py) {
  2825. var tm = now();
  2826. self.steptime = tm - self.lasttime;
  2827. self.lasttime = tm;
  2828. var dy = py - self.lasty;
  2829. var dx = px - self.lastx;
  2830. var sy = self.nc.getScrollTop();
  2831. var sx = self.nc.getScrollLeft();
  2832. var newy = sy + dy;
  2833. var newx = sx + dx;
  2834. self.snapx = (newx < 0) || (newx > self.nc.page.maxw);
  2835. self.snapy = (newy < 0) || (newy > self.nc.page.maxh);
  2836. self.speedx = dx;
  2837. self.speedy = dy;
  2838. self.lastx = px;
  2839. self.lasty = py;
  2840. };
  2841. this.stop = function () {
  2842. self.nc.unsynched("domomentum2d");
  2843. if (self.timer) clearTimeout(self.timer);
  2844. self.timer = 0;
  2845. self.lastscrollx = -1;
  2846. self.lastscrolly = -1;
  2847. };
  2848. this.doSnapy = function (nx, ny) {
  2849. var snap = false;
  2850. if (ny < 0) {
  2851. ny = 0;
  2852. snap = true;
  2853. } else if (ny > self.nc.page.maxh) {
  2854. ny = self.nc.page.maxh;
  2855. snap = true;
  2856. }
  2857. if (nx < 0) {
  2858. nx = 0;
  2859. snap = true;
  2860. } else if (nx > self.nc.page.maxw) {
  2861. nx = self.nc.page.maxw;
  2862. snap = true;
  2863. }
  2864. (snap) ? self.nc.doScrollPos(nx, ny, self.nc.opt.snapbackspeed) : self.nc.triggerScrollEnd();
  2865. };
  2866. this.doMomentum = function (gp) {
  2867. var t = now();
  2868. var l = (gp) ? t + gp : self.lasttime;
  2869. var sl = self.nc.getScrollLeft();
  2870. var st = self.nc.getScrollTop();
  2871. var pageh = self.nc.page.maxh;
  2872. var pagew = self.nc.page.maxw;
  2873. self.speedx = (pagew > 0) ? Math.min(60, self.speedx) : 0;
  2874. self.speedy = (pageh > 0) ? Math.min(60, self.speedy) : 0;
  2875. var chk = l && (t - l) <= 60;
  2876. if ((st < 0) || (st > pageh) || (sl < 0) || (sl > pagew)) chk = false;
  2877. var sy = (self.speedy && chk) ? self.speedy : false;
  2878. var sx = (self.speedx && chk) ? self.speedx : false;
  2879. if (sy || sx) {
  2880. var tm = Math.max(16, self.steptime); //timeout granularity
  2881. if (tm > 50) { // do smooth
  2882. var xm = tm / 50;
  2883. self.speedx *= xm;
  2884. self.speedy *= xm;
  2885. tm = 50;
  2886. }
  2887. self.demulxy = 0;
  2888. self.lastscrollx = self.nc.getScrollLeft();
  2889. self.chkx = self.lastscrollx;
  2890. self.lastscrolly = self.nc.getScrollTop();
  2891. self.chky = self.lastscrolly;
  2892. var nx = self.lastscrollx;
  2893. var ny = self.lastscrolly;
  2894. var onscroll = function () {
  2895. var df = ((now() - t) > 600) ? 0.04 : 0.02;
  2896. if (self.speedx) {
  2897. nx = Math.floor(self.lastscrollx - (self.speedx * (1 - self.demulxy)));
  2898. self.lastscrollx = nx;
  2899. if ((nx < 0) || (nx > pagew)) df = 0.10;
  2900. }
  2901. if (self.speedy) {
  2902. ny = Math.floor(self.lastscrolly - (self.speedy * (1 - self.demulxy)));
  2903. self.lastscrolly = ny;
  2904. if ((ny < 0) || (ny > pageh)) df = 0.10;
  2905. }
  2906. self.demulxy = Math.min(1, self.demulxy + df);
  2907. self.nc.synched("domomentum2d", function () {
  2908. if (self.speedx) {
  2909. var scx = self.nc.getScrollLeft();
  2910. // if (scx != self.chkx) self.stop();
  2911. self.chkx = nx;
  2912. self.nc.setScrollLeft(nx);
  2913. }
  2914. if (self.speedy) {
  2915. var scy = self.nc.getScrollTop();
  2916. // if (scy != self.chky) self.stop();
  2917. self.chky = ny;
  2918. self.nc.setScrollTop(ny);
  2919. }
  2920. if (!self.timer) {
  2921. self.nc.hideCursor();
  2922. self.doSnapy(nx, ny);
  2923. }
  2924. });
  2925. if (self.demulxy < 1) {
  2926. self.timer = setTimeout(onscroll, tm);
  2927. } else {
  2928. self.stop();
  2929. self.nc.hideCursor();
  2930. self.doSnapy(nx, ny);
  2931. }
  2932. };
  2933. onscroll();
  2934. } else {
  2935. self.doSnapy(self.nc.getScrollLeft(), self.nc.getScrollTop());
  2936. }
  2937. };
  2938. };
  2939. // override jQuery scrollTop
  2940. var _scrollTop = jQuery.fn.scrollTop; // preserve original function
  2941. jQuery.cssHooks.pageYOffset = {
  2942. get: function (elem, computed, extra) {
  2943. var nice = $.data(elem, '__nicescroll') || false;
  2944. return (nice && nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(elem);
  2945. },
  2946. set: function (elem, value) {
  2947. var nice = $.data(elem, '__nicescroll') || false;
  2948. (nice && nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call(elem, value);
  2949. return this;
  2950. }
  2951. };
  2952. jQuery.fn.scrollTop = function (value) {
  2953. if (value === undefined) {
  2954. var nice = (this[0]) ? $.data(this[0], '__nicescroll') || false : false;
  2955. return (nice && nice.ishwscroll) ? nice.getScrollTop() : _scrollTop.call(this);
  2956. } else {
  2957. return this.each(function () {
  2958. var nice = $.data(this, '__nicescroll') || false;
  2959. (nice && nice.ishwscroll) ? nice.setScrollTop(parseInt(value)) : _scrollTop.call($(this), value);
  2960. });
  2961. }
  2962. };
  2963. // override jQuery scrollLeft
  2964. var _scrollLeft = jQuery.fn.scrollLeft; // preserve original function
  2965. $.cssHooks.pageXOffset = {
  2966. get: function (elem, computed, extra) {
  2967. var nice = $.data(elem, '__nicescroll') || false;
  2968. return (nice && nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(elem);
  2969. },
  2970. set: function (elem, value) {
  2971. var nice = $.data(elem, '__nicescroll') || false;
  2972. (nice && nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call(elem, value);
  2973. return this;
  2974. }
  2975. };
  2976. jQuery.fn.scrollLeft = function (value) {
  2977. if (value === undefined) {
  2978. var nice = (this[0]) ? $.data(this[0], '__nicescroll') || false : false;
  2979. return (nice && nice.ishwscroll) ? nice.getScrollLeft() : _scrollLeft.call(this);
  2980. } else {
  2981. return this.each(function () {
  2982. var nice = $.data(this, '__nicescroll') || false;
  2983. (nice && nice.ishwscroll) ? nice.setScrollLeft(parseInt(value)) : _scrollLeft.call($(this), value);
  2984. });
  2985. }
  2986. };
  2987. var NiceScrollArray = function (doms) {
  2988. var self = this;
  2989. this.length = 0;
  2990. this.name = "nicescrollarray";
  2991. this.each = function (fn) {
  2992. $.each(self, fn);
  2993. return self;
  2994. };
  2995. this.push = function (nice) {
  2996. self[self.length] = nice;
  2997. self.length++;
  2998. };
  2999. this.eq = function (idx) {
  3000. return self[idx];
  3001. };
  3002. if (doms) {
  3003. for (var a = 0; a < doms.length; a++) {
  3004. var nice = $.data(doms[a], '__nicescroll') || false;
  3005. if (nice) {
  3006. this[this.length] = nice;
  3007. this.length++;
  3008. }
  3009. }
  3010. }
  3011. return this;
  3012. };
  3013. function mplex(el, lst, fn) {
  3014. for (var a = 0, l = lst.length; a < l; a++) fn(el, lst[a]);
  3015. }
  3016. mplex(
  3017. NiceScrollArray.prototype, ['show', 'hide', 'toggle', 'onResize', 'resize', 'remove', 'stop', 'doScrollPos'],
  3018. function (e, n) {
  3019. e[n] = function () {
  3020. var args = arguments;
  3021. return this.each(function () {
  3022. this[n].apply(this, args);
  3023. });
  3024. };
  3025. }
  3026. );
  3027. jQuery.fn.getNiceScroll = function (index) {
  3028. if (index === undefined) {
  3029. return new NiceScrollArray(this);
  3030. } else {
  3031. return this[index] && $.data(this[index], '__nicescroll') || false;
  3032. }
  3033. };
  3034. var pseudos = jQuery.expr.pseudos || jQuery.expr[':']; // jQuery 3 migration
  3035. pseudos.nicescroll = function (a) {
  3036. return $.data(a, '__nicescroll') !== undefined;
  3037. };
  3038. $.fn.niceScroll = function (wrapper, _opt) {
  3039. if (_opt === undefined && typeof wrapper == "object" && !("jquery" in wrapper)) {
  3040. _opt = wrapper;
  3041. wrapper = false;
  3042. }
  3043. var ret = new NiceScrollArray();
  3044. this.each(function () {
  3045. var $this = $(this);
  3046. var opt = $.extend({}, _opt); // cloning
  3047. if (wrapper || false) {
  3048. var wrp = $(wrapper);
  3049. opt.doc = (wrp.length > 1) ? $(wrapper, $this) : wrp;
  3050. opt.win = $this;
  3051. }
  3052. var docundef = !("doc" in opt);
  3053. if (!docundef && !("win" in opt)) opt.win = $this;
  3054. var nice = $this.data('__nicescroll') || false;
  3055. if (!nice) {
  3056. opt.doc = opt.doc || $this;
  3057. nice = new NiceScrollClass(opt, $this);
  3058. $this.data('__nicescroll', nice);
  3059. }
  3060. ret.push(nice);
  3061. });
  3062. return (ret.length === 1) ? ret[0] : ret;
  3063. };
  3064. _win.NiceScroll = {
  3065. getjQuery: function () {
  3066. return jQuery;
  3067. }
  3068. };
  3069. if (!$.nicescroll) {
  3070. $.nicescroll = new NiceScrollArray();
  3071. $.nicescroll.options = _globaloptions;
  3072. }
  3073. }));