fu-rem 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.umd.js +47 -0
- package/package.json +1 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.setRem = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 动态设置根字体大小工具
|
|
9
|
+
* @param {Object} options 配置项
|
|
10
|
+
* @param {number} options.designWidth 设计稿宽度,默认 750
|
|
11
|
+
* @param {number} options.baseSize 基准值,默认 100
|
|
12
|
+
*/
|
|
13
|
+
function setRem(options = {}) {
|
|
14
|
+
const designWidth = options.designWidth || 750;
|
|
15
|
+
const baseSize = options.baseSize || 100;
|
|
16
|
+
let ticking = false;
|
|
17
|
+
|
|
18
|
+
function updateRem() {
|
|
19
|
+
const scale = document.documentElement.clientWidth / designWidth;
|
|
20
|
+
document.documentElement.style.fontSize = baseSize * scale + 'px';
|
|
21
|
+
ticking = false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function onResize() {
|
|
25
|
+
if (!ticking) {
|
|
26
|
+
window.requestAnimationFrame(updateRem);
|
|
27
|
+
ticking = true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 初始化
|
|
32
|
+
updateRem();
|
|
33
|
+
|
|
34
|
+
// 监听窗口变化
|
|
35
|
+
window.addEventListener('resize', onResize);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 支持 CommonJS 和 ESM
|
|
39
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
40
|
+
module.exports = setRem;
|
|
41
|
+
} else if (typeof window !== 'undefined') {
|
|
42
|
+
window.setRem = setRem;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return setRem;
|
|
46
|
+
|
|
47
|
+
}));
|