Vue3 动态数据大屏 scale
案例
<template> <div class="container"> <div class="screen" ref="screen"> <!-- 顶部 --> <div class="top"></div> <!-- 底部 --> <div class="bottom"> <!-- 左侧 --> <div class="left"></div> <!-- 中间 --> <div class="center"></div> <!-- 右边 --> <div class="right"></div> </div> </div> </div> </template> <script setup lang="ts" name="Screen"> import {ref,reactive, onMounted} from "vue" let screen = ref() onMounted(() => { screen.value.style.transform = `scale(${getScale()}) translate(-50%,-50%)` }); //定义大屏缩放比例 function getScale(w = 1920, h = 1080) { const ww = window.innerWidth / w; const wh = window.innerHeight / h; return ww < wh ? ww : wh; } //监听视口变化 window.onresize = () => { screen.value.style.transform = `scale(${getScale()}) translate(-50%,-50%)` } </script> <style lang="scss" scoped> .container{ width: 100vw; height: 100vh; background: url('./images/bg.png') no-repeat; background-size: cover; .screen{ width: 1920px; height: 1080px; // 定位 position: fixed; top: 50%; left: 50%; transform-origin: left top; background: red; //为了观察,后期去掉 .top{ height: 40px; width: 100%; background: cyan; } } } </style>