jquery.nicescroll.js 113 KB

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