jquery.nicescroll.js 118 KB

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