118 lines
4.4 KiB
JavaScript
118 lines
4.4 KiB
JavaScript
"use strict";
|
||
define(function (require, exports, module) {
|
||
|
||
function handle(e) {
|
||
const ctx = require("../../common/ctx");
|
||
let historyScreenW = ctx.screenWidth;
|
||
let curScreenW = ctx.getScreenWidth();
|
||
if (historyScreenW === curScreenW) {
|
||
return
|
||
}
|
||
|
||
//调整
|
||
refreshEditFrontSize()
|
||
refreshRootFrontSize();
|
||
|
||
//更新
|
||
ctx.screenWidth = ctx.getScreenWidth();
|
||
ctx.screenHeight = ctx.getScreenHeight();
|
||
}
|
||
|
||
/**
|
||
* 刷新跟节点 front-size for 全局盒子尺寸
|
||
* @constructor
|
||
*/
|
||
function refreshRootFrontSize() {
|
||
const ctx = require("../../common/ctx");
|
||
let designWidth = ctx.designWith;
|
||
let curDoc = document.documentElement;//当前文档的 root 元素
|
||
let curClientW = ctx.getScreenWidth();
|
||
if (!curClientW) {
|
||
curClientW = designWidth;
|
||
}
|
||
|
||
|
||
let dpr = getDpr();
|
||
//set 1rem = curClientW / designWidth (支持响应式)
|
||
let nowFrontSize = '1px';
|
||
if (ctx.isTablet || ctx.isMobile || ctx.isIOS || ctx.isAndroid) {
|
||
nowFrontSize = (curClientW / designWidth) * dpr + 'px';
|
||
} else {
|
||
nowFrontSize = (curClientW / designWidth) + 'px';
|
||
}
|
||
|
||
// curDoc.style.fontSize = dpr * nowFrontSize;
|
||
curDoc.style.fontSize = nowFrontSize;
|
||
|
||
console.log("curClientW :", curClientW, "designWidth: ", designWidth, "-> ", nowFrontSize);
|
||
let testDiv = document.getElementById("testDevice");
|
||
testDiv.innerText += "\ncurClientW: " + curClientW;
|
||
testDiv.innerText += "\ndesignWith: " + designWidth;
|
||
testDiv.innerText += "\n1rem =: " + nowFrontSize;
|
||
testDiv.innerText += "\nwindow.screen.width =: " + window.screen.width;
|
||
testDiv.innerText += "\nwindow.screen.height =: " + window.screen.height;
|
||
// testDiv.innerText = testDiv.innerText +
|
||
// // "\n navigator_userAgent :" + navigator.userAgent.toLocaleLowerCase() +
|
||
// "\n isMobile :" + /mobi|android|iphone|ipad|ipod/i.test(navigator.userAgent.toLocaleLowerCase()) +
|
||
// "\n isIOS :" + /iphone|ipad|ipod/.test(window.navigator.userAgent.toLocaleLowerCase()) +
|
||
// "\n isAndroid :" + /android/.test(window.navigator.userAgent.toLocaleLowerCase()) +
|
||
// "\n window.width :" + window.innerWidth +
|
||
// "\n curClientW :" + curClientW +
|
||
// " \n designWidth: " + designWidth +
|
||
// "\n 1rem = " + nowFrontSize;
|
||
|
||
}
|
||
|
||
function getDpr() {
|
||
//获取屏幕像素密度
|
||
let dpr = window.devicePixelRatio || 1;//当前设置下 物理像素和虚拟像素的比值
|
||
if (!dpr) {
|
||
//devicePixelRatio这个属性是可以获取到设备的dpr
|
||
let devicePixelRatio = window?.devicePixelRatio;
|
||
//判断dpr是否为整数
|
||
let isRegularDpr = devicePixelRatio.toString().match(/^[1-9]\d*$/g)
|
||
if (isRegularDpr) {
|
||
// 对于是整数的dpr,对dpr进行操作
|
||
if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
|
||
dpr = 3;
|
||
} else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)) {
|
||
dpr = 2;
|
||
} else {
|
||
dpr = 1;
|
||
}
|
||
} else {
|
||
// 其他设备下,仍旧使用1倍的方案
|
||
dpr = 1;
|
||
}
|
||
}
|
||
return dpr;
|
||
}
|
||
|
||
function refreshEditFrontSize() {
|
||
const ctx = require("../../common/ctx");
|
||
let curClientW = ctx.getScreenWidth();
|
||
if (!curClientW) {
|
||
return
|
||
}
|
||
//字体尺寸 https://www.shejidaren.com/zihao-daxiao-sheji-bilv.html
|
||
//12~72 px 建议最大字体是48px
|
||
|
||
let dpr = getDpr();
|
||
|
||
let myEditFrontSize = document.getElementById("myEdit_main");
|
||
if (ctx.isTablet) {
|
||
myEditFrontSize.style.fontSize = ctx.editFrontSize * dpr + 'px';
|
||
} else {
|
||
myEditFrontSize.style.fontSize = ctx.editFrontSize + 'px';
|
||
}
|
||
|
||
// console.log("myEditFrontSize: ", myEditFrontSize.style.fontSize);
|
||
let testDiv = document.getElementById("testDevice");
|
||
testDiv.innerText += "\ndpr: " + dpr + "\nmyEditFrontSize: " + myEditFrontSize.style.fontSize;
|
||
}
|
||
|
||
|
||
//导出
|
||
exports.handle = handle;
|
||
exports.refreshEditFrontSize = refreshEditFrontSize;
|
||
}); |