jquery.nicescroll.js 101 KB

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