component-zoom.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <!-- 组件确定最大宽度时 可等比缩放组件 -->
  2. <template>
  3. <span class="zoom-wrap" ref="zoomDom" :style="{ zoom: zoom, 'font-size': props.fontSize + 'px' }">
  4. <slot></slot>
  5. </span>
  6. </template>
  7. <script setup>
  8. import { ref, defineProps, onMounted, watch } from 'vue'
  9. let props = defineProps({
  10. width: {
  11. type: [String, Number],
  12. default: '375'
  13. },
  14. fontSize: {
  15. type: String,
  16. default: '22'
  17. },
  18. txt: {
  19. type: String,
  20. default: ''
  21. }
  22. })
  23. watch(() => props.txt, (newVal) => {
  24. setFontZoom(300)
  25. },
  26. {
  27. deep: true
  28. })
  29. let zoom = ref(1);
  30. let zoomDom = ref({});
  31. const setFontZoom = (time = 600) => {
  32. setTimeout(() => {
  33. let offsetWidth = zoomDom.value && zoomDom.value.offsetWidth || props.width;
  34. zoom.value = offsetWidth > props.width ? +props.width / offsetWidth : 1
  35. }, time)
  36. }
  37. onMounted(() => {
  38. setFontZoom()
  39. })
  40. </script>
  41. <style lang="scss" scoped>
  42. .zoom-wrap {
  43. font-weight: 800;
  44. color: #FFFFFF;
  45. display: flex;
  46. align-items: center;
  47. justify-content: center;
  48. white-space: nowrap;
  49. width: fit-content;
  50. }
  51. </style>