slideResize.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/11/7
  7. * @version
  8. */
  9. /*
  10. * div之间的水平拖动,适应各种情况
  11. * module: 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  12. * eleObj: resize, parent, left, right
  13. * limit: min, max
  14. * */
  15. const SlideResize = (function() {
  16. //设置水平拖动条的宽度
  17. //@param {Object dom}resize滚动条
  18. function setResizeWidth (resize) {
  19. const fixedWidth = 10;
  20. //滚动条节点 及 同层非滚动条节点的索引
  21. let bros = resize.parent().children();
  22. let index = bros.index(resize),
  23. otherIndex = index ? 0 : 1;
  24. const other = resize.parent().children(`:eq(${otherIndex})`);
  25. let resizeParentWidth = resize.parent().width();
  26. let resizeDecimalWidth = fixedWidth / resizeParentWidth,
  27. otherDecimalWidth = 1 - resizeDecimalWidth;
  28. let resizePercentWidth = resizeDecimalWidth * 100 + '%',
  29. otherPercentWidth = otherDecimalWidth * 100 + '%';
  30. resize.css('width', resizePercentWidth);
  31. other.css('width', otherPercentWidth);
  32. }
  33. let mouseMoveCount = 0;
  34. function horizontalSlide(module, eleObj, limit, callback) {
  35. const triggerCBSize = 5;
  36. let drag = false,
  37. startPoint = 0,
  38. leftWidth = 0,
  39. rightWidth = 0,
  40. leftChange = 0,
  41. rightChange = 0,
  42. limitMax = 0;
  43. eleObj.resize.mousedown(function (e) {
  44. drag = true;
  45. startPoint = e.clientX;
  46. leftWidth = eleObj.left.width();
  47. rightWidth = eleObj.right.width();
  48. limitMax = eval(limit.max);
  49. });
  50. $('body').mousemove(function (e) {
  51. if (drag) {
  52. let moveSize = e.clientX - startPoint;
  53. leftChange = leftWidth + moveSize;
  54. leftChange = leftChange < limit.min ? limit.min : leftChange;
  55. leftChange = leftChange > limitMax ? limitMax - 3 : leftChange;
  56. rightChange = rightWidth - moveSize;
  57. rightChange = rightChange < limit.min ? limit.min : rightChange;
  58. rightChange = rightChange > limitMax ? limitMax - 3 : rightChange;
  59. let leftPercentWidth = leftChange / eleObj.parent.width() * 100 + '%',
  60. rightPercentWidth = rightChange / eleObj.parent.width() * 100 + '%';
  61. eleObj.left.css('width', leftPercentWidth);
  62. eleObj.right.css('width', rightPercentWidth);
  63. setResizeWidth(eleObj.resize);
  64. mouseMoveCount += Math.abs(moveSize);
  65. if (mouseMoveCount > triggerCBSize && callback) {
  66. callback();
  67. mouseMoveCount = 0;
  68. }
  69. }
  70. });
  71. $('body').mouseup(function (e) {
  72. if (drag) {
  73. drag = false;
  74. mouseMoveCount = 0;
  75. //将宽度信息存储到localstorage
  76. let leftWidthInfo = eleObj.left[0].style.width;
  77. setLocalCache(`${module}${eleObj.left.attr('id')}Width`, leftWidthInfo);
  78. let rightWidthInfo = eleObj.right[0].style.width;
  79. setLocalCache(`${module}${eleObj.right.attr('id')}Width`, rightWidthInfo);
  80. }
  81. });
  82. }
  83. function loadHorizonWidth(module, resizes, eles, callback) {
  84. for (let ele of eles) {
  85. let cache = getLocalCache(`${module}${ele.attr('id')}Width`);
  86. if (cache) {
  87. ele.css('width', cache);
  88. }
  89. }
  90. for (let resize of resizes) {
  91. setResizeWidth(resize);
  92. }
  93. if (callback) {
  94. callback();
  95. }
  96. }
  97. /*
  98. * div上下拖动
  99. * module: 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  100. * eleObj: resize, top, topSpread, bottom, bottomSpread
  101. * limit: min, max, notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  102. * */
  103. function verticalSlide(module, eleObj, limit, callback) {
  104. const triggerCBSize = 5;
  105. let drag = false,
  106. startPoint = 0,
  107. topHeight = 0,
  108. bottomHeight = 0,
  109. topChange = 0,
  110. bottomChange = 0,
  111. limitMax = 0;
  112. eleObj.resize.mousedown(function (e) {
  113. drag = true;
  114. startPoint = e.clientY;
  115. topHeight = eleObj.top.height();
  116. bottomHeight = eleObj.bottom.height();
  117. limitMax = eval(limit.max);
  118. });
  119. $('body').mousemove(function (e) {
  120. if (drag) {
  121. let moveSize = e.clientY - startPoint;
  122. topChange = topHeight + moveSize;
  123. topChange = topChange < limit.min ? limit.min : topChange;
  124. topChange = topChange > limitMax ? limitMax - 3 : topChange;
  125. bottomChange = bottomHeight - moveSize;
  126. bottomChange = bottomChange < limit.min ? limit.min : bottomChange;
  127. bottomChange = bottomChange > limitMax ? limitMax - 3 : bottomChange;
  128. //设置上部分div高度
  129. eleObj.top.height(topChange);
  130. //设置上部分div内spread高度
  131. eleObj.topSpread.height(topChange - limit.notTopSpread);
  132. //设置下部分div高度
  133. eleObj.bottom.height(bottomChange);
  134. //设置下部分div内spread高度
  135. eleObj.bottomSpread.height(bottomChange - limit.notBottomSpread);
  136. mouseMoveCount += Math.abs(moveSize);
  137. if (mouseMoveCount > triggerCBSize && callback) {
  138. callback();
  139. mouseMoveCount = 0;
  140. }
  141. }
  142. });
  143. $('body').mouseup(function (e) {
  144. if (drag) {
  145. drag = false;
  146. mouseMoveCount = 0;
  147. //将高度信息存储到localstorage
  148. let topHeightInfo = eleObj.top.height();
  149. setLocalCache(`${module}${eleObj.top.attr('id')}Height`, topHeightInfo);
  150. let bottomHeightInfo = eleObj.bottom.height();
  151. setLocalCache(`${module}${eleObj.bottom.attr('id')}Height`, bottomHeightInfo);
  152. }
  153. });
  154. }
  155. /*
  156. * 加载上下高度
  157. * module: 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  158. * eleObj: top, topSpread, bottom, bottomSpread
  159. * limit: totalHeight(实时的上下部分总高度) notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  160. * */
  161. function loadVerticalHeight(module, eleObj, limit, callback) {
  162. let topHeight = getLocalCache(`${module}${eleObj.top.attr('id')}Height`),
  163. bottomHeight = getLocalCache(`${module}${eleObj.bottom.attr('id')}Height`);
  164. //默认上下比例
  165. const topProp = 5;
  166. const bottomProp = 2;
  167. let topProportion = topProp / (topProp + bottomProp);
  168. if (topHeight !== null && bottomHeight !== null) {
  169. topHeight = parseFloat(topHeight);
  170. bottomHeight = parseFloat(bottomHeight);
  171. topProportion = topHeight / (topHeight + bottomHeight);
  172. }
  173. //设置当前窗口下的的上下部分高度
  174. let totalHeight = eval(limit.totalHeight);
  175. let curTopHeight = totalHeight * topProportion,
  176. curBottomHeight = totalHeight - curTopHeight;
  177. eleObj.top.height(curTopHeight);
  178. eleObj.topSpread.height(curTopHeight - limit.notTopSpread);
  179. eleObj.bottom.height(curBottomHeight);
  180. eleObj.bottomSpread.height(curBottomHeight - limit.notBottomSpread);
  181. if (callback) {
  182. callback();
  183. }
  184. }
  185. return {horizontalSlide, loadHorizonWidth, verticalSlide, loadVerticalHeight}
  186. })();