jquery.nicescroll.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* jquery.nicescroll
  2. -- versione 1.0.0
  3. -- copyright 2011 InuYaksa*2011
  4. -- licensed under the MIT
  5. --
  6. -- https://github.com/inuyaksa/jquery.nicescroll
  7. --
  8. */
  9. (function( $ ){
  10. var AScrollClass = function(myopt) {
  11. var self = this;
  12. var opt = {
  13. doc:$("body"),
  14. win:$(window),
  15. zindex:9999,
  16. cursoropacitymin:0,
  17. scrollspeed:60
  18. };
  19. if (myopt) {
  20. for(var a in opt) {
  21. if (myopt[a]!==undefined) opt[a] = myopt[a];
  22. }
  23. }
  24. this.zindex = opt.zindex;
  25. this.cursoropacitymin = opt.cursoropacitymin;
  26. this.doc = opt.doc;
  27. this.win = opt.win;
  28. this.docscroll = (self.doc[0].nodeName=='BODY'||self.doc[0].nodeName=='HTML')?$(window):this.doc;
  29. this.screen = false;
  30. this.page = false;
  31. this.scroll = {x:0,y:0};
  32. this.scrollratio = {x:0,y:0};
  33. this.cursorheight = 20;
  34. this.scrollvaluemax = 0;
  35. do {
  36. this.id = "ascrail"+Math.round(Math.random() * 99999);
  37. } while (document.getElementById(this.id));
  38. this.rail = false;
  39. this.cursor = false;
  40. this.cursorfreezed = false;
  41. this.isie = (document.all && !document.opera);
  42. this.scrollTop = function(val) {
  43. return (val === undefined) ? self.getScrollTop() : self.setScrollTop(val);
  44. };
  45. this.getScrollTop = function() {
  46. return self.docscroll.scrollTop();
  47. };
  48. this.setScrollTop = function(val) {
  49. return self.docscroll.scrollTop(val);
  50. };
  51. this.hasParent = function(e,id) {
  52. var el = e.target||e||false;
  53. while (el && el.id != id) {
  54. el = el.parentNode||false;
  55. }
  56. return (el!==false);
  57. };
  58. this.init = function() {
  59. self.doc.css({'overflow-y':'hidden'});
  60. var rail = $(document.createElement('div'));
  61. rail.attr('id',self.id);
  62. rail.css({position:"fixed",top:0,right:0,"padding-left":"4px",width:"12px",height:"100%",'z-index':self.zindex,opacity:self.cursoropacitymin});
  63. self.rail = rail;
  64. self.doc.append(rail);
  65. var cursor = $(document.createElement('div'));
  66. cursor.css({
  67. position:"relative",top:0,right:0,width:"8px",height:"0px",'background-color':"#424242",
  68. border:"1px solid #fff",
  69. '-webkit-border-radius':'4px',
  70. '-moz-border-radius':'4px',
  71. 'border-radius':'4px'
  72. });
  73. self.cursor = cursor;
  74. self.rail.append(cursor);
  75. self.win.resize(function(){self.onResize()});
  76. self.doc.resize(function(){self.onResize()});
  77. self.onResize();
  78. $(self.rail).mousedown(function(e) {
  79. self.rail.drag = {x:e.screenX,y:e.screenY,sx:self.scroll.x,sy:self.scroll.y};
  80. e.preventDefault();
  81. return false;
  82. });
  83. $(self.rail).mouseup(function() {
  84. self.rail.drag = false;
  85. return false;
  86. });
  87. $(document).mouseup(function(e) {
  88. self.rail.drag = false;
  89. self.hideCursor();
  90. });
  91. $(document).mousemove(function(e) {
  92. if (self.rail.drag) {
  93. self.scroll.y = self.rail.drag.sy + (e.screenY-self.rail.drag.y);
  94. if (self.scroll.y<0) self.scroll.y=0;
  95. var my = self.scrollvaluemax;
  96. if (self.scroll.y>my) self.scroll.y=my;
  97. self.showCursor();
  98. self.cursorfreezed = true;
  99. self.doScroll(Math.round(self.scroll.y*self.scrollratio.y));
  100. e.preventDefault();
  101. return false;
  102. }
  103. });
  104. $(self.rail).mouseenter(function() {
  105. $(self.rail).animate({opacity:1});
  106. self.rail.active = true;
  107. });
  108. $(self.rail).mouseleave(function() {
  109. self.rail.active = false;
  110. if (!self.rail.drag) self.hideCursor();
  111. });
  112. self.mousewheel(function(delta){
  113. self.scroll.y+=(-delta)*12;
  114. if (self.scroll.y<0) self.scroll.y=0;
  115. var my = self.scrollvaluemax;
  116. if (self.scroll.y>my) self.scroll.y=my;
  117. self.cursorfreezed = false;
  118. self.doScroll(Math.round(self.scroll.y*self.scrollratio.y));
  119. });
  120. };
  121. this.showCursor = function() {
  122. if (self.cursortimeout) {
  123. clearTimeout(self.cursortimeout);
  124. self.cursortimeout = 0;
  125. }
  126. self.rail.clearQueue().css({opacity:1});
  127. self.cursor.css({height:self.cursorheight,top:self.scroll.y});
  128. }
  129. this.hideCursor = function(tm) {
  130. if (self.cursortimeout) return;
  131. self.cursortimeout = setTimeout(function() {
  132. if (self.rail.active) return;
  133. $(self.rail).animate({opacity:self.cursoropacitymin});
  134. self.cursortimeout = 0;
  135. },tm||800);
  136. };
  137. this.getContentSize = function() {
  138. return (self.doc[0].nodeName=='BODY') ?
  139. {
  140. w:Math.max(document.body.scrollWidth,document.documentElement.scrollWidth),
  141. h:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight)
  142. } :
  143. {
  144. w:self.doc[0].scrollWidth,
  145. h:self.doc[0].scrollHeight
  146. };
  147. };
  148. this.onResize = function() {
  149. self.view = {
  150. w:self.win.width(),
  151. h:self.win.height()
  152. };
  153. self.page = self.getContentSize();
  154. self.cursorheight = Math.round(self.view.h * (self.view.h / self.page.h));
  155. self.scrollvaluemax = self.view.h-self.cursorheight-1;
  156. self.scrollratio = {
  157. x:0,
  158. y:((self.page.h - self.view.h)/self.scrollvaluemax)
  159. };
  160. self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  161. self.showCursor();
  162. self.hideCursor();
  163. };
  164. this.bind = function(dom,name,fn,bubble) { // fixing jquery
  165. var el = dom[0];
  166. if (el.addEventListener) {
  167. el.addEventListener(name,fn,bubble||false);
  168. }
  169. else if (el.attachEvent) {
  170. el.attachEvent(name,fn);
  171. }
  172. else {
  173. el["on"+name] = fn;
  174. }
  175. };
  176. this.mousewheel = function(fn) {
  177. function wheel(e){
  178. var delta = 0;
  179. e = e ? e : window.event;
  180. var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  181. if (fn&&delta) fn(delta);
  182. if (e.preventDefault) e.preventDefault();
  183. e.returnValue = false;
  184. }
  185. if (!self.isie) self.bind(self.docscroll,'DOMMouseScroll',wheel,false);
  186. self.bind(self.docscroll,'mousewheel',wheel,false);
  187. };
  188. this.doScroll = function(y) {
  189. self.newscrolly = y;
  190. if (self.timer) return;
  191. self.timer = setInterval(function() {
  192. var gp = self.newscrolly - self.getScrollTop();
  193. var df = (gp>0) ? Math.ceil(gp/4) : Math.floor(gp/4);
  194. var sc = self.getScrollTop()+df;
  195. self.setScrollTop(sc);
  196. if (sc == self.newscrolly) {
  197. clearInterval(self.timer);
  198. self.timer = 0;
  199. self.cursorfreezed = false;
  200. }
  201. },opt.scrollspeed);
  202. }
  203. self.win.scroll(function(e) {
  204. var tm = (new Date()).getTime();
  205. if (!self.lastcontentcheck || self.lastcontentcheck<tm) {
  206. self.lastcontentcheck=tm+500;
  207. var pg = self.getContentSize();
  208. if (pg.h!=self.page.h) self.onResize();
  209. }
  210. if (self.rail.drag) return;
  211. if (!self.cursorfreezed) self.scroll.y = Math.round(self.getScrollTop() * (1/self.scrollratio.y));
  212. self.showCursor();
  213. self.hideCursor();
  214. });
  215. this.init();
  216. }
  217. $.fn.niceScroll = function(opt) {
  218. var ret = [];
  219. if (!opt) opt = {};
  220. this.each(function() {
  221. opt.doc = (opt.doc===undefined) ? $(this) : opt.doc;
  222. ret.push(new AScrollClass(opt));
  223. });
  224. return (ret.length==1) ? ret[0] : ret;
  225. };
  226. })( jQuery );