component-zoom.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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(0)
  39. setFontZoom()
  40. })
  41. </script>
  42. <style lang="scss" scoped>
  43. .zoom-wrap {
  44. font-weight: 800;
  45. color: #FFFFFF;
  46. display: flex;
  47. align-items: center;
  48. justify-content: center;
  49. white-space: nowrap;
  50. width: fit-content;
  51. }
  52. </style>