component-zoom.vue 972 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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, getCurrentInstance } from 'vue'
  9. let props = defineProps({
  10. width: {
  11. type: String,
  12. default: '375'
  13. },
  14. fontSize: {
  15. type: String,
  16. default: '22'
  17. }
  18. })
  19. let zoom = ref(1);
  20. let currentInstance;
  21. const setFontZoom = () => {
  22. const currentInstance = getCurrentInstance()
  23. let offsetWidth = currentInstance.ctx.$refs.zoomDom.offsetWidth;
  24. zoom.value = offsetWidth > props.width ? +props.width / offsetWidth : 1
  25. }
  26. onMounted(() => {
  27. setFontZoom()
  28. })
  29. </script>
  30. <style lang="scss" scoped>
  31. .zoom-wrap {
  32. font-weight: 800;
  33. color: #FFFFFF;
  34. display: flex;
  35. align-items: center;
  36. justify-content: center;
  37. white-space: nowrap;
  38. }
  39. </style>