FontZoomMobile.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!-- 组件确定最大宽度时 可等比缩放组件 -->
  2. <template>
  3. <div :style="{ width: `${zoomWidth}px` }">
  4. <span class="zoom-wrap" ref="zoomDom" :style="{ transform: `scale(${zoom})` }">
  5. <slot></slot>
  6. </span>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. width: {
  13. type: String,
  14. default: '375'
  15. },
  16. unColor: {
  17. type: Boolean,
  18. default: false
  19. },
  20. },
  21. data() {
  22. return {
  23. zoom: 1,
  24. zoomWidth: 375
  25. }
  26. },
  27. mounted() {
  28. this.setFontZoom()
  29. },
  30. methods: {
  31. setFontZoom() {
  32. this.$nextTick(() => {
  33. let offsetWidth = this.$refs.zoomDom.offsetWidth;
  34. console.log(this.$refs)
  35. console.log('refs', this.$refs.zoomDom.offsetWidth)
  36. this.zoom = offsetWidth > this.width ? +this.width / offsetWidth : 1
  37. this.zoomWidth = offsetWidth > this.width ? this.width : offsetWidth;
  38. })
  39. }
  40. }
  41. }
  42. </script>
  43. <style lang="scss" scoped>
  44. .zoom-wrap {
  45. white-space: nowrap;
  46. display: inline-block;
  47. transform-origin: left center;
  48. }
  49. </style>