slideResize.js 12 KB

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