CardHeadBodyAutoResize.vue 2.7 KB

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