body样式
body {
position: relative;
width: 1920px;
height: 1080px;
box-sizing: border-box;
// scale 缩放中心 左上角
transform-origin: left top;
}
useScalePage.js
import { onMounted, onUnmounted } from 'vue';
import _ from 'lodash';
export default function useScalePage(option) {
const resizeFunc = _.throttle(function () {
triggerScale();
}, 100);
onMounted(() => {
triggerScale();
window.addEventListener('resize', resizeFunc);
});
onUnmounted(() => {
window.removeEventListener('resize', resizeFunc);
});
function triggerScale() {
let targetX = option.targetX || 1920;
let targetY = option.targetY || 1080;
let targetRatio = option.targetRatio || 16 / 9;
let currentX = document.documentElement.clientWidth || document.body.clientWidth;
let currentY = document.documentElement.clientHeight || document.body.clientHeight;
let scaleRatio = currentX / targetX;
let currentRatio = currentX / currentY;
if (currentRatio > targetRatio) {
scaleRatio = currentY / targetY;
document.body.style.cssText = `width:${targetX}px; height:${targetY}px;transform: scale(${scaleRatio}) translateX(-50%); left: 50%`;
} else {
document.body.style.cssText = `width:${targetX}px; height:${targetY}px; transform: scale(${scaleRatio})`;
}
}
}
使用
<template>
<router-view />
</template>
<script setup>
import { RouterView } from 'vue-router';
import useScalePage from '@/hooks/useScalePage';
let option = {
targetX: 1920,
targetY: 1080,
targetRatio: 16 / 9,
};
useScalePage(option);
</script>
<style scoped></style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2020-04-28 【Element】table 组件出现表格线条不对齐的问题
2020-04-28 【Vscode】Elements in iteration expect to have 'v-bind:key' directives 报错
2020-04-28 【Git】创建子分支,合并分支并提交