slideResize.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/11/7
  7. * @version
  8. */
  9. /*
  10. * div之间的水平拖动,适应各种情况
  11. * module: 所属模块,防止不同页面相同id导致localstorage数据被覆盖
  12. * eleObj: resize, parent, left, right
  13. * limit: min, max
  14. * */
  15. const SlideResize = (function() {
  16. let mouseMoveCount = 0;
  17. function horizontalSlide(module, eleObj, limit, callback) {
  18. const triggerCBSize = 5;
  19. let drag = false,
  20. startPoint = 0,
  21. leftWidth = 0,
  22. rightWidth = 0,
  23. leftChange = 0,
  24. rightChange = 0,
  25. limitMax = 0;
  26. eleObj.resize.mousedown(function (e) {
  27. drag = true;
  28. startPoint = e.clientX;
  29. leftWidth = eleObj.left.width();
  30. rightWidth = eleObj.right.width();
  31. limitMax = eval(limit.max);
  32. });
  33. $('body').mousemove(function (e) {
  34. if (drag) {
  35. let moveSize = e.clientX - startPoint;
  36. leftChange = leftWidth + moveSize;
  37. leftChange = leftChange < limit.min ? limit.min : leftChange;
  38. leftChange = leftChange > limitMax ? limitMax - 3 : leftChange;
  39. rightChange = rightWidth - moveSize;
  40. rightChange = rightChange < limit.min ? limit.min : rightChange;
  41. rightChange = rightChange > limitMax ? limitMax - 3 : rightChange;
  42. let leftPercentWidth = leftChange / eleObj.parent.width() * 100 + '%',
  43. rightPercentWidth = rightChange / eleObj.parent.width() * 100 + '%';
  44. eleObj.left.css('width', leftPercentWidth);
  45. eleObj.right.css('width', rightPercentWidth);
  46. mouseMoveCount += Math.abs(moveSize);
  47. if (mouseMoveCount > triggerCBSize && callback) {
  48. callback();
  49. mouseMoveCount = 0;
  50. }
  51. }
  52. });
  53. $('body').mouseup(function (e) {
  54. if (drag) {
  55. drag = false;
  56. mouseMoveCount = 0;
  57. //将宽度信息存储到localstorage
  58. let leftWidthInfo = eleObj.left[0].style.width;
  59. setLocalCache(`${module}${eleObj.left.attr('id')}Width`, leftWidthInfo);
  60. let rightWidthInfo = eleObj.right[0].style.width;
  61. setLocalCache(`${module}${eleObj.right.attr('id')}Width`, rightWidthInfo);
  62. }
  63. });
  64. }
  65. function loadHorizonWidth(module, eles, callback) {
  66. for (let ele of eles) {
  67. let cache = getLocalCache(`${module}${ele.attr('id')}Width`);
  68. if (cache) {
  69. ele.css('width', cache);
  70. }
  71. }
  72. if (callback) {
  73. callback();
  74. }
  75. }
  76. return {horizontalSlide, loadHorizonWidth}
  77. })();