div_resizer.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. /**
  3. *
  4. * div可拖动调整分栏
  5. *
  6. * 示例结构:
  7. * <div id="top-div"></div>
  8. * <div r-Type="height|width" div1="#top-div" div2="#bottom-div" store-id="XXX" store-version="1.0.0" min="100">
  9. * <div id="bottom-div"></div>
  10. *
  11. * 其中:
  12. * r-Type: height表示上下拖动调整, width表示左右拖动调整
  13. * div1: 上(左)部结构的id
  14. * div2: 下(右)部结构的id
  15. * store-id:可选,存在则以该值生成key,将调整结果写入localStorage
  16. * min: 可选,无则默认两部分结构相加的10%
  17. *
  18. * setting: {select, callback}
  19. * 其中:
  20. * select:选定分栏
  21. * callback:回调(除了上述两个div外需要调整的,自定义放在此处)
  22. *
  23. * @author Mai
  24. * @date 2019/3/15
  25. * @version
  26. */
  27. (function($){
  28. $.divResizer = function(setting) {
  29. const obj = $(setting.select);
  30. let drag, mouseMoveCount, startPos, orgSize1, orgSize2, newSize1, newSize2, objSize;
  31. const rType = obj.attr('r-Type') || 'width', aType = obj.attr('a-type');
  32. // if (obj.attr('r-type') === 'height') {
  33. // obj.css('width', '100%').css('height', '1%').css('resize', 'vertical').css('cursor', 's-resize').css('float', 'top');
  34. // } else {
  35. // obj.css('width', '1%').css('height', '100%').css('resize', 'horizontal').css('cursor', 'w-resize').css('float', 'left');
  36. // }
  37. // 根据localStorage初始化
  38. const refreshDivSize = function() {
  39. const div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  40. const parent = div1.parent();
  41. if (aType !== 'percent' && parent[rType]() > 0) {
  42. orgSize1 = div1[rType]();
  43. orgSize2 = div2[rType]();
  44. const parentSize = parent[rType]() * 0.99;
  45. const size1 = Math.floor(orgSize1 / (orgSize1 + orgSize2) * parentSize);
  46. const size2 = Math.floor(parentSize - size1);
  47. div1[rType](size1);
  48. div2[rType](size2);
  49. }
  50. if (setting.callback) { setting.callback(); }
  51. };
  52. const showSprPart = function (show) {
  53. const rParent = obj.attr('r-parent');
  54. const div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  55. const parent = div1.parent();
  56. if (show) {
  57. if (rParent === 'div1') {
  58. div1.show();
  59. autoFlashHeight();
  60. div2[rType](parent[rType] - div2[rType]);
  61. } else {
  62. div2.show();
  63. autoFlashHeight();
  64. div1[rType](parent[rType] - div2[rType]);
  65. }
  66. } else {
  67. if (rParent === 'div1') {
  68. div1.hide();
  69. autoFlashHeight();
  70. div2[rType](parent[rType]());
  71. } else {
  72. div2.hide();
  73. autoFlashHeight();
  74. div1[rType](parent[rType]());
  75. }
  76. }
  77. };
  78. if (obj.attr('store-id')) {
  79. const version = obj.attr('store-version') ? ('-'+obj.attr('store-version')) : '' ;
  80. const objSize1 = getLocalCache('v-resize-1-' + obj.attr('store-id') + version);
  81. if (objSize1) {
  82. $(obj.attr('div1')).css(rType, objSize1);
  83. }
  84. const objSize2 = getLocalCache('v-resize-2-' + obj.attr('store-id') + version);
  85. if (objSize2) {
  86. $(obj.attr('div2')).css(rType, objSize2);
  87. }
  88. refreshDivSize();
  89. if (setting.callback) { setting.callback(); }
  90. }
  91. // 以下代码触发sjs的内存泄漏,原因暂不明
  92. // else {
  93. // orgSize1 = $($(this).attr('div1')) ? $($(this).attr('div1'))[rType]() : 0;
  94. // orgSize2 = $($(this).attr('div2')) ? $($(this).attr('div2'))[rType]() : 0;
  95. // }
  96. obj.mousedown(function (e) {
  97. mouseMoveCount = 0;
  98. drag = true;
  99. startPos = rType === 'height' ? e.clientY : e.clientX;
  100. // 获取左(上)部分的宽度(高度)
  101. orgSize1 = $($(this).attr('div1'))[rType]();
  102. // 获取右(下)部分的宽度(高度)
  103. orgSize2 = $($(this).attr('div2'))[rType]();
  104. // nav宽(高度)部分
  105. objSize = $(this)[rType]();
  106. //resizeElement.tooltip('hide');
  107. });
  108. $('body').mousemove(function (e) {
  109. if (drag) {
  110. const moveSize = rType === 'height' ? e.clientY - startPos : e.clientX - startPos;
  111. //实时刷新页面
  112. mouseMoveCount += moveSize;
  113. //当累计移动超过5个像素时,才刷新,减少刷新次数
  114. if(Math.abs(mouseMoveCount) >= 5){
  115. if (aType === 'percent') {
  116. const min = obj.attr('min') ? obj.attr('min') : 10;
  117. const max = 99 - min;
  118. const percent1 = Math.min(Math.max((orgSize1 + moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  119. $(obj.attr('div1')).css(rType, percent1 + '%').attr('dr-' + rType, percent1+'%');
  120. const percent2 = Math.min(Math.max((orgSize2 - moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  121. $(obj.attr('div2')).css(rType, percent2 + '%').attr('dr-' + rType, percent2+'%');
  122. } else {
  123. const min = obj.attr('min') ? obj.attr('min') : parseInt(((orgSize1 + orgSize2) / 10).toFixed(0));
  124. const max = orgSize1 + orgSize2 - min;
  125. // 判断拖动范围不能超出
  126. newSize1 = Math.min(Math.max(orgSize1 + moveSize, min), max);
  127. newSize2 = Math.min(Math.max(orgSize2 - moveSize, min), max);
  128. $(obj.attr('div1'))[rType](newSize1);
  129. $(obj.attr('div2'))[rType](newSize2);
  130. }
  131. if(setting.callback) setting.callback();
  132. mouseMoveCount = 0;
  133. }
  134. }
  135. });
  136. $('body').mouseup(function () {
  137. if (drag) {
  138. drag = false;
  139. const localId = obj.attr('store-id'), version = obj.attr('store-version') ? ('-'+obj.attr('store-version')) : '' ;
  140. const div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  141. if (aType === 'percent') {
  142. setLocalCache('v-resize-1-' + localId + version, div1.attr('dr-' + rType));
  143. setLocalCache('v-resize-2-' + localId + version, div2.attr('dr-' + rType));
  144. } else {
  145. setLocalCache('v-resize-1-' + localId + version, div1[rType]());
  146. setLocalCache('v-resize-2-' + localId + version, div2[rType]());
  147. }
  148. }
  149. });
  150. return { refreshDivSize, showSprPart };
  151. }
  152. })(jQuery);