slideResize.js 7.8 KB

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