slideResize.js 12 KB

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