slideResize.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/10/22
  7. * @version
  8. */
  9. let mouseMoveCount = 0;
  10. /**
  11. * 拖动更改div大小
  12. *
  13. * @param {Object} eles - id:存储本地的标记 resize:拖动条 nearElement:左上外层div nearSpread:左上spread farElement:右下外层div evFixedSize:造价书左右拖动用
  14. * @param {Object} limit - min/max
  15. * @param {String} type - height/width
  16. * @param {function} callback - 成功后执行
  17. * @return {void}
  18. */
  19. function slideResize(eles, limit, type, callback) {
  20. if(type !== 'height' && type !== 'width'){
  21. return;
  22. }
  23. //nearElement:左上, farElement:右下
  24. let startP = 0;
  25. let drag = false;
  26. const resizeElement = eles.resize;
  27. const nElement = eles.nearElement;
  28. const fElement = eles.farElement;
  29. const navContentEle = eles.nav ? eles.nav : null;
  30. let nEleSize = 0;
  31. let fEleSize = 0;
  32. let navSize = 0;
  33. let nEleChangeSize = 0;
  34. let fEleChangeSize = 0;
  35. let limitMax = 0;
  36. // 鼠标点下时
  37. resizeElement.mousedown(function(e) {
  38. limitMax = eval(limit.max);
  39. mouseMoveCount = 0;
  40. drag = true;
  41. startP = type === 'height' ? e.clientY : e.clientX;
  42. // 获取左(上)部分的宽度(高度)
  43. nEleSize = nElement[type]();
  44. // 获取右(下)部分的宽度(高度)
  45. fEleSize = fElement[type]();
  46. // nav宽(高度)部分
  47. if(navContentEle){
  48. navSize = navContentEle[type]() + 4;
  49. }
  50. resizeElement.tooltip('hide');
  51. });
  52. // 鼠标移动
  53. $("body").mousemove(function(e) {
  54. if (drag) {
  55. let moveSize = type === 'height' ? e.clientY - startP : e.clientX - startP;
  56. // 判断拖动范围不能超出
  57. nEleChangeSize = nEleSize + moveSize;
  58. nEleChangeSize = nEleChangeSize < limit.min ? limit.min : nEleChangeSize;
  59. nEleChangeSize = nEleChangeSize > limitMax ? limitMax + 9 : nEleChangeSize;
  60. fEleChangeSize = fEleSize - moveSize;
  61. fEleChangeSize = fEleChangeSize < limit.min ? limit.min : fEleChangeSize;
  62. fEleChangeSize = fEleChangeSize > limitMax ? limitMax + 9 : fEleChangeSize;
  63. if(type === 'width'){
  64. if(eles.totalWidth) {
  65. nEleChangeSize = nEleChangeSize * eles.totalWidth;
  66. fEleChangeSize = fEleChangeSize * eles.totalWidth;
  67. }
  68. let rePercent = getResizeWidthPercent(nEleChangeSize, fEleChangeSize, eles.totalWidth);
  69. eles.nearElement.css(type, rePercent.nearPercent);
  70. eles.farElement.css(type, rePercent.farPercent);
  71. }
  72. else{
  73. eles.nearSpread[type](nEleChangeSize);
  74. eles.farSpread[type](fEleChangeSize - navSize);
  75. eles.farElement[type](fEleChangeSize);
  76. }
  77. //实时刷新页面
  78. mouseMoveCount+=Math.abs(moveSize);//取移动的决对值
  79. if(mouseMoveCount >=5){//当累计移动超过5个像素时,才刷新,减少刷新次数
  80. if(callback) callback();
  81. mouseMoveCount = 0;
  82. }
  83. }
  84. });
  85. // 鼠标弹起
  86. $("body").mouseup(function(e) {
  87. if (drag) {
  88. callback();
  89. drag = false;
  90. // 存入本地缓存
  91. const id = eles.id;
  92. nEleChangeSize = nEleChangeSize >= limit.max ? limit.max + 9 : nEleChangeSize;
  93. fEleChangeSize = fEleChangeSize >= limit.max ? limit.max + 9 : fEleChangeSize;
  94. setLocalCache(`near${type}:${id}`, nEleChangeSize);
  95. setLocalCache(`far${type}:${id}`, fEleChangeSize);
  96. }
  97. });
  98. }
  99. /**
  100. * 读取设置的高度
  101. *
  102. * @param {String} tag - 顶层div的id
  103. * @param {function} callback - 回调函数
  104. * @return {void}
  105. */
  106. function loadSize(eles, type, callback) {
  107. let tag = eles.id;
  108. if (tag === '') {
  109. return;
  110. }
  111. if(type !== 'height' && type !== 'width'){
  112. return;
  113. }
  114. let o_nearSize = eles.nearSpread[type]();
  115. let o_farSize = eles.farSpread[type]();
  116. let nearSize = getLocalCache(`near${type}:${tag}`);
  117. let farSize = getLocalCache(`far${type}:${tag}`);
  118. if (nearSize === null || farSize === null) {
  119. setDefaultSize(tag,eles,type);//zhang 2018-05-21
  120. /* eles.nearSpread[type](o_nearSize);
  121. eles.farSpread[type](o_farSize);*/
  122. }else {
  123. setSizeWithPercent(tag,eles,nearSize,farSize,type)//zhang 2018-06-04 改成按百分比设置
  124. }
  125. if(type === 'width'){//使用百分比
  126. if (eles.totalWidth) {
  127. o_nearSize = o_nearSize * eles.totalWidth;
  128. o_farSize = o_farSize * eles.totalWidth;
  129. }
  130. let rePercent = getResizeWidthPercent(nearSize ? nearSize : o_nearSize, farSize ? farSize : o_farSize, eles.totalWidth);
  131. eles.nearElement.css(type, rePercent.nearPercent);
  132. eles.farElement.css(type, rePercent.farPercent);
  133. }
  134. callback();
  135. }
  136. function getResizeWidthPercent(nearSize, farSize, totalWidth = 1){
  137. const resizeWidth = 6;
  138. nearSize = parseFloat(nearSize);
  139. farSize = parseFloat(farSize);
  140. let nearPercent = (nearSize / (resizeWidth + nearSize + farSize) * totalWidth * 100) + '%';
  141. let farPercent = (farSize / (resizeWidth + nearSize + farSize) * totalWidth * 100) + '%';
  142. return {nearPercent, farPercent};
  143. }
  144. function setSizeWithPercent(tag,eles,nearSize,farSize,type) {
  145. nearSize = parseFloat(nearSize);
  146. farSize = parseFloat(farSize);
  147. if(type !== 'width') {
  148. let headerHeight = $(".header").height();
  149. let toolsbarHeight = $(".toolsbar").height() ? $(".toolsbar").height() : 0;
  150. let resizeHeight = 12;
  151. let exand = tag == "#main" ? 1:50;
  152. if (tag === '#stdRationSub') {
  153. exand = 35;
  154. } else if (tag === '#stdZmhsAdj'){
  155. exand = 0;
  156. }
  157. let totalHeight = $(window).height() - headerHeight - toolsbarHeight - exand - resizeHeight;
  158. const navSize = eles.nav ? eles.nav[type]() + 4 : 0;
  159. totalHeight = totalHeight - navSize;
  160. nearSize = (nearSize/(nearSize + farSize))* totalHeight;
  161. eles.nearSpread[type](nearSize);
  162. eles.farSpread[type](totalHeight - nearSize);
  163. eles.farElement[type](totalHeight - nearSize + navSize);
  164. }
  165. }
  166. function setDefaultSize(tag,eles,type) {
  167. let o_nearSize = 5;
  168. let o_farSize = 2;
  169. if(type == 'height'){
  170. let headerHeight = $(".header").height();
  171. let toolsbarHeight = $(".toolsbar").height();
  172. let exand = tag == "#main" ? 1:50;
  173. let resizeHeight = 12;
  174. let totalHeight = $(window).height() - headerHeight - toolsbarHeight-exand - resizeHeight;
  175. const navSize = eles.nav ? eles.nav[type]() + 4 : 0;
  176. totalHeight = totalHeight - navSize;
  177. let nearSize = (o_nearSize/(o_nearSize + o_farSize))* totalHeight;
  178. eles.nearSpread[type](nearSize);
  179. eles.farSpread[type](totalHeight - nearSize);
  180. eles.farElement[type](totalHeight - nearSize + navSize);
  181. }
  182. }
  183. /*
  184. * div之间的水平拖动,适应各种情况
  185. * module: 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  186. * eleObj: resize, parent, left, right
  187. * limit: min, max
  188. * */
  189. function horizontalSlide(module, eleObj, limit, callback) {
  190. const triggerCBSize = 5;
  191. let drag = false,
  192. startPoint = 0,
  193. leftWidth = 0,
  194. rightWidth = 0,
  195. leftChange = 0,
  196. rightChange = 0,
  197. limitMax = 0;
  198. eleObj.resize.mousedown(function (e) {
  199. drag = true;
  200. startPoint = e.clientX;
  201. leftWidth = eleObj.left.width();
  202. rightWidth = eleObj.right.width();
  203. limitMax = eval(limit.max);
  204. });
  205. $('body').mousemove(function (e) {
  206. if (drag) {
  207. let moveSize = e.clientX - startPoint;
  208. leftChange = leftWidth + moveSize;
  209. leftChange = leftChange < limit.min ? limit.min : leftChange;
  210. leftChange = leftChange > limitMax ? limitMax - 3 : leftChange;
  211. rightChange = rightWidth - moveSize;
  212. rightChange = rightChange < limit.min ? limit.min : rightChange;
  213. rightChange = rightChange > limitMax ? limitMax - 3 : rightChange;
  214. let leftPercentWidth = leftChange / eleObj.parent.width() * 100 + '%',
  215. rightPercentWidth = rightChange / eleObj.parent.width() * 100 + '%';
  216. eleObj.left.css('width', leftPercentWidth);
  217. eleObj.right.css('width', rightPercentWidth);
  218. mouseMoveCount += Math.abs(moveSize);
  219. if (mouseMoveCount > triggerCBSize && callback) {
  220. callback();
  221. mouseMoveCount = 0;
  222. }
  223. }
  224. });
  225. $('body').mouseup(function (e) {
  226. if (drag) {
  227. drag = false;
  228. mouseMoveCount = 0;
  229. //将宽度信息存储到localstorage
  230. let leftWidthInfo = eleObj.left[0].style.width;
  231. setLocalCache(`${module}${eleObj.left.attr('id')}Width`, leftWidthInfo);
  232. let rightWidthInfo = eleObj.right[0].style.width;
  233. setLocalCache(`${module}${eleObj.right.attr('id')}Width`, rightWidthInfo);
  234. }
  235. });
  236. }
  237. function loadHorizonWidth(module, eles, callback) {
  238. for (let ele of eles) {
  239. let cache = getLocalCache(`${module}${ele.attr('id')}Width`);
  240. if (cache) {
  241. ele.css('width', cache);
  242. }
  243. }
  244. if (callback) {
  245. callback();
  246. }
  247. }