Files
aida_front/src/tool/flexible.js

70 lines
2.3 KiB
JavaScript
Raw Normal View History

2025-06-19 09:21:32 +08:00
import { getUniversalZoomLevel } from "@/tool/util";
import MyEvent from "@/tool/myEvents";
2025-06-19 09:21:32 +08:00
2023-10-20 17:21:45 +08:00
let flexible = (designWidth, maxWidth,minWidth) =>{
2023-01-06 16:00:15 +08:00
var doc = document, win = window, docEl = doc.documentElement, remStyle = document.createElement("style"), tid;
2024-01-10 09:34:32 +08:00
designWidth = designWidth || 1920;
2024-01-09 17:24:01 +08:00
maxWidth = maxWidth || 2560;
2025-07-19 14:04:48 +08:00
minWidth = minWidth || 1024;
2025-08-22 10:27:48 +08:00
let oldDesignWidth = designWidth
2025-06-19 09:21:32 +08:00
// minWidth = minWidth || 1024;
2023-01-06 16:00:15 +08:00
function refreshRem() {
var width = docEl.getBoundingClientRect().width;
2024-04-29 17:00:33 +08:00
var height = docEl.getBoundingClientRect().height;
2025-06-19 09:21:32 +08:00
width = getUniversalZoomLevel() * width
height = getUniversalZoomLevel() * height
2023-09-25 10:09:00 +08:00
maxWidth = maxWidth || 1920;
2025-06-19 11:00:52 +08:00
if(width < 1100){
document.body.classList.add('ipad')
}else{
document.body.classList.remove('ipad')
}
2025-08-22 10:27:48 +08:00
if(width > 768){
if(width/height>1.98) width = height * 1.98;
width > maxWidth && (width = maxWidth);
width < minWidth && (width = minWidth);
designWidth = oldDesignWidth
}else{
designWidth = 393
}
console.log(width,designWidth)
2024-12-31 11:40:40 +08:00
// var rem = width * 10 / designWidth;
var rem = Math.round(width * 10 / designWidth);
2024-03-05 10:53:01 +08:00
docEl.style.fontSize = rem+'px'
2023-01-06 16:00:15 +08:00
remStyle.innerHTML = 'html{font-size:' + rem + 'px;}';
MyEvent.emit('remChange',rem)
2023-01-06 16:00:15 +08:00
}
2024-03-05 10:53:01 +08:00
// if (docEl.firstElementChild) {
// docEl.firstElementChild.appendChild(remStyle);
// } else {
// var wrap = doc.createElement("div");
// wrap.appendChild(remStyle);
// doc.write(wrap.innerHTML);
// wrap = null;
// }
2023-01-06 16:00:15 +08:00
//要等 wiewport 设置好后才能执行 refreshRem不然 refreshRem 会执行2次
refreshRem();
win.addEventListener("resize", function() {
clearTimeout(tid); //防止执行两次
tid = setTimeout(refreshRem, 300);
}, false);
win.addEventListener("pageshow", function(e) {
if (e.persisted) { // 浏览器后退的时候重新计算
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}
}, false);
if (doc.readyState === "complete") {
doc.body.style.fontSize = "16px";
} else {
doc.addEventListener("DOMContentLoaded", function(e) {
doc.body.style.fontSize = "16px";
}, false);
}
};
export default flexible