slideResize.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/11/7
  7. * @version
  8. */
  9. /*
  10. * 拖动相关公共接口
  11. * 前台使用此接口时,注意在恢复系统默认设置时,
  12. * 清除相关缓存数据: project_view.js--->$('#property_default').click(callback)
  13. * */
  14. const SlideResize = (function() {
  15. //设置水平拖动条的宽度
  16. //@param {Object dom}resize滚动条
  17. function setResizeWidth (resize) {
  18. const fixedWidth = 10;
  19. //滚动条节点 及 同层非滚动条节点的索引
  20. let bros = resize.parent().children();
  21. let index = bros.index(resize),
  22. otherIndex = index ? 0 : 1;
  23. const other = resize.parent().children(`:eq(${otherIndex})`);
  24. let resizeParentWidth = resize.parent().width();
  25. let resizeDecimalWidth = fixedWidth / resizeParentWidth,
  26. otherDecimalWidth = 1 - resizeDecimalWidth;
  27. let resizePercentWidth = resizeDecimalWidth * 100 + '%',
  28. otherPercentWidth = otherDecimalWidth * 100 + '%';
  29. resize.css('width', resizePercentWidth);
  30. other.css('width', otherPercentWidth);
  31. }
  32. let mouseMoveCount = 0;
  33. /*
  34. * div之间的水平拖动,适应各种情况
  35. * @param {Object}eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module)
  36. * resize, parent, left, right
  37. * @param {Object}limit: min, max
  38. * @param {Function}callback 回调
  39. * */
  40. function horizontalSlide(eleObj, limit, callback) {
  41. const triggerCBSize = 5;
  42. let drag = false,
  43. startPoint = 0,
  44. leftWidth = 0,
  45. rightWidth = 0,
  46. leftChange = 0,
  47. rightChange = 0,
  48. limitMax = 0;
  49. eleObj.resize.mousedown(function (e) {
  50. drag = true;
  51. startPoint = e.clientX;
  52. leftWidth = eleObj.left.width();
  53. rightWidth = eleObj.right.width();
  54. limitMax = eval(limit.max);
  55. });
  56. $('body').mousemove(function (e) {
  57. if (drag) {
  58. let moveSize = e.clientX - startPoint;
  59. leftChange = leftWidth + moveSize;
  60. leftChange = leftChange < limit.min ? limit.min : leftChange;
  61. leftChange = leftChange > limitMax ? limitMax - 5 : leftChange;
  62. rightChange = rightWidth - moveSize;
  63. rightChange = rightChange < limit.min ? limit.min : rightChange;
  64. rightChange = rightChange > limitMax ? limitMax - 5 : rightChange;
  65. let leftPercentWidth = leftChange / eleObj.parent.width() * 100 + '%',
  66. rightPercentWidth = rightChange / eleObj.parent.width() * 100 + '%';
  67. eleObj.left.css('width', leftPercentWidth);
  68. eleObj.right.css('width', rightPercentWidth);
  69. setResizeWidth(eleObj.resize);
  70. mouseMoveCount += Math.abs(moveSize);
  71. if (mouseMoveCount > triggerCBSize && callback) {
  72. callback();
  73. mouseMoveCount = 0;
  74. }
  75. }
  76. });
  77. $('body').mouseup(function (e) {
  78. if (drag) {
  79. drag = false;
  80. mouseMoveCount = 0;
  81. //将宽度信息存储到localstorage
  82. let leftWidthInfo = eleObj.left[0].style.width;
  83. setLocalCache(`${eleObj.module}${eleObj.left.attr('id')}Width`, leftWidthInfo);
  84. let rightWidthInfo = eleObj.right[0].style.width;
  85. setLocalCache(`${eleObj.module}${eleObj.right.attr('id')}Width`, rightWidthInfo);
  86. }
  87. });
  88. }
  89. function loadHorizonWidth(module, resizes, eles, callback) {
  90. for (let ele of eles) {
  91. let cache = getLocalCache(`${module}${ele.attr('id')}Width`);
  92. if (cache) {
  93. ele.css('width', cache);
  94. }
  95. }
  96. for (let resize of resizes) {
  97. setResizeWidth(resize);
  98. }
  99. if (callback) {
  100. callback();
  101. }
  102. }
  103. /*
  104. * div上下拖动
  105. * @param {Object} eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module)
  106. * resize, top, topSpread, bottom, bottomSpread
  107. * @param {Object}limit: min, max, notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  108. * @param {Function}callback 回调
  109. * */
  110. function verticalSlide(eleObj, limit, callback) {
  111. const triggerCBSize = 5;
  112. let drag = false,
  113. startPoint = 0,
  114. topHeight = 0,
  115. bottomHeight = 0,
  116. topChange = 0,
  117. bottomChange = 0,
  118. limitMax = 0;
  119. eleObj.resize.mousedown(function (e) {
  120. drag = true;
  121. startPoint = e.clientY;
  122. topHeight = eleObj.top.height();
  123. bottomHeight = eleObj.bottom.height();
  124. limitMax = eval(limit.max);
  125. });
  126. $('body').mousemove(function (e) {
  127. if (drag) {
  128. let moveSize = e.clientY - startPoint;
  129. topChange = topHeight + moveSize;
  130. topChange = topChange < limit.min ? limit.min : topChange;
  131. topChange = topChange > limitMax ? limitMax : topChange;
  132. bottomChange = bottomHeight - moveSize;
  133. bottomChange = bottomChange < limit.min ? limit.min : bottomChange;
  134. bottomChange = bottomChange > limitMax ? limitMax : bottomChange;
  135. //设置上部分div高度
  136. eleObj.top.height(topChange);
  137. //设置上部分div内spread高度
  138. eleObj.topSpread.height(topChange - limit.notTopSpread);
  139. //设置下部分div高度
  140. eleObj.bottom.height(bottomChange);
  141. //设置下部分div内spread高度
  142. eleObj.bottomSpread.height(bottomChange - limit.notBottomSpread);
  143. mouseMoveCount += Math.abs(moveSize);
  144. if (mouseMoveCount > triggerCBSize && callback) {
  145. callback();
  146. mouseMoveCount = 0;
  147. }
  148. }
  149. });
  150. $('body').mouseup(function (e) {
  151. if (drag) {
  152. drag = false;
  153. mouseMoveCount = 0;
  154. //将高度信息存储到localstorage
  155. let topHeightInfo = eleObj.top.height();
  156. setLocalCache(`${eleObj.module}${eleObj.top.attr('id')}Height`, topHeightInfo);
  157. let bottomHeightInfo = eleObj.bottom.height();
  158. setLocalCache(`${eleObj.module}${eleObj.bottom.attr('id')}Height`, bottomHeightInfo);
  159. }
  160. });
  161. }
  162. /*
  163. * 加载上下高度
  164. * @param {String}module 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  165. * @param {Object}eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module) top, topSpread, bottom, bottomSpread
  166. * @param {Object}limit: totalHeight(实时的上下部分总高度) notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  167. * @param {Function}callback 回调
  168. * */
  169. function loadVerticalHeight(module, eleObj, limit, callback) {
  170. let topHeight = getLocalCache(`${module}${eleObj.top.attr('id')}Height`),
  171. bottomHeight = getLocalCache(`${module}${eleObj.bottom.attr('id')}Height`);
  172. //默认上下比例
  173. const topProp = 5;
  174. const bottomProp = 2;
  175. let topProportion = topProp / (topProp + bottomProp);
  176. if (topHeight !== null && bottomHeight !== null) {
  177. topHeight = parseFloat(topHeight);
  178. bottomHeight = parseFloat(bottomHeight);
  179. topProportion = topHeight / (topHeight + bottomHeight);
  180. }
  181. //设置当前窗口下的的上下部分高度
  182. let totalHeight = eval(limit.totalHeight);
  183. let curTopHeight = totalHeight * topProportion,
  184. curBottomHeight = totalHeight - curTopHeight;
  185. eleObj.top.height(curTopHeight);
  186. eleObj.topSpread.height(curTopHeight - limit.notTopSpread);
  187. eleObj.bottom.height(curBottomHeight);
  188. eleObj.bottomSpread.height(curBottomHeight - limit.notBottomSpread);
  189. if (callback) {
  190. callback();
  191. }
  192. }
  193. /*
  194. * 包含多个上下结构的拖动(n>= 2,只包含上下结构的地方可以用loadVerticalHeight)
  195. * 加载多个结构高度
  196. * @param {String}module所属模块,防止不同页面相同id导致localstorage数据被覆盖
  197. * @param {Array}eles 元素为结构对象 内部: {container: 外部div, spread: 该div的Spread, notSpread: 该div不含Spread的高度, defaultProportion: 默认高度比例0.6}
  198. * @param {String}totalHeight 各个结构div总高度-可编译字符串
  199. * @param {Function}callback 回调
  200. * */
  201. function loadMultiVerticalHeight(module, eles, totalHeight, callback) {
  202. //当前窗口拖动区域总高度
  203. totalHeight = eval(totalHeight);
  204. let cacheHeight = 0,//缓存中设置了高度的div总高度
  205. notCacheProportion = 0;//缓存中没设置高度的div占用的总高度比例(每个div都有默认的高度比例) (有了缓存高度的div就是被拖动过的,被拖动过的div,其默认高度比例就没用了)
  206. //获取缓存的总高度、未被拖动过的所有div的默认比例之和notCacheProportion (1-notCacheProportion)就是缓存高度的占总的比例
  207. for (let ele of eles) {
  208. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`);
  209. if (eleHeight !== null) {
  210. eleHeight = parseFloat(eleHeight);
  211. cacheHeight += eleHeight;
  212. } else {
  213. notCacheProportion += ele.defaultProportion;
  214. }
  215. }
  216. //缓存高度占总的比例
  217. let cacheProportion = 1 - notCacheProportion;
  218. for (let ele of eles) {
  219. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`),
  220. curHeight = 0;
  221. if (eleHeight !== null) {
  222. eleHeight = parseFloat(eleHeight);
  223. //ele在当前窗口大小中的高度
  224. curHeight = eleHeight / cacheHeight * cacheProportion * totalHeight;
  225. } else {
  226. curHeight = ele.defaultProportion * totalHeight;
  227. }
  228. ele.container.height(curHeight);
  229. ele.spread.height(curHeight - ele.notSpread);
  230. }
  231. if (callback) {
  232. callback();
  233. }
  234. }
  235. return {setResizeWidth, horizontalSlide, loadHorizonWidth, verticalSlide, loadVerticalHeight, loadMultiVerticalHeight}
  236. })();