适配4K大屏(3840×2160分辨率)需要考虑高DPI显示、布局缩放、字体清晰度等多方面因素。以下是针对Umi+React后台管理系统的完整适配方案:
<!-- 在public/index.html中 -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
在config/config.ts
中:
export default {
// 启用高清方案
hd: {
// 根据dpr调整rem基准值
theme: {
'@hd': '2px',
},
},
// 其他配置...
}
/* 在全局样式文件中 */
@media screen and (min-width: 3840px) {
:root {
font-size: 24px; /* 4K下增大基准字体 */
}
.container {
max-width: 80%; /* 控制内容区域宽度 */
margin: 0 auto;
}
}
// 在入口文件或layout组件中
function setRem() {
const baseSize = 16; // 基准大小
const scale = document.documentElement.clientWidth / 1920; // 设计稿宽度
document.documentElement.style.fontSize =
baseSize * Math.min(scale, 2) + 'px'; // 限制最大缩放2倍
}
window.addEventListener('resize', setRem);
setRem();
// 初始化图表时设置devicePixelRatio
const chart = echarts.init(dom, null, {
devicePixelRatio: window.devicePixelRatio > 1 ? 2 : 1
});
// 使用Umi的图片处理
import logo from '@/assets/logo@2x.png'; // 准备2x/3x图
// 或者使用动态加载
const imageUrl = window.devicePixelRatio > 2
? require('@/assets/logo@3x.png')
: require('@/assets/logo@2x.png');
在config/config.ts
中:
theme: {
'font-size-base': '16px', // 基础字体大小
'layout-header-height': '80px', // 增大头部高度
'menu-item-height': '60px', // 增大菜单项高度
'table-padding-vertical': '24px', // 表格内边距
'table-padding-horizontal': '24px',
}
// src/global.less
.ant-btn {
padding: 12px 24px;
font-size: 16px;
}
.ant-table {
font-size: 16px;
td, th {
padding: 16px;
}
}
// 使用dynamicImport拆分大屏专用组件
export default {
dynamicImport: {
loading: '@/components/PageLoading',
},
}
/* 使用更清晰的字体渲染 */
body {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-rendering: optimizeLegibility;
}
// utils/screenAdapter.js
export const is4KScreen = () => {
return window.screen.width >= 3840 || window.devicePixelRatio >= 2;
};
export const getScaleRatio = (baseWidth = 1920) => {
return Math.min(window.innerWidth / baseWidth, 2);
};
// 组件中使用
import { is4KScreen, getScaleRatio } from '@/utils/screenAdapter';
const scale = getScaleRatio();
const style = {
transform: `scale(${scale})`,
transformOrigin: 'top left',
width: `${100/scale}%`,
height: `${100/scale}%`
};
通过以上方案,可以优雅地实现Umi+React后台管理系统在4K大屏上的完美展示,同时保持良好的开发体验和代码可维护性。