CardHeadBodyAutoResize.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div :class="classes">
  3. <div ref="rectColRef1" class="rect-col" style="width: 50%">
  4. <div
  5. class="rect-col-item"
  6. ref="stdnoContainer"
  7. :style="{ height: heights.stdno + 'px' }"
  8. >
  9. <slot name="stdno"></slot>
  10. </div>
  11. <div
  12. :class="['rect-col-item', { 'rect-col-item-none': !$slots.dynamic }]"
  13. ref="dynamicContainer"
  14. :style="{ height: heights.dynamic + 'px' }"
  15. >
  16. <slot name="dynamic"></slot>
  17. </div>
  18. </div>
  19. <div ref="rectColRef2" class="rect-col" style="width: 50%">
  20. <div
  21. class="rect-col-item"
  22. ref="stdinfoContainer"
  23. :style="{ height: heights.stdinfo + 'px' }"
  24. >
  25. <slot name="stdinfo"></slot>
  26. </div>
  27. <div
  28. class="rect-col-item"
  29. ref="noticeContainer"
  30. :style="{ height: heights.notice + 'px' }"
  31. >
  32. <slot name="notice"></slot>
  33. </div>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. name: "card-head-body-auto-resize",
  40. props: {
  41. data: {
  42. type: Object,
  43. },
  44. },
  45. data() {
  46. return {
  47. orgHeights: {
  48. stdinfo: 40,
  49. notice: 40,
  50. stdno: 40,
  51. dynamic: 40,
  52. },
  53. heights: {
  54. stdinfo: 40,
  55. notice: 40,
  56. stdno: 40,
  57. dynamic: 40,
  58. },
  59. leftColWidth: 50,
  60. rightColWidth: 50,
  61. maxRigthColWidth: 60,
  62. };
  63. },
  64. computed: {
  65. classes() {
  66. return ["card-head-body-auto-resize", "col-item-auto-height"];
  67. },
  68. },
  69. mounted() {
  70. this.initStyles();
  71. },
  72. methods: {
  73. initStyles() {
  74. // width size
  75. const fillNumber = this.data.fillNumber || 13;
  76. if (fillNumber <= 10) {
  77. this.leftColWidth = 50;
  78. } else {
  79. let leftColWidth = 50 + 4 * (fillNumber - 10);
  80. this.leftColWidth = Math.min(this.maxRigthColWidth, leftColWidth);
  81. }
  82. this.rightColWidth = 100 - this.leftColWidth;
  83. this.$refs.rectColRef1.style.width = `${this.leftColWidth}%`;
  84. this.$refs.rectColRef2.style.width = `${this.rightColWidth}%`;
  85. const containers = ["stdinfo", "notice", "stdno", "dynamic"];
  86. containers.forEach((container) => {
  87. const dom =
  88. this.$refs[`${container}Container`] &&
  89. this.$refs[`${container}Container`].firstChild;
  90. this.orgHeights[container] = dom ? dom.offsetHeight : 0;
  91. });
  92. Object.keys(this.orgHeights).map((key) => {
  93. this.heights[key] = this.orgHeights[key] + 2;
  94. });
  95. this.resizeRect();
  96. },
  97. resizeRect() {
  98. // height size
  99. let col1 = this.orgHeights.stdinfo + this.orgHeights.notice;
  100. let col2 = this.orgHeights.stdno + this.orgHeights.dynamic;
  101. if (this.$slots.dynamic) {
  102. if (col1 > col2) {
  103. this.heights.stdno = col1 - col2 + this.orgHeights.stdno + 2;
  104. this.heights.dynamic = this.orgHeights.dynamic + 2;
  105. } else {
  106. const splitHeight = (col2 - col1) / 2;
  107. this.heights.stdinfo = splitHeight + this.orgHeights.stdinfo + 2;
  108. this.heights.notice = splitHeight + this.orgHeights.notice + 2;
  109. }
  110. } else {
  111. col1 += 14;
  112. col2 -= 2;
  113. if (col1 > col2) {
  114. this.heights.stdno = col1;
  115. } else {
  116. const splitHeight = (col2 - col1) / 2;
  117. this.heights.stdinfo = splitHeight + this.orgHeights.stdinfo + 2;
  118. this.heights.notice = splitHeight + this.orgHeights.notice + 2;
  119. }
  120. }
  121. },
  122. },
  123. };
  124. </script>