jquery.nicescroll.js 111 KB

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