jquery.nicescroll.js 106 KB

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