div_resizer.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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'), 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('rParent');
  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](orgSize2);
  61. } else {
  62. div2.show();
  63. autoFlashHeight();
  64. div1[rType](orgSize1);
  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. } else {
  91. orgSize1 = $($(this).attr('div1'))[rType]();
  92. orgSize2 = $($(this).attr('div2'))[rType]();
  93. }
  94. obj.mousedown(function (e) {
  95. mouseMoveCount = 0;
  96. drag = true;
  97. startPos = rType === 'height' ? e.clientY : e.clientX;
  98. // 获取左(上)部分的宽度(高度)
  99. orgSize1 = $($(this).attr('div1'))[rType]();
  100. // 获取右(下)部分的宽度(高度)
  101. orgSize2 = $($(this).attr('div2'))[rType]();
  102. // nav宽(高度)部分
  103. objSize = $(this)[rType]();
  104. //resizeElement.tooltip('hide');
  105. });
  106. $('body').mousemove(function (e) {
  107. if (drag) {
  108. const moveSize = rType === 'height' ? e.clientY - startPos : e.clientX - startPos;
  109. //实时刷新页面
  110. mouseMoveCount += moveSize;
  111. //当累计移动超过5个像素时,才刷新,减少刷新次数
  112. if(Math.abs(mouseMoveCount) >= 5){
  113. if (aType === 'percent') {
  114. const min = obj.attr('min') ? obj.attr('min') : 10;
  115. const max = 99 - min;
  116. const percent1 = Math.min(Math.max((orgSize1 + moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  117. $(obj.attr('div1')).css(rType, percent1 + '%').attr('dr-' + rType, percent1+'%');
  118. const percent2 = Math.min(Math.max((orgSize2 - moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  119. $(obj.attr('div2')).css(rType, percent2 + '%').attr('dr-' + rType, percent2+'%');
  120. } else {
  121. const min = obj.attr('min') ? obj.attr('min') : parseInt(((orgSize1 + orgSize2) / 10).toFixed(0));
  122. const max = orgSize1 + orgSize2 - min;
  123. // 判断拖动范围不能超出
  124. newSize1 = Math.min(Math.max(orgSize1 + moveSize, min), max);
  125. newSize2 = Math.min(Math.max(orgSize2 - moveSize, min), max);
  126. $(obj.attr('div1'))[rType](newSize1);
  127. $(obj.attr('div2'))[rType](newSize2);
  128. }
  129. if(setting.callback) setting.callback();
  130. mouseMoveCount = 0;
  131. }
  132. }
  133. });
  134. $('body').mouseup(function () {
  135. if (drag) {
  136. drag = false;
  137. const localId = obj.attr('store-id'), version = obj.attr('store-version') ? ('-'+obj.attr('store-version')) : '' ;
  138. const div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  139. if (aType === 'percent') {
  140. setLocalCache('v-resize-1-' + localId + version, div1.attr('dr-' + rType));
  141. setLocalCache('v-resize-2-' + localId + version, div2.attr('dr-' + rType));
  142. } else {
  143. setLocalCache('v-resize-1-' + localId + version, div1[rType]());
  144. setLocalCache('v-resize-2-' + localId + version, div2[rType]());
  145. }
  146. }
  147. });
  148. return { refreshDivSize, showSprPart };
  149. }
  150. })(jQuery);