slideResize.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // 鼠标点下时
  36. resizeElement.mousedown(function(e) {
  37. mouseMoveCount = 0;
  38. drag = true;
  39. startP = type === 'height' ? e.clientY : e.clientX;
  40. // 获取左(上)部分的宽度(高度)
  41. nEleSize = nElement[type]();
  42. // 获取右(下)部分的宽度(高度)
  43. fEleSize = fElement[type]();
  44. // nav宽(高度)部分
  45. if(navContentEle){
  46. navSize = navContentEle[type]() + 4;
  47. }
  48. resizeElement.tooltip('hide');
  49. });
  50. // 鼠标移动
  51. $("body").mousemove(function(e) {
  52. if (drag) {
  53. let moveSize = type === 'height' ? e.clientY - startP : e.clientX - startP;
  54. // 判断拖动范围不能超出
  55. nEleChangeSize = nEleSize + moveSize;
  56. nEleChangeSize = nEleChangeSize < limit.min ? limit.min : nEleChangeSize;
  57. nEleChangeSize = nEleChangeSize > limit.max ? limit.max + 9 : nEleChangeSize;
  58. fEleChangeSize = fEleSize - moveSize;
  59. fEleChangeSize = fEleChangeSize < limit.min ? limit.min : fEleChangeSize;
  60. fEleChangeSize = fEleChangeSize > limit.max ? limit.max + 9 : fEleChangeSize;
  61. if(type === 'width'){
  62. if(eles.totalWidth) {
  63. nEleChangeSize = nEleChangeSize * eles.totalWidth;
  64. fEleChangeSize = fEleChangeSize * eles.totalWidth;
  65. }
  66. let rePercent = getResizeWidthPercent(nEleChangeSize, fEleChangeSize, eles.totalWidth);
  67. eles.nearElement.css(type, rePercent.nearPercent);
  68. eles.farElement.css(type, rePercent.farPercent);
  69. }
  70. else{
  71. eles.nearSpread[type](nEleChangeSize);
  72. eles.farSpread[type](fEleChangeSize - navSize);
  73. eles.farElement[type](fEleChangeSize);
  74. }
  75. //实时刷新页面
  76. mouseMoveCount+=Math.abs(moveSize);//取移动的决对值
  77. if(mouseMoveCount >=5){//当累计移动超过5个像素时,才刷新,减少刷新次数
  78. if(callback) callback();
  79. mouseMoveCount = 0;
  80. }
  81. }
  82. });
  83. // 鼠标弹起
  84. $("body").mouseup(function(e) {
  85. if (drag) {
  86. callback();
  87. drag = false;
  88. // 存入本地缓存
  89. const id = eles.id;
  90. nEleChangeSize = nEleChangeSize >= limit.max ? limit.max + 9 : nEleChangeSize;
  91. fEleChangeSize = fEleChangeSize >= limit.max ? limit.max + 9 : fEleChangeSize;
  92. setLocalCache(`near${type}:${id}`, nEleChangeSize);
  93. setLocalCache(`far${type}:${id}`, fEleChangeSize);
  94. }
  95. });
  96. }
  97. /**
  98. * 读取设置的高度
  99. *
  100. * @param {String} tag - 顶层div的id
  101. * @param {function} callback - 回调函数
  102. * @return {void}
  103. */
  104. function loadSize(eles, type, callback) {
  105. let tag = eles.id;
  106. if (tag === '') {
  107. return;
  108. }
  109. if(type !== 'height' && type !== 'width'){
  110. return;
  111. }
  112. let o_nearSize = eles.nearSpread[type]();
  113. let o_farSize = eles.farSpread[type]();
  114. let nearSize = getLocalCache(`near${type}:${tag}`);
  115. let farSize = getLocalCache(`far${type}:${tag}`);
  116. if (nearSize === null || farSize === null) {
  117. setDefaultSize(tag,eles,type);//zhang 2018-05-21
  118. /* eles.nearSpread[type](o_nearSize);
  119. eles.farSpread[type](o_farSize);*/
  120. }else {
  121. setSizeWithPercent(tag,eles,nearSize,farSize,type)//zhang 2018-06-04 改成按百分比设置
  122. }
  123. if(type === 'width'){//使用百分比
  124. if (eles.totalWidth) {
  125. o_nearSize = o_nearSize * eles.totalWidth;
  126. o_farSize = o_farSize * eles.totalWidth;
  127. }
  128. let rePercent = getResizeWidthPercent(nearSize ? nearSize : o_nearSize, farSize ? farSize : o_farSize, eles.totalWidth);
  129. eles.nearElement.css(type, rePercent.nearPercent);
  130. eles.farElement.css(type, rePercent.farPercent);
  131. }
  132. callback();
  133. }
  134. function getResizeWidthPercent(nearSize, farSize, totalWidth = 1){
  135. const resizeWidth = 6;
  136. nearSize = parseFloat(nearSize);
  137. farSize = parseFloat(farSize);
  138. let nearPercent = (nearSize / (resizeWidth + nearSize + farSize) * totalWidth * 100) + '%';
  139. let farPercent = (farSize / (resizeWidth + nearSize + farSize) * totalWidth * 100) + '%';
  140. return {nearPercent, farPercent};
  141. }
  142. function setSizeWithPercent(tag,eles,nearSize,farSize,type) {
  143. nearSize = parseFloat(nearSize);
  144. farSize = parseFloat(farSize);
  145. if(type !== 'width') {
  146. let headerHeight = $(".header").height();
  147. let toolsbarHeight = $(".toolsbar").height();
  148. let exand = tag == "#main" ? 1:50;
  149. let totalHeight = $(window).height() - headerHeight - toolsbarHeight-exand;
  150. const navSize = eles.nav ? eles.nav[type]() + 4 : 0;
  151. totalHeight = totalHeight - navSize;
  152. nearSize = (nearSize/(nearSize + farSize))* totalHeight;
  153. eles.nearSpread[type](nearSize);
  154. eles.farSpread[type](totalHeight - nearSize);
  155. eles.farElement[type](totalHeight - nearSize + navSize);
  156. }
  157. }
  158. function setDefaultSize(tag,eles,type) {
  159. let o_nearSize = 5;
  160. let o_farSize = 2;
  161. if(type == 'height'){
  162. let headerHeight = $(".header").height();
  163. let toolsbarHeight = $(".toolsbar").height();
  164. let exand = tag == "#main" ? 1:50;
  165. let totalHeight = $(window).height() - headerHeight - toolsbarHeight-exand;
  166. const navSize = eles.nav ? eles.nav[type]() + 4 : 0;
  167. totalHeight = totalHeight - navSize;
  168. let nearSize = (o_nearSize/(o_nearSize + o_farSize))* totalHeight;
  169. eles.nearSpread[type](nearSize);
  170. eles.farSpread[type](totalHeight - nearSize);
  171. eles.farElement[type](totalHeight - nearSize + navSize);
  172. }
  173. }