jquery.nicescroll.js 7.4 KB

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