div_resizer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if (obj.attr('store-id')) {
  38. const rType = obj.attr('r-type'), version = obj.attr('store-version') ? ('-'+obj.attr('store-version')) : '' ;
  39. const objSize1 = getLocalCache('v-resize-1-' + obj.attr('store-id') + version);
  40. if (objSize1) {
  41. $(obj.attr('div1')).css(rType, objSize1);
  42. }
  43. const objSize2 = getLocalCache('v-resize-2-' + obj.attr('store-id') + version);
  44. if (objSize2) {
  45. $(obj.attr('div2')).css(rType, objSize2);
  46. }
  47. if (setting.callback) { setting.callback(); }
  48. }
  49. obj.mousedown(function (e) {
  50. const rType = obj.attr('r-type');
  51. mouseMoveCount = 0;
  52. drag = true;
  53. startPos = rType === 'height' ? e.clientY : e.clientX;
  54. // 获取左(上)部分的宽度(高度)
  55. orgSize1 = $($(this).attr('div1'))[rType]();
  56. // 获取右(下)部分的宽度(高度)
  57. orgSize2 = $($(this).attr('div2'))[rType]();
  58. // nav宽(高度)部分
  59. objSize = $(this)[rType]();
  60. //resizeElement.tooltip('hide');
  61. });
  62. $('body').mousemove(function (e) {
  63. if (drag) {
  64. const rType = obj.attr('r-type'), aType = obj.attr('a-type');
  65. const moveSize = rType === 'height' ? e.clientY - startPos : e.clientX - startPos;
  66. //实时刷新页面
  67. mouseMoveCount += moveSize;
  68. //当累计移动超过5个像素时,才刷新,减少刷新次数
  69. if(Math.abs(mouseMoveCount) >= 5){
  70. if (aType === 'percent') {
  71. const min = obj.attr('min') ? obj.attr('min') : 10;
  72. const max = 100 - min;
  73. const percent1 = Math.min(Math.max((orgSize1 + moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  74. $(obj.attr('div1')).css(rType, percent1 + '%');
  75. const percent2 = Math.min(Math.max((orgSize2 - moveSize) / (orgSize1 + orgSize2) * 100, min), max);
  76. $(obj.attr('div2')).css(rType, percent2 + '%');
  77. } else {
  78. const min = obj.attr('min') ? obj.attr('min') : parseInt(((orgSize1 + orgSize2) / 10).toFixed(0));
  79. const max = orgSize1 + orgSize2 - min;
  80. // 判断拖动范围不能超出
  81. newSize1 = Math.min(Math.max(orgSize1 + moveSize, min), max);
  82. newSize2 = Math.min(Math.max(orgSize2 - moveSize, min), max);
  83. $(obj.attr('div1'))[rType](newSize1);
  84. $(obj.attr('div2'))[rType](newSize2);
  85. }
  86. if(setting.callback) { setting.callback(); }
  87. mouseMoveCount = 0;
  88. }
  89. }
  90. });
  91. $('body').mouseup(function () {
  92. if (drag) {
  93. drag = false;
  94. const rType = obj.attr('r-type');
  95. const localId = obj.attr('store-id'), div1 = $(obj.attr('div1')), div2 = $(obj.attr('div2'));
  96. setLocalCache('v-resize-1-' + localId, div1[rType]());
  97. setLocalCache('v-resize-2-' + localId, div2[rType]());
  98. }
  99. });
  100. }
  101. })(jQuery);