jquery.nicescroll.js 109 KB

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