Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

scrollBar.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /**
  2. * Created by Administrator on 2017/9/6.
  3. */
  4. //Vertical and horizontal
  5. /**
  6. * 用div的拖拽以及滚动事件代替了滚动条,通过改变div的样式来改变滚动条的样式
  7. */
  8. ;(function($){
  9. $.fn.extend({
  10. "scrollBar":function(options){
  11. var settings = {
  12. barWidth:5,
  13. position:"x,y",
  14. wheelDis:15
  15. };
  16. options = $.extend(settings,options);
  17. var horizontalDiv = '<div class="zl-scrollBarBox" style="width:100%;left:0;height:'+settings.barWidth+'px;bottom:0;">'+
  18. '<div class="zl-scrollBar zl-horizontalBar" style="height:'+settings.barWidth+'px;border-radius:'+settings.barWidth/2+'px;"></div>'+
  19. '</div>';
  20. var verticalDiv = '<div class="zl-scrollBarBox" style="height:100%;top:0;width:'+settings.barWidth+'px;right:0;">'+
  21. '<div class="zl-scrollBar zl-verticalBar" style="width:'+settings.barWidth+'px;border-radius:'+settings.barWidth/2+'px;"></div>'+
  22. '</div>';
  23. var T = this;
  24. /**
  25. * 将每个div的padding值保存到自定义属性中
  26. */
  27. T.each(function(){
  28. $(this).attr("paddingR",$(this).css("padding-right")).attr("paddingB",$(this).css("padding-bottom"));
  29. });
  30. /**
  31. *
  32. *创建滚动条的函数
  33. */
  34. function creatScrollBar(obj){
  35. var This = $(obj).get(0);//转化为JS对象,增加运行效率
  36. var paddingR = parseFloat($(obj).attr("paddingR"));
  37. var paddingB = parseFloat($(obj).attr("paddingB"));
  38. $(obj).css({
  39. "padding-right":paddingR+"px",
  40. "padding-bottom":paddingB+"px",
  41. "overflow":"hidden"
  42. });
  43. //获取内容的总高度和总宽度
  44. if(!($(obj).children().hasClass("zl-scrollContentDiv"))){
  45. $(obj).wrapInner('<div class="zl-scrollContentDiv"></div>');
  46. };
  47. if($(obj).css("position")=="static"){
  48. $(obj).css({"position":"relative"});
  49. };
  50. var scrollContentDiv = $(obj).find(".zl-scrollContentDiv");
  51. var scrollH = scrollContentDiv[0].scrollHeight;
  52. var scrollW = scrollContentDiv[0].scrollWidth;
  53. var innerH = scrollContentDiv.height();
  54. var innerW = scrollContentDiv.width();
  55. var outerH = $(obj).innerHeight();
  56. var outerW = $(obj).innerWidth();
  57. function addVerticalBar(a){
  58. This.style.paddingRight = paddingR + settings.barWidth + 'px';
  59. innerH = $(a).height();
  60. var barHeight = outerH*(innerH/scrollH);
  61. $(a).find(".zl-scrollBarBox").remove().end().append(verticalDiv).find(".zl-verticalBar").height(barHeight);
  62. };
  63. function addHorizontalBar(a){
  64. This.style.paddingBottom = paddingB + settings.barWidth + 'px';
  65. innerW = $(a).width();
  66. var barWidth = outerW*(innerW/scrollW);
  67. $(a).find(".zl-scrollBarBox").remove().end().append(horizontalDiv).find(".zl-horizontalBar").width(barWidth);
  68. };
  69. switch (settings.position){
  70. case "x,y":
  71. if(scrollH>innerH && scrollW>innerW){
  72. This.style.paddingRight = paddingR + settings.barWidth + 'px';
  73. innerH = $(obj).height();
  74. outerH = $(obj).innerHeight();
  75. var barHeight = (outerH-settings.barWidth)*((innerH-settings.barWidth)/scrollH);
  76. if(!($(obj).find(".zl-verticalBar").length)){
  77. $(obj).append(verticalDiv);
  78. };
  79. $(obj).find(".zl-verticalBar").height(barHeight).parent().height(outerH-settings.barWidth);
  80. This.style.paddingBottom = paddingB + settings.barWidth + 'px';
  81. innerW = $(obj).width();
  82. var barWidth = (outerW-settings.barWidth)*((innerW-settings.barWidth)/scrollW);
  83. if(!($(obj).find(".zl-horizontalBar").length)){
  84. $(obj).append(horizontalDiv);
  85. };
  86. $(obj).find(".zl-horizontalBar").width(barWidth).parent().width(outerW)
  87. .css({
  88. "padding-right":settings.barWidth+"px",
  89. "box-sizing":"border-box"
  90. });
  91. }else if(scrollH>innerH){
  92. addVerticalBar(obj);
  93. }else if(scrollW>innerW){
  94. addHorizontalBar(obj);
  95. }else{
  96. $(obj).find(".zl-scrollBarBox").remove();
  97. }
  98. break;
  99. case "x":
  100. if(scrollW>innerW){
  101. addHorizontalBar(obj);
  102. }else{
  103. $(obj).find(".zl-scrollBarBox").remove();
  104. }
  105. break;
  106. case "y":
  107. if(scrollH>innerH){
  108. addVerticalBar(obj);
  109. }else{
  110. $(obj).find(".zl-scrollBarBox").remove();
  111. }
  112. break;
  113. };
  114. }
  115. /**
  116. * 循环给每一个Div添加
  117. */
  118. function recycleThis(){
  119. T.each(function(){
  120. creatScrollBar(this);
  121. });
  122. };
  123. recycleThis();//执行循环
  124. /**
  125. *创建监听div内容变化的定时器
  126. */
  127. function creatTimer(obj,oldWidth,oldHeight,oldInnerWidth,oldInnerHeight,timer){
  128. timer = setInterval(function(){
  129. var newWidth = null;
  130. var newHeight = null;
  131. var newInnerWidth = null;
  132. var newInnerHeight = null;
  133. var topDiv = null;
  134. var leftDiv = null;
  135. var topBar = null;
  136. var leftBar = null;
  137. var scrollContentDiv = $(obj).find(".zl-scrollContentDiv");
  138. if(scrollContentDiv.length){
  139. newWidth = scrollContentDiv[0].scrollWidth;
  140. newHeight = scrollContentDiv[0].scrollHeight;
  141. newInnerWidth = scrollContentDiv.width();
  142. newInnerHeight = scrollContentDiv.height();
  143. }else{
  144. newWidth = $(obj)[0].scrollWidth - parseFloat($(obj).css("padding-left"));
  145. newHeight = $(obj)[0].scrollHeight - parseFloat($(obj).css("padding-top"));
  146. newInnerWidth = $(obj).width();
  147. newInnerHeight = $(obj).height();
  148. };
  149. if(newWidth!=oldWidth || newHeight!=oldHeight || newInnerWidth!=oldInnerWidth || newInnerHeight!=oldInnerHeight){
  150. //记录更新滚动条长短前的位置
  151. if(scrollContentDiv.length){
  152. topDiv = parseFloat(scrollContentDiv.css("top"));
  153. leftDiv = parseFloat(scrollContentDiv.css("left"));
  154. };
  155. if($(obj).find(".zl-verticalBar").length){
  156. topBar = parseFloat($(obj).find(".zl-verticalBar").css("top"));
  157. };
  158. if($(obj).find(".zl-horizontalBar").length){
  159. leftBar = parseFloat($(obj).find(".zl-horizontalBar").css("left"));
  160. };
  161. //更新滚动条长短或有无
  162. creatScrollBar(obj);
  163. if($(obj).find(".zl-scrollBarBox").length){
  164. if(topDiv){
  165. var maxTopBox = scrollContentDiv[0].scrollHeight - $(obj).height();
  166. var maxLeftBox = scrollContentDiv[0].scrollWidth - $(obj).width();
  167. if(-leftDiv>maxLeftBox){
  168. leftDiv = -maxLeftBox;
  169. }
  170. if(-topDiv>maxTopBox){
  171. topDiv = -maxTopBox;
  172. }
  173. scrollContentDiv.css({
  174. "left":leftDiv+'px',
  175. "top":topDiv+'px'
  176. });
  177. };
  178. //将原来的位置赋值给现在的滚动条
  179. if(topBar && $(obj).find(".zl-verticalBar").length){
  180. var verticalBar = $(obj).find(".zl-verticalBar");
  181. var maxTop = verticalBar.parent().height() -verticalBar.height();
  182. if(topBar>maxTop){
  183. topBar = maxTop;
  184. }
  185. verticalBar.css("top",topBar+'px');
  186. };
  187. if(leftBar && $(obj).find(".zl-horizontalBar").length){
  188. var horizontalBar = $(obj).find(".zl-verticalBar");
  189. var maxLeft = horizontalBar.parent().width() -horizontalBar.width();
  190. if(leftBar>maxLeft){
  191. leftBar = maxLeft;
  192. }
  193. $(obj).find(".zl-horizontalBar").css("left",leftBar+'px');
  194. };
  195. }
  196. oldWidth = newWidth;
  197. oldHeight = newHeight;
  198. oldInnerHeight = newInnerHeight;
  199. oldInnerWidth = newInnerWidth;
  200. }
  201. },100);
  202. }
  203. /**
  204. * 通过循环给每一个div添加上监听内容变化的定时器
  205. */
  206. function addTimer(){
  207. $.each(T,function(k,v){
  208. var obj = v;
  209. var timer = "timer"+k;
  210. var oldWidth = null;
  211. var oldHeight = null;
  212. var oldInnerWidth = null;
  213. var oldInnerHeight = null;
  214. if($(v).find(".zl-scrollContentDiv").length){
  215. oldWidth = $(v).find(".zl-scrollContentDiv")[0].scrollWidth ;
  216. oldHeight = $(v).find(".zl-scrollContentDiv")[0].scrollHeight;
  217. oldInnerWidth = $(v).find(".zl-scrollContentDiv").width();
  218. oldInnerHeight = $(v).find(".zl-scrollContentDiv").height();
  219. }else{
  220. oldWidth = $(obj)[0].scrollWidth - parseFloat($(obj).css("padding-left"));
  221. oldHeight = $(obj)[0].scrollHeight - parseFloat($(obj).css("padding-top"));
  222. oldInnerWidth = $(obj).width();
  223. oldInnerHeight = $(obj).height();
  224. }
  225. creatTimer(obj,oldWidth,oldHeight,oldInnerWidth,oldInnerHeight,timer);
  226. });
  227. }
  228. addTimer();
  229. function clearTimer(){
  230. $.each(T,function(index,item){
  231. var timer = "timer"+index;
  232. clearInterval(timer);
  233. });
  234. };
  235. /**
  236. * 滚动条拖拽效果
  237. */
  238. this.on("mousedown",".zl-scrollBar",function(ev){
  239. clearTimer();//清除定时器
  240. var direction = null;
  241. if($(this).hasClass("zl-verticalBar")){
  242. direction = "0";
  243. }else if($(this).hasClass("zl-horizontalBar")){
  244. direction = "1";
  245. }
  246. var This = $(this).get(0);
  247. var height = $(this).parent().height() - $(this).height();
  248. var width = $(this).parent().width() - $(this).width();
  249. var contentDiv = $(this).parent().parent().find(".zl-scrollContentDiv").get(0);
  250. var scrollH = contentDiv.scrollHeight;
  251. var innerH = $(this).parent().parent().height();
  252. var scrollW = contentDiv.scrollWidth;
  253. var innerW = $(this).parent().parent().width();
  254. var ev = ev || event;
  255. var disY = ev.clientY - This.offsetTop;
  256. var disX = ev.clientX - This.offsetLeft;
  257. var topCount = null;
  258. var leftCount = null;
  259. switch (direction){
  260. case "0":
  261. document.onmousemove = function(ev){
  262. var ev = ev || event;
  263. if(ev.clientY - disY <= 0){
  264. topCount = 0;
  265. }else
  266. if((ev.clientY - disY) >= height){
  267. topCount = height;
  268. }else{
  269. topCount = ev.clientY - disY;
  270. }
  271. This.style.top = topCount + "px";
  272. contentDiv.style.top = -(topCount*(scrollH - innerH)/height) + 'px';
  273. };
  274. break;
  275. case "1":
  276. document.onmousemove = function(ev){
  277. var ev = ev || event;
  278. if(ev.clientX - disX <= 0){
  279. leftCount = 0;
  280. }else
  281. if((ev.clientX - disX) >= width){
  282. leftCount = width;
  283. }else{
  284. leftCount = ev.clientX - disX;
  285. }
  286. This.style.left = leftCount + "px";
  287. contentDiv.style.left = -(leftCount*(scrollW - innerW)/width) + 'px';
  288. };
  289. break;
  290. }
  291. document.onmouseup = function(){
  292. document.onmousemove = null;
  293. document.onmouseup = null;
  294. addTimer();//添加定时器
  295. };
  296. return false;
  297. });
  298. /**
  299. *鼠标滚轮效果
  300. */
  301. function fn(ev,a){
  302. if(a.find(".zl-verticalBar").length){
  303. var b = true;
  304. var height = a.find(".zl-scrollBarBox").height() - a.find(".zl-scrollBar").height();
  305. var contentDiv = a.find(".zl-scrollContentDiv").get(0);
  306. var scrollH = contentDiv.scrollHeight;
  307. var innerH = a.height();
  308. if(ev.wheelDelta){
  309. b = ev.wheelDelta>0?true:false;
  310. }else{
  311. b = ev.detail<0?true:false;
  312. }
  313. var topDis = parseFloat(a.find(".zl-scrollBar").css("top"));
  314. if(b){
  315. topDis -= settings.wheelDis;
  316. if(topDis <0){
  317. topDis = 0;
  318. }
  319. }else{
  320. topDis += settings.wheelDis;
  321. if(topDis > height){
  322. topDis = height;
  323. }
  324. }
  325. a.find(".zl-scrollBar").get(0).style.top = topDis + "px";
  326. a.find(".zl-scrollContentDiv").get(0).style.top = -(topDis*(scrollH - innerH)/height) + 'px';
  327. }
  328. };
  329. T.each(function(){
  330. var oDiv = $(this).get(0);
  331. if(oDiv.addEventListener){
  332. oDiv.addEventListener("DOMMouseScroll",function(ev){
  333. var ev = ev || event;
  334. var $This = $(this);
  335. fn(ev,$This);
  336. ev.preventDefault();
  337. },false);
  338. }
  339. oDiv.onmousewheel = function(ev){
  340. var ev = ev || event;
  341. var $This = $(this);
  342. fn(ev,$This);
  343. return false;
  344. };
  345. });
  346. return this;
  347. }
  348. });
  349. })(jQuery);