mylomen-server/static/js/event/impl/RSizeEventImpl.js

118 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-11-05 22:43:34 +08:00
"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();
}
/**
2024-11-08 10:09:31 +08:00
* 刷新跟节点 front-size for 全局盒子尺寸
2024-11-05 22:43:34 +08:00
* @constructor
*/
function refreshRootFrontSize() {
const ctx = require("../../common/ctx");
2024-11-08 10:09:31 +08:00
let designWidth = ctx.designWith;
2024-11-05 22:43:34 +08:00
let curDoc = document.documentElement;//当前文档的 root 元素
let curClientW = ctx.getScreenWidth();
if (!curClientW) {
2024-11-08 10:09:31 +08:00
curClientW = designWidth;
2024-11-05 22:43:34 +08:00
}
2024-11-08 10:09:31 +08:00
2024-11-08 10:35:10 +08:00
let dpr = getDpr();
2024-11-08 10:09:31 +08:00
//set 1rem = curClientW / designWidth (支持响应式)
2024-11-08 10:35:10 +08:00
let nowFrontSize = '1px';
2024-11-08 10:25:30 +08:00
if (ctx.isTablet || ctx.isMobile || ctx.isIOS || ctx.isAndroid) {
2024-11-08 10:35:10 +08:00
nowFrontSize = (curClientW / designWidth) * dpr + 'px';
2024-11-08 10:25:30 +08:00
} else {
2024-11-08 10:35:10 +08:00
nowFrontSize = (curClientW / designWidth) + 'px';
2024-11-08 10:25:30 +08:00
}
2024-11-08 10:35:10 +08:00
// curDoc.style.fontSize = dpr * nowFrontSize;
curDoc.style.fontSize = nowFrontSize;
2024-11-05 22:43:34 +08:00
console.log("curClientW :", curClientW, "designWidth: ", designWidth, "-> ", nowFrontSize);
let testDiv = document.getElementById("testDevice");
2024-11-08 10:09:31 +08:00
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;
2024-11-05 22:43:34 +08:00
// 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;
}
2024-11-08 10:25:30 +08:00
function getDpr() {
2024-11-08 10:09:31 +08:00
//获取屏幕像素密度
2024-11-05 22:43:34 +08:00
let dpr = window.devicePixelRatio || 1;//当前设置下 物理像素和虚拟像素的比值
if (!dpr) {
//devicePixelRatio这个属性是可以获取到设备的dpr
2024-11-08 10:25:30 +08:00
let devicePixelRatio = window?.devicePixelRatio;
2024-11-05 22:43:34 +08:00
//判断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;
}
}
2024-11-08 10:25:30 +08:00
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
//1272 px 建议最大字体是48px
let dpr = getDpr();
2024-11-05 22:43:34 +08:00
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");
2024-11-08 10:09:31 +08:00
testDiv.innerText += "\ndpr: " + dpr + "\nmyEditFrontSize: " + myEditFrontSize.style.fontSize;
2024-11-05 22:43:34 +08:00
}
//导出
exports.handle = handle;
exports.refreshEditFrontSize = refreshEditFrontSize;
});