大屏适配问题(公共js)

 1 export default function () {
 2   const scale = {
 3     width: '1',
 4     height: '1',
 5   }
 6   const baseWidth = 1920
 7   const baseHeight = 1080
 8   const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
 9   let drawTiming = $ref(0)
10   let appRef = null
11   const calcRate = () => {
12     if (!appRef) {
13       appRef = document.getElementById('appRef')
14     }
15     // 当前宽高比
16     const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
17     if (appRef) {
18       if (currentRate > baseProportion) {
19         // 表示更宽
20         scale.width = (window.innerWidth / baseWidth).toFixed(5)
21         scale.height = (window.innerHeight / baseHeight).toFixed(5)
22         appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
23       } else {
24         // 表示更高
25         scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
26         scale.width = (window.innerWidth / baseWidth).toFixed(5)
27         appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
28       }
29     }
30   }
31   const resize = () => {
32     clearTimeout(drawTiming)
33     drawTiming = setTimeout(() => {
34       calcRate()
35     }, 200)
36   }
37 
38   onMounted(() => {
39     calcRate()
40     window.addEventListener('resize', resize)
41 
42     // 临时解决有滚动条的问题
43     const app = document.getElementById('app').children[0]
44 
45     if (app) {
46       app.style.width = '99vw'
47 
48       setTimeout(() => {
49         app.style.width = '100vw'
50       })
51     }
52   })
53   onBeforeUnmount(() => {
54     window.removeEventListener('resize', resize)
55   })
56 }

vue3使用时

image

 

posted @ 2026-05-14 14:50  Ocean-  阅读(13)  评论(0)    收藏  举报