2025-10-09 09:29:36 +08:00
|
|
|
|
import { getUniversalZoomLevel } from '@/utils/tools'
|
|
|
|
|
|
|
2025-10-09 16:04:55 +08:00
|
|
|
|
let flexible = (designWidth) =>{
|
2025-10-09 09:29:36 +08:00
|
|
|
|
var doc = document, win = window, docEl = doc.documentElement, remStyle = document.createElement("style"), tid;
|
2025-10-09 11:20:35 +08:00
|
|
|
|
designWidth = designWidth || 1080;
|
2025-10-09 09:29:36 +08:00
|
|
|
|
// maxWidth = maxWidth || 1920;
|
|
|
|
|
|
// minWidth = minWidth || 500;
|
|
|
|
|
|
// minWidth = minWidth || 1024;
|
|
|
|
|
|
function refreshRem() {
|
|
|
|
|
|
var width = docEl.getBoundingClientRect().width;
|
|
|
|
|
|
var height = docEl.getBoundingClientRect().height;
|
|
|
|
|
|
width = getUniversalZoomLevel() * width
|
|
|
|
|
|
height = getUniversalZoomLevel() * height
|
|
|
|
|
|
// width > maxWidth && (width = maxWidth);
|
|
|
|
|
|
// width < minWidth && (width = minWidth);
|
2025-10-09 11:20:35 +08:00
|
|
|
|
|
|
|
|
|
|
// if(width >= 1024){
|
|
|
|
|
|
// designWidth = 1920
|
|
|
|
|
|
// }else{
|
|
|
|
|
|
// designWidth = 375
|
|
|
|
|
|
// }
|
2025-10-09 15:43:43 +08:00
|
|
|
|
var rem = (width * 10 / designWidth).toFixed(2);
|
2025-10-09 09:29:36 +08:00
|
|
|
|
docEl.style.fontSize = rem+'px'
|
|
|
|
|
|
remStyle.innerHTML = 'html{font-size:' + rem + 'px;}';
|
|
|
|
|
|
}
|
|
|
|
|
|
//要等 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
|