jquery.nicescroll.js 119 KB

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