scrollbar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. (function(factory) {
  2. "use strict";
  3. if(typeof exports === "object" && typeof module === "object")
  4. {
  5. module.exports = factory(PlaneUI);
  6. }
  7. else if(typeof define === "function" && (define.amd || define.cmd))
  8. {
  9. define(["PlaneUI"], factory);
  10. }
  11. else
  12. {
  13. window.PUI = window.PlaneUI = window.$ = factory(PlaneUI);
  14. }
  15. })(function(PlaneUI) {
  16. (function($) {
  17. function scrollbox(options) {
  18. var defaults = {
  19. width : 8,
  20. speed : 500,
  21. direction : "vertical", // x|horizontal => horizontal, y|vertical => vertical, auto => Auto direction
  22. animated : true,
  23. touchable : true,
  24. mousewheel : true,
  25. hoverDisplay : true,
  26. start : function() {},
  27. end : function() {}
  28. };
  29. var settings = $.extend(defaults, options || {});
  30. $(this).each(function() {
  31. this.settings = settings;
  32. $.proxy(eachHanle, this)();
  33. });
  34. return this;
  35. };
  36. $.fn.scrollbox = scrollbox;
  37. function eachHanle() {
  38. var $this = $(this);
  39. var settings = this.settings;
  40. if ($this.find(".pui-scrollbox-scrollbar").length < 1)
  41. {
  42. $this.append('<div class="pui-scrollbox-scrollbar"><div class="pui-scrollbox-thumb"></div></div>');
  43. }
  44. var scrollbar = $this.find(".pui-scrollbox-scrollbar");
  45. var thumb = $this.find(".pui-scrollbox-thumb");
  46. var container = $this.find(".pui-scrollbox-container");
  47. this.thumb = thumb;
  48. this.container = container;
  49. this.scrollbar = scrollbar;
  50. this.offsetY = 0;
  51. this.offsetX = 0;
  52. this.touchOffset = null;
  53. $.proxy(resize, this)();
  54. $(window).resize($.proxy(resize, this));
  55. var _this = this;
  56. if (settings.hoverDisplay)
  57. {
  58. $(this).hover(function() {
  59. thumb.stop().fadeIn();
  60. }, function() {
  61. thumb.stop().fadeOut();
  62. });
  63. }
  64. else
  65. {
  66. thumb.fadeIn();
  67. }
  68. // 鼠标按下
  69. thumb.bind({
  70. "selectstart" : function() {
  71. return false;
  72. },
  73. "mousedown" : function(e) {
  74. e = e || window.event;
  75. if (e.preventDefault) {
  76. e.preventDefault();
  77. } else {
  78. e.returnValue = false;
  79. }
  80. _this.offsetX = e.clientX - parseInt(thumb.css("left"));
  81. _this.offsetY = e.clientY - parseInt(thumb.css("top"));
  82. document.onmousemove = $.proxy(moveAction, _this);
  83. }
  84. });
  85. document.onmouseup = function() {
  86. document.onselectstart = null;
  87. document.onmousemove = null;
  88. };
  89. if (settings.mousewheel) // 如果开启鼠标滑轮滚动
  90. {
  91. var mousewheelEvent = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
  92. $this.bind(mousewheelEvent, $.proxy(mouseWheelAction, this));
  93. }
  94. }
  95. function resize() {
  96. var $this = $(this);
  97. var thumb = this.thumb;
  98. var settings = this.settings;
  99. var container = this.container;
  100. var scrollbar = this.scrollbar;
  101. if (settings.direction === "x" || settings.direction === "horizontal")
  102. {
  103. scrollbar.css({
  104. width : $this.width() + "px",
  105. height : settings.width + "px"
  106. }).css({
  107. top : ($this.height() - scrollbar.height()) + "px"
  108. });
  109. var thumbWidth = $this.width() / container.width() * $this.width();
  110. thumb.css({
  111. width : thumbWidth + "px",
  112. height : scrollbar.height() + "px"
  113. });
  114. }
  115. else if (settings.direction === "y" || settings.direction === "vertical") // 竖向
  116. {
  117. scrollbar.css({
  118. height : $this.height(),
  119. width : settings.width + "px"
  120. });
  121. var thumbHeight = $this.height() / container.height() * $this.height();
  122. thumb.css({
  123. width : settings.width + "px",
  124. height : thumbHeight + "px"
  125. });
  126. }
  127. else
  128. {
  129. }
  130. }
  131. // 鼠标拖动
  132. function moveAction(e) {
  133. var thumb = this.thumb;
  134. var settings = this.settings;
  135. var container = this.container;
  136. var scrollbar = this.scrollbar;
  137. var offsetX = this.offsetX;
  138. var offsetY = this.offsetY;
  139. document.onselectstart = function() {
  140. return false;
  141. };
  142. if (settings.direction === "x" || settings.direction === "horizontal") // 横向
  143. {
  144. var x = e.clientX - offsetX, left = 0;
  145. if (x + thumb.width() >= scrollbar.width())
  146. {
  147. left = scrollbar.width() - thumb.width();
  148. $.proxy(settings.end, this)();
  149. document.onmousemove = null;
  150. }
  151. else if (x <= 0)
  152. {
  153. left = 0;
  154. $.proxy(settings.start, this)();
  155. document.onmousemove = null;
  156. }
  157. else
  158. {
  159. left = x;
  160. }
  161. $.proxy(scrollLeft, this)(left);
  162. }
  163. else if (settings.direction === "y" || settings.direction === "vertical") // 竖向
  164. {
  165. var clientY = document.all ? e.clientY : e.pageY;
  166. var y = clientY - offsetY, top = 0;
  167. if (y + thumb.height() >= scrollbar.height())
  168. {
  169. top = scrollbar.height() - thumb.height();
  170. $.proxy(settings.end, this)();
  171. document.onmousemove = null;
  172. }
  173. else if (y <= 0)
  174. {
  175. top = 0;
  176. $.proxy(settings.start, this)();
  177. document.onmousemove = null;
  178. }
  179. else
  180. {
  181. top = y;
  182. }
  183. $.proxy(scrollTop, this)(top);
  184. }
  185. else
  186. {
  187. }
  188. }
  189. function scrollTop(top, mousewheel) {
  190. mousewheel = mousewheel || false;
  191. var thumb = this.thumb;
  192. var settings = this.settings;
  193. var container = this.container;
  194. var scrollbar = this.scrollbar;
  195. var ratio = top / scrollbar.height(); //移动比例
  196. var newTop = Math.floor(container.height() * ratio);
  197. if (settings.animated)
  198. {
  199. container.stop().animate({top : "-" + newTop + "px"}, settings.speed);
  200. if (mousewheel) {
  201. thumb.stop().animate({top : top + "px"}, settings.speed);
  202. } else {
  203. thumb.css("top", top);
  204. }
  205. }
  206. else
  207. {
  208. thumb.css("top", top);
  209. container.css("top", "-" + newTop + "px");
  210. }
  211. }
  212. function scrollLeft(left, mousewheel) {
  213. mousewheel = mousewheel || false;
  214. var thumb = this.thumb;
  215. var settings = this.settings;
  216. var container = this.container;
  217. var scrollbar = this.scrollbar;
  218. var ratio = left / scrollbar.width(); //移动比例
  219. var newLeft = Math.floor(container.width() * ratio);
  220. if (settings.animated)
  221. {
  222. container.stop().animate({left : "-" + newLeft + "px"}, settings.speed);
  223. if (mousewheel) {
  224. thumb.stop().animate({left : left + "px"}, settings.speed);
  225. } else {
  226. thumb.css("left", left);
  227. }
  228. }
  229. else
  230. {
  231. thumb.css("left", left);
  232. container.css("left", "-" + newLeft + "px");
  233. }
  234. }
  235. // 鼠标滑轮滚动
  236. function mouseWheelAction(e) {
  237. var delta = e.originalEvent.wheelDelta ? (e.originalEvent.wheelDelta / 120) : (- e.originalEvent.detail / 3);
  238. var thumb = this.thumb;
  239. var settings = this.settings;
  240. var container = this.container;
  241. var scrollbar = this.scrollbar;
  242. if (settings.hoverDisplay) {
  243. thumb.show();
  244. }
  245. if (settings.direction === "x" || settings.direction === "horizontal") // 横向
  246. {
  247. var x = parseInt(thumb.css("left")), left, speed = scrollbar.width() * 0.1223;
  248. x = (delta < 0) ? x + speed : x - speed; //每步移动距离, 滑动方向 delta,-1向下,1向上
  249. if (x + thumb.width() >= scrollbar.width())
  250. {
  251. left = scrollbar.width() - thumb.width();
  252. $.proxy(settings.end, this)();
  253. }
  254. else if (x <= 0)
  255. {
  256. left = 0;
  257. $.proxy(settings.start, this)();
  258. }
  259. else
  260. {
  261. left = x;
  262. }
  263. $.proxy(scrollLeft, this)(left, true);
  264. }
  265. else if (settings.direction === "y" || settings.direction === "vertical") // 竖向
  266. {
  267. var y = parseInt(thumb.css("top")), top, speed = scrollbar.height() * 0.1223;
  268. y = (delta < 0) ? y + speed : y - speed; // 每步移动距离
  269. if (y + thumb.height() >= scrollbar.height())
  270. {
  271. top = scrollbar.height() - thumb.height();
  272. $.proxy(settings.end, this)();
  273. }
  274. else if(y <= 0)
  275. {
  276. top = 0;
  277. $.proxy(settings.start, this)();
  278. }
  279. else
  280. {
  281. top = y;
  282. }
  283. $.proxy(scrollTop, this)(top, true);
  284. }
  285. else
  286. {
  287. }
  288. }
  289. })(PlaneUI);
  290. return PlaneUI;
  291. });