Home.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="promotion-portal">
  3. <div
  4. class="promotion-portal-top"
  5. ref="topRef"
  6. :style="{ height: topHeight + 'px', backgroundImage: `url(${AiBgImage})` }"
  7. >
  8. <div class="promotion-portal-top__wrapper">
  9. <TopContent></TopContent>
  10. </div>
  11. </div>
  12. <div
  13. class="promotion-portal-tab record"
  14. ref="tab1Ref"
  15. :style="{ height: tab1Height + 'px', backgroundImage: `url(${Tab1Image})` }"
  16. ></div>
  17. <div
  18. class="promotion-portal-tab cando"
  19. ref="tab2Ref"
  20. :style="{ height: tab2Height + 'px', backgroundImage: `url(${Tab2Image})` }"
  21. ></div>
  22. </div>
  23. </template>
  24. <script lang="ts" setup>
  25. import { ref, onMounted, onUnmounted } from 'vue';
  26. import TopContent from '@/components/TopContent.vue';
  27. import AiBgImage from '@/assets/images/ai-bg.png';
  28. import Tab1Image from '@/assets/images/record.png';
  29. import Tab2Image from '@/assets/images/cando.png';
  30. const topRef = ref<HTMLElement | null>(null);
  31. const tab1Ref = ref<HTMLElement | null>(null);
  32. const tab2Ref = ref<HTMLElement | null>(null);
  33. const topHeight = ref(0);
  34. const tab1Height = ref(0);
  35. const tab2Height = ref(0);
  36. function setHeight(refEl: HTMLElement | null, imgUrl: string, heightRef: typeof topHeight) {
  37. if (!refEl) return;
  38. const img = new Image();
  39. img.src = imgUrl;
  40. img.onload = () => {
  41. const ratio = img.height / img.width;
  42. heightRef.value = refEl.offsetWidth * ratio;
  43. };
  44. }
  45. function setRem() {
  46. const baseSize = 16; // 基准字体大小
  47. const designWidth = 1920; // 设计稿宽度
  48. const scale = window.innerWidth / designWidth;
  49. document.documentElement.style.fontSize = baseSize * scale + 'px';
  50. }
  51. setRem();
  52. function updateAllHeights() {
  53. setHeight(topRef.value, AiBgImage, topHeight);
  54. setHeight(tab1Ref.value, Tab1Image, tab1Height);
  55. setHeight(tab2Ref.value, Tab2Image, tab2Height);
  56. }
  57. onMounted(() => {
  58. updateAllHeights();
  59. window.addEventListener('resize', updateAllHeights);
  60. window.addEventListener('resize', setRem);
  61. });
  62. onUnmounted(() => {
  63. window.removeEventListener('resize', updateAllHeights);
  64. window.addEventListener('resize', setRem);
  65. });
  66. </script>
  67. <style lang="scss" scoped>
  68. .promotion-portal {
  69. width: 100%;
  70. overflow: auto;
  71. &-top,
  72. &-tab {
  73. width: 100%;
  74. background-repeat: no-repeat;
  75. background-position: center;
  76. background-size: contain; /* 或 cover,根据你需求 */
  77. transition: height 0.1s;
  78. }
  79. &-top {
  80. position: relative;
  81. &__wrapper {
  82. position: absolute;
  83. top: 2rem;
  84. left: 16.25rem;
  85. width: 41rem;
  86. height: 34rem;
  87. overflow: auto;
  88. }
  89. }
  90. &-tab.record {
  91. /* backgroundImage 由绑定 style 设置 */
  92. }
  93. &-tab.cando {
  94. /* backgroundImage 由绑定 style 设置 */
  95. }
  96. }
  97. </style>