代码拉取完成,页面将自动刷新
"use strict";
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.elRectification = elRectification;
// type Ignore = Array<{ height: number, width: number, fontSize: number, scale: number, el: HTMLElement, dom: HTMLElement }>;
var currRenderDom = null;
var currelRectification = "";
var currelRectificationLevel = "";
var currelRectificationIsKeepRatio = "";
var resizeListener = null;
var timer = null;
var currScale = 1;
var isElRectification = false;
var autofit = {
isAutofitRunnig: false,
init: function (options, isShowInitTip) {
if (options === void 0) { options = {}; }
if (isShowInitTip === void 0) { isShowInitTip = true; }
if (isShowInitTip) {
console.log("autofit.js is running");
}
var _a = options, _b = _a.dw, dw = _b === void 0 ? 1920 : _b, _c = _a.dh, dh = _c === void 0 ? 1080 : _c, _d = _a.el, el = _d === void 0 ? typeof options === "string" ? options : "body" : _d, _e = _a.resize, resize = _e === void 0 ? true : _e, _f = _a.ignore, ignore = _f === void 0 ? [] : _f, _g = _a.transition, transition = _g === void 0 ? "none" : _g, _h = _a.delay, delay = _h === void 0 ? 0 : _h, _j = _a.limit, limit = _j === void 0 ? 0.1 : _j;
currRenderDom = el;
var dom = document.querySelector(el);
if (!dom) {
console.error("autofit: '".concat(el, "' is not exist"));
return;
}
var style = document.createElement("style");
var ignoreStyle = document.createElement("style");
style.lang = "text/css";
ignoreStyle.lang = "text/css";
style.id = "autofit-style";
ignoreStyle.id = "ignoreStyle";
style.innerHTML = "body {overflow: hidden;}";
var bodyEl = document.querySelector("body");
bodyEl.appendChild(style);
bodyEl.appendChild(ignoreStyle);
dom.style.height = "".concat(dh, "px");
dom.style.width = "".concat(dw, "px");
dom.style.transformOrigin = "0 0";
dom.style.overflow = "hidden";
keepFit(dw, dh, dom, ignore, limit);
resizeListener = function () {
clearTimeout(timer);
if (delay != 0)
timer = setTimeout(function () {
keepFit(dw, dh, dom, ignore, limit);
isElRectification &&
elRectification(currelRectification, currelRectificationIsKeepRatio, currelRectificationLevel);
}, delay);
else {
keepFit(dw, dh, dom, ignore, limit);
isElRectification &&
elRectification(currelRectification, currelRectificationIsKeepRatio, currelRectificationLevel);
}
};
resize && window.addEventListener("resize", resizeListener);
this.isAutofitRunnig = true;
setTimeout(function () {
dom.style.transition = "".concat(transition, "s");
});
},
off: function (el) {
var _a;
if (el === void 0) { el = "body"; }
try {
isElRectification = false;
window.removeEventListener("resize", resizeListener);
(_a = document.querySelector("#autofit-style")) === null || _a === void 0 ? void 0 : _a.remove();
var ignoreStyleDOM = document.querySelector("#ignoreStyle");
ignoreStyleDOM && ignoreStyleDOM.remove();
var temp = document.querySelector(currRenderDom ? currRenderDom : el);
if (temp) {
// @ts-ignore
temp.style = "";
}
isElRectification && offelRectification();
}
catch (error) {
console.error("autofit: Failed to remove normally", error);
this.isAutofitRunnig = false;
}
this.isAutofitRunnig && console.log("autofit.js is off");
},
elRectification: null,
};
function elRectification(el, isKeepRatio, level) {
var e_1, _a;
if (isKeepRatio === void 0) { isKeepRatio = true; }
if (level === void 0) { level = 1; }
if (!autofit.isAutofitRunnig) {
console.error("autofit.js:autofit has not been initialized yet");
}
!el && console.error("autofit.js\uFF1Abad selector: ".concat(el));
currelRectification = el;
currelRectificationLevel = level;
currelRectificationIsKeepRatio = isKeepRatio;
var currEl = Array.from(document.querySelectorAll(el));
if (currEl.length == 0) {
console.error("autofit.js:elRectification found no element");
return;
}
try {
for (var currEl_1 = __values(currEl), currEl_1_1 = currEl_1.next(); !currEl_1_1.done; currEl_1_1 = currEl_1.next()) {
var item = currEl_1_1.value;
var rectification = currScale == 1 ? 1 : Number(currScale) * Number(level);
if (!isElRectification) {
item.originalWidth = item.clientWidth;
item.originalHeight = item.clientHeight;
}
if (isKeepRatio) {
item.style.width = "".concat(item.originalWidth * rectification, "px");
item.style.height = "".concat(item.originalHeight * rectification, "px");
}
else {
item.style.width = "".concat(100 * rectification, "%");
item.style.height = "".concat(100 * rectification, "%");
}
item.style.transform = "scale(".concat(1 / Number(currScale), ")");
item.style.transformOrigin = "0 0";
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (currEl_1_1 && !currEl_1_1.done && (_a = currEl_1.return)) _a.call(currEl_1);
}
finally { if (e_1) throw e_1.error; }
}
isElRectification = true;
}
function offelRectification() {
var e_2, _a;
if (!currelRectification)
return;
try {
for (var _b = __values(Array.from(document.querySelectorAll(currelRectification))), _c = _b.next(); !_c.done; _c = _b.next()) {
var item = _c.value;
item.style.width = "";
item.style.height = "";
item.style.transform = "";
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
}
function keepFit(dw, dh, dom, ignore, limit) {
var e_3, _a;
var clientHeight = document.documentElement.clientHeight;
var clientWidth = document.documentElement.clientWidth;
currScale =
clientWidth / clientHeight < dw / dh ? clientWidth / dw : clientHeight / dh;
currScale = Math.abs(1 - currScale) > limit ? currScale.toFixed(2) : 1;
var height = Math.round(clientHeight / Number(currScale));
var width = Math.round(clientWidth / Number(currScale));
dom.style.height = "".concat(height, "px");
dom.style.width = "".concat(width, "px");
dom.style.transform = "scale(".concat(currScale, ")");
var ignoreStyleDOM = document.querySelector("#ignoreStyle");
ignoreStyleDOM.innerHTML = "";
try {
for (var _b = __values(ignore), _c = _b.next(); !_c.done; _c = _b.next()) {
var temp = _c.value;
var item = temp;
var itemEl = item.el || item.dom;
typeof item == "string" && (itemEl = item);
if (!itemEl) {
console.error("autofit: bad selector: ".concat(itemEl));
continue;
}
var realScale = item.scale ? item.scale : 1 / Number(currScale);
var realFontSize = realScale != currScale ? item.fontSize : "autofit";
var realWidth = realScale != currScale ? item.width : "autofit";
var realHeight = realScale != currScale ? item.height : "autofit";
var regex = new RegExp("".concat(itemEl, "( |{)"), "gm");
var isIgnored = regex.test(ignoreStyleDOM.innerHTML);
if (isIgnored) {
continue;
}
ignoreStyleDOM.innerHTML += "\n".concat(itemEl, " { \n transform: scale(").concat(realScale, ")!important;\n transform-origin: 0 0;\n width: ").concat(realWidth, "!important;\n height: ").concat(realHeight, "!important;\n }");
if (realFontSize) {
ignoreStyleDOM.innerHTML += "\n".concat(itemEl, " div ,").concat(itemEl, " span,").concat(itemEl, " a,").concat(itemEl, " * {\n font-size: ").concat(realFontSize, "px;\n }");
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_3) throw e_3.error; }
}
}
// UMD导出需要,UMD导出使用的 export default
// 所以 export 函数|变量|常量 导出的,都需要挂载到 autofit 上,也就是在 export default autofit 上了
autofit.elRectification = elRectification;
exports.default = autofit;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。