slideResize.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. console.log('drag');
  59. let moveSize = e.clientX - startPoint;
  60. leftChange = leftWidth + moveSize;
  61. leftChange = leftChange < limit.min ? limit.min : leftChange;
  62. leftChange = leftChange > limitMax ? limitMax - 5 : leftChange;
  63. rightChange = rightWidth - moveSize;
  64. rightChange = rightChange < limit.min ? limit.min : rightChange;
  65. rightChange = rightChange > limitMax ? limitMax - 5 : rightChange;
  66. let leftPercentWidth = leftChange / eleObj.parent.width() * 100 + '%',
  67. rightPercentWidth = rightChange / eleObj.parent.width() * 100 + '%';
  68. eleObj.left.css('width', leftPercentWidth);
  69. eleObj.right.css('width', rightPercentWidth);
  70. setResizeWidth(eleObj.resize);
  71. mouseMoveCount += Math.abs(moveSize);
  72. if (mouseMoveCount > triggerCBSize && callback) {
  73. callback();
  74. mouseMoveCount = 0;
  75. }
  76. }
  77. });
  78. $('body').mouseup(function (e) {
  79. if (drag) {
  80. drag = false;
  81. mouseMoveCount = 0;
  82. //将宽度信息存储到localstorage
  83. let leftWidthInfo = eleObj.left[0].style.width;
  84. setLocalCache(`${eleObj.module}${eleObj.left.attr('id')}Width`, leftWidthInfo);
  85. let rightWidthInfo = eleObj.right[0].style.width;
  86. setLocalCache(`${eleObj.module}${eleObj.right.attr('id')}Width`, rightWidthInfo);
  87. }
  88. });
  89. }
  90. function loadHorizonWidth(module, resizes, eles, callback) {
  91. for (let ele of eles) {
  92. let cache = getLocalCache(`${module}${ele.attr('id')}Width`);
  93. if (cache) {
  94. ele.css('width', cache);
  95. }
  96. }
  97. for (let resize of resizes) {
  98. setResizeWidth(resize);
  99. }
  100. if (callback) {
  101. callback();
  102. }
  103. }
  104. /*
  105. * div上下拖动
  106. * @param {Object} eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module)
  107. * resize, top, topSpread, bottom, bottomSpread
  108. * @param {Object}limit: min, max, notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  109. * @param {Function}callback 回调
  110. * */
  111. function verticalSlide(eleObj, limit, callback) {
  112. const triggerCBSize = 5;
  113. let drag = false,
  114. startPoint = 0,
  115. topHeight = 0,
  116. bottomHeight = 0,
  117. topChange = 0,
  118. bottomChange = 0,
  119. limitMax = 0;
  120. eleObj.resize.mousedown(function (e) {
  121. drag = true;
  122. startPoint = e.clientY;
  123. topHeight = eleObj.top.height();
  124. bottomHeight = eleObj.bottom.height();
  125. limitMax = eval(limit.max);
  126. });
  127. $('body').mousemove(function (e) {
  128. if (drag) {
  129. let moveSize = e.clientY - startPoint;
  130. topChange = topHeight + moveSize;
  131. topChange = topChange < limit.min ? limit.min : topChange;
  132. topChange = topChange > limitMax ? limitMax : topChange;
  133. bottomChange = bottomHeight - moveSize;
  134. bottomChange = bottomChange < limit.min ? limit.min : bottomChange;
  135. bottomChange = bottomChange > limitMax ? limitMax : bottomChange;
  136. //设置上部分div高度
  137. eleObj.top.height(topChange);
  138. //设置上部分div内spread高度
  139. eleObj.topSpread.height(topChange - limit.notTopSpread);
  140. //设置下部分div高度
  141. eleObj.bottom.height(bottomChange);
  142. //设置下部分div内spread高度
  143. eleObj.bottomSpread.height(bottomChange - limit.notBottomSpread);
  144. mouseMoveCount += Math.abs(moveSize);
  145. if (mouseMoveCount > triggerCBSize && callback) {
  146. callback();
  147. mouseMoveCount = 0;
  148. }
  149. }
  150. });
  151. $('body').mouseup(function (e) {
  152. if (drag) {
  153. drag = false;
  154. mouseMoveCount = 0;
  155. //将高度信息存储到localstorage
  156. let topHeightInfo = eleObj.top.height();
  157. setLocalCache(`${eleObj.module}${eleObj.top.attr('id')}Height`, topHeightInfo);
  158. let bottomHeightInfo = eleObj.bottom.height();
  159. setLocalCache(`${eleObj.module}${eleObj.bottom.attr('id')}Height`, bottomHeightInfo);
  160. }
  161. });
  162. }
  163. /*
  164. * 加载上下高度
  165. * @param {String}module 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  166. * @param {Object}eleObj: module所属模块,防止不同页面相同id导致localstorage数据被覆盖(放在对象里,因为可能有的地方要实时改这个module) top, topSpread, bottom, bottomSpread
  167. * @param {Object}limit: totalHeight(实时的上下部分总高度) notTopSpread(上部分非spread部分的高度) notBottomSpread(下部分非spread部分的高度)
  168. * @param {Function}callback 回调
  169. * */
  170. function loadVerticalHeight(module, eleObj, limit, callback) {
  171. let topHeight = getLocalCache(`${module}${eleObj.top.attr('id')}Height`),
  172. bottomHeight = getLocalCache(`${module}${eleObj.bottom.attr('id')}Height`);
  173. //默认上下比例
  174. const topProp = 5;
  175. const bottomProp = 2;
  176. let topProportion = topProp / (topProp + bottomProp);
  177. if (topHeight !== null && bottomHeight !== null) {
  178. topHeight = parseFloat(topHeight);
  179. bottomHeight = parseFloat(bottomHeight);
  180. topProportion = topHeight / (topHeight + bottomHeight);
  181. }
  182. //设置当前窗口下的的上下部分高度
  183. let totalHeight = eval(limit.totalHeight);
  184. let curTopHeight = totalHeight * topProportion,
  185. curBottomHeight = totalHeight - curTopHeight;
  186. eleObj.top.height(curTopHeight);
  187. eleObj.topSpread.height(curTopHeight - limit.notTopSpread);
  188. eleObj.bottom.height(curBottomHeight);
  189. eleObj.bottomSpread.height(curBottomHeight - limit.notBottomSpread);
  190. if (callback) {
  191. callback();
  192. }
  193. }
  194. /*
  195. * 包含多个上下结构的拖动(n>= 2,只包含上下结构的地方可以用loadVerticalHeight)
  196. * 加载多个结构高度
  197. * @param {String}module所属模块,防止不同页面相同id导致localstorage数据被覆盖
  198. * @param {Array}eles 元素为结构对象 内部: {container: 外部div, spread: 该div的Spread, notSpread: 该div不含Spread的高度, defaultProportion: 默认高度比例0.6}
  199. * @param {String}totalHeight 各个结构div总高度-可编译字符串
  200. * @param {Function}callback 回调
  201. * */
  202. function loadMultiVerticalHeight(module, eles, totalHeight, callback) {
  203. //当前窗口拖动区域总高度
  204. totalHeight = eval(totalHeight);
  205. let cacheHeight = 0,//缓存中设置了高度的div总高度
  206. notCacheProportion = 0;//缓存中没设置高度的div占用的总高度比例(每个div都有默认的高度比例) (有了缓存高度的div就是被拖动过的,被拖动过的div,其默认高度比例就没用了)
  207. //获取缓存的总高度、未被拖动过的所有div的默认比例之和notCacheProportion (1-notCacheProportion)就是缓存高度的占总的比例
  208. for (let ele of eles) {
  209. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`);
  210. if (eleHeight !== null) {
  211. eleHeight = parseFloat(eleHeight);
  212. cacheHeight += eleHeight;
  213. } else {
  214. notCacheProportion += ele.defaultProportion;
  215. }
  216. }
  217. //缓存高度占总的比例
  218. let cacheProportion = 1 - notCacheProportion;
  219. for (let ele of eles) {
  220. let eleHeight = getLocalCache(`${module}${ele.container.attr('id')}Height`),
  221. curHeight = 0;
  222. if (eleHeight !== null) {
  223. eleHeight = parseFloat(eleHeight);
  224. //ele在当前窗口大小中的高度
  225. curHeight = eleHeight / cacheHeight * cacheProportion * totalHeight;
  226. } else {
  227. curHeight = ele.defaultProportion * totalHeight;
  228. }
  229. ele.container.height(curHeight);
  230. ele.spread.height(curHeight - ele.notSpread);
  231. }
  232. if (callback) {
  233. callback();
  234. }
  235. }
  236. return {setResizeWidth, horizontalSlide, loadHorizonWidth, verticalSlide, loadVerticalHeight, loadMultiVerticalHeight}
  237. })();