slideResize.js 11 KB

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