slideResize.js 11 KB

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