forked from liweijie/education
全局自适应以及表格横向拖动优化
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 全局视口等比缩放 —— 让整个系统在不同大小屏幕(桌面 1280~1920)下布局不崩塌。
|
||||
*
|
||||
* 原理:
|
||||
* 以 DESIGN_WIDTH 为设计基准宽度(视觉稿基准)。当视口宽度小于该值时,
|
||||
* 对整个文档根节点 <html> 应用 zoom = 视口宽度 / DESIGN_WIDTH(不超过 1),
|
||||
* 使页面内部所有元素(含 Element UI 挂载到 body 的弹窗/提示/下拉等)等比缩小,
|
||||
* 从而在保持原有表格/表单固定布局不塌陷的前提下,整体适配到当前屏幕。
|
||||
*
|
||||
* 说明:
|
||||
* 1. 仅在“需要缩小”时写入 zoom,宽度 ≥ 设计宽时清除,保证大屏原生 1:1 显示;
|
||||
* 2. zoom 挂在 documentElement 上,能覆盖包括 Element 弹层在内的所有内容;
|
||||
* 3. modern Chromium / Edge / Safari 及 Firefox(≥126) 均支持 zoom。
|
||||
*/
|
||||
|
||||
let initialized = false
|
||||
|
||||
export const DEFAULT_DESIGN_WIDTH = 1920
|
||||
|
||||
/**
|
||||
* 初始化全局自适应。可在任意入口调用,重复调用幂等。
|
||||
* @param {number} [designWidth] 设计基准宽度,默认 1920
|
||||
*/
|
||||
export function initAdaptive(designWidth = DEFAULT_DESIGN_WIDTH) {
|
||||
if (initialized) {
|
||||
return
|
||||
}
|
||||
initialized = true
|
||||
|
||||
let timer = null
|
||||
|
||||
const applyScale = () => {
|
||||
const root = document.documentElement
|
||||
const width = (root && root.clientWidth) || window.innerWidth
|
||||
const scale = Math.min(1, width / designWidth)
|
||||
// 小于设计宽才缩放,否则清除,避免触发整页缩放动画/重置
|
||||
if (root) {
|
||||
root.style.zoom = scale < 1 ? String(scale) : ''
|
||||
}
|
||||
}
|
||||
|
||||
const onResize = () => {
|
||||
// 简单防抖,避免 resize 高频触发引起抖动
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
timer = setTimeout(applyScale, 100)
|
||||
}
|
||||
|
||||
applyScale()
|
||||
window.addEventListener('resize', onResize)
|
||||
}
|
||||
Reference in New Issue
Block a user