48 lines
1.6 KiB
JavaScript
48 lines
1.6 KiB
JavaScript
|
|
import { getUniversalZoomLevel } from '@/utils/tools'
|
|||
|
|
|
|||
|
|
let flexible = (designWidth, maxWidth,minWidth) =>{
|
|||
|
|
var doc = document, win = window, docEl = doc.documentElement, remStyle = document.createElement("style"), tid;
|
|||
|
|
designWidth = designWidth || 1920;
|
|||
|
|
// 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);
|
|||
|
|
if(width >= 1024){
|
|||
|
|
designWidth = 1920
|
|||
|
|
}else{
|
|||
|
|
designWidth = 375
|
|||
|
|
}
|
|||
|
|
var rem = Math.round(width * 10 / designWidth);
|
|||
|
|
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
|