div_resizer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // if (obj.attr('r-type') === 'height') {
  32. // obj.css('width', '100%').css('height', '1%').css('resize', 'vertical').css('cursor', 's-resize').css('float', 'top');
  33. // } else {
  34. // obj.css('width', '1%').css('height', '100%').css('resize', 'horizontal').css('cursor', 'w-resize').css('float', 'left');
  35. // }
  36. // 根据localStorage初始化
  37. const refreshDivSize = function() {
  38. const rType = obj.attr('r-type'), aType = obj.attr('a-type');
  39. const div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  40. const parent = div1.parent();
  41. if (aType !== 'percent') {
  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. if (obj.attr('store-id')) {
  53. const rType = obj.attr('r-type'), version = obj.attr('store-version') ? ('-'+obj.attr('store-version')) : '' ;
  54. const objSize1 = getLocalCache('v-resize-1-' + obj.attr('store-id') + version);
  55. if (objSize1) {
  56. $(obj.attr('div1')).css(rType, objSize1);
  57. }
  58. const objSize2 = getLocalCache('v-resize-2-' + obj.attr('store-id') + version);
  59. if (objSize2) {
  60. $(obj.attr('div2')).css(rType, objSize2);
  61. }
  62. refreshDivSize();
  63. if (setting.callback) { setting.callback(); }
  64. }
  65. obj.mousedown(function (e) {
  66. const rType = obj.attr('r-type');
  67. mouseMoveCount = 0;
  68. drag = true;
  69. startPos = rType === 'height' ? e.clientY : e.clientX;
  70. // 获取左(上)部分的宽度(高度)
  71. orgSize1 = $($(this).attr('div1'))[rType]();
  72. // 获取右(下)部分的宽度(高度)
  73. orgSize2 = $($(this).attr('div2'))[rType]();
  74. // nav宽(高度)部分
  75. objSize = $(this)[rType]();
  76. //resizeElement.tooltip('hide');
  77. });
  78. $('body').mousemove(function (e) {
  79. if (drag) {
  80. const rType = obj.attr('r-type'), aType = obj.attr('a-type');
  81. const moveSize = rType === 'height' ? e.clientY - startPos : e.clientX - startPos;
  82. //实时刷新页面
  83. mouseMoveCount += moveSize;
  84. //当累计移动超过5个像素时,才刷新,减少刷新次数
  85. if(Math.abs(mouseMoveCount) >= 5){
  86. if (aType === 'percent') {
  87. const min = obj.attr('min') ? obj.attr('min') : 10;
  88. const max = 99 - min;
  89. const percent1 = Math.min(Math.max((orgSize1 + moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  90. $(obj.attr('div1')).css(rType, percent1 + '%').attr('dr-' + rType, percent1+'%');
  91. const percent2 = Math.min(Math.max((orgSize2 - moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  92. $(obj.attr('div2')).css(rType, percent2 + '%').attr('dr-' + rType, percent2+'%');
  93. } else {
  94. const min = obj.attr('min') ? obj.attr('min') : parseInt(((orgSize1 + orgSize2) / 10).toFixed(0));
  95. const max = orgSize1 + orgSize2 - min;
  96. // 判断拖动范围不能超出
  97. newSize1 = Math.min(Math.max(orgSize1 + moveSize, min), max);
  98. newSize2 = Math.min(Math.max(orgSize2 - moveSize, min), max);
  99. $(obj.attr('div1'))[rType](newSize1);
  100. $(obj.attr('div2'))[rType](newSize2);
  101. }
  102. if(setting.callback) setting.callback();
  103. mouseMoveCount = 0;
  104. }
  105. }
  106. });
  107. $('body').mouseup(function () {
  108. if (drag) {
  109. drag = false;
  110. const rType = obj.attr('r-type'), aType = obj.attr('a-type');
  111. const localId = obj.attr('store-id'), version = obj.attr('store-version') ? ('-'+obj.attr('store-version')) : '' ;
  112. const div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  113. if (aType === 'percent') {
  114. setLocalCache('v-resize-1-' + localId + version, div1.attr('dr-' + rType));
  115. setLocalCache('v-resize-2-' + localId + version, div2.attr('dr-' + rType));
  116. } else {
  117. setLocalCache('v-resize-1-' + localId + version, div1[rType]());
  118. setLocalCache('v-resize-2-' + localId + version, div2[rType]());
  119. }
  120. }
  121. });
  122. return { refreshDivSize };
  123. }
  124. })(jQuery);