1.utils文件夹建一个文件cv以下代码
export function useIndex (appRef) {
let timer = null
const scale = {
width: '1',
height: '1'
}
const baseWidth = 1920
const baseHeight = 1080
const baseProportion = 2
const calcRate = () => {
const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
if (appRef) {
if (currentRate > baseProportion) {
scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
scale.height = (window.innerHeight / baseHeight).toFixed(5)
appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
} else {
scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
scale.width = (window.innerWidth / baseWidth).toFixed(5)
appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
}
}
}
const resize = () => {
clearTimeout(timer)
timer = setTimeout(() => {
calcRate()
}, 200)
}
const windowDraw = () => {
window.addEventListener('resize', resize)
}
return {
appRef,
calcRate,
windowDraw
}
}
2.app.vue结构样式
<template>
<div ref="appRef" class="main">
<div class="layout-container">
</div>
</div>
</template>
<script>
import { useIndex } from '@/utils/utilsDram.js'
export default {
mounted () {
const { calcRate, windowDraw } =useIndex(this.$refs.appRef)
calcRate()
windowDraw()
}
}
</script>
<style lang="scss" scoped>
.main {
color: #d3d6dd;
width: 1920px;
height: 1080px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transform-origin: left top;
.layout-container {
width: 100%;
height: 100%;
}
}
</style>
3.字体大小盒子宽度直接设置px不管放大缩小都是最初的样子,基本页面大小不会相差太远,下图是页面放大500倍的效果
|