jquery.nicescroll.js 111 KB

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