CardHeadBodyAutoResize.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div :class="classes">
  3. <div class="rect-col" :style="{ width: rightColWidth + '%' }">
  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 class="rect-col" :style="{ width: leftColWidth + '%' }">
  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. const containers = ["stdinfo", "notice", "stdno", "dynamic"];
  75. containers.forEach((container) => {
  76. const dom =
  77. this.$refs[`${container}Container`] &&
  78. this.$refs[`${container}Container`].firstChild;
  79. this.orgHeights[container] = dom ? dom.offsetHeight : 0;
  80. });
  81. Object.keys(this.orgHeights).map((key) => {
  82. this.heights[key] = this.orgHeights[key] + 2;
  83. });
  84. this.resizeRect();
  85. },
  86. resizeRect() {
  87. // width size
  88. const fillNumber = this.data.fillNumber || 13;
  89. if (fillNumber <= 10) {
  90. this.rightColWidth = 50;
  91. } else {
  92. let rightColWidth = 50 + 4 * (fillNumber - 10);
  93. this.rightColWidth = Math.min(this.maxRigthColWidth, rightColWidth);
  94. }
  95. this.leftColWidth = 100 - this.rightColWidth;
  96. // height size
  97. let col1 = this.orgHeights.stdinfo + this.orgHeights.notice;
  98. let col2 = this.orgHeights.stdno + this.orgHeights.dynamic;
  99. if (this.$slots.dynamic) {
  100. if (col1 > col2) {
  101. this.heights.stdno = col1 - col2 + this.orgHeights.stdno + 2;
  102. this.heights.dynamic = this.orgHeights.dynamic + 2;
  103. } else {
  104. const splitHeight = (col2 - col1) / 2;
  105. this.heights.stdinfo = splitHeight + this.orgHeights.stdinfo + 2;
  106. this.heights.notice = splitHeight + this.orgHeights.notice + 2;
  107. }
  108. } else {
  109. col1 += 14;
  110. col2 -= 2;
  111. if (col1 > col2) {
  112. this.heights.stdno = col1;
  113. } else {
  114. const splitHeight = (col2 - col1) / 2;
  115. this.heights.stdinfo = splitHeight + this.orgHeights.stdinfo + 2;
  116. this.heights.notice = splitHeight + this.orgHeights.notice + 2;
  117. }
  118. }
  119. },
  120. },
  121. };
  122. </script>