fu-rem 1.0.0

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/README.md ADDED
@@ -0,0 +1,26 @@
1
+
2
+ # fu-rem
3
+
4
+ > 动态设置根字体大小的工具
5
+
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install fu-rem
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```js
16
+ import setRem from 'fu-rem';
17
+
18
+ // 初始化,默认设计稿宽度为 750,根字体大小为 100px
19
+ setRem();
20
+
21
+ // 自定义设计稿宽度和根字体大小
22
+ setRem({
23
+ designWidth: 750,
24
+ baseSize: 100
25
+ });
26
+ ```
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 动态设置根字体大小工具
5
+ * @param {Object} options 配置项
6
+ * @param {number} options.designWidth 设计稿宽度,默认 750
7
+ * @param {number} options.baseSize 基准值,默认 100
8
+ */
9
+ function setRem(options = {}) {
10
+ const designWidth = options.designWidth || 750;
11
+ const baseSize = options.baseSize || 100;
12
+ let ticking = false;
13
+
14
+ function updateRem() {
15
+ const scale = document.documentElement.clientWidth / designWidth;
16
+ document.documentElement.style.fontSize = baseSize * scale + 'px';
17
+ ticking = false;
18
+ }
19
+
20
+ function onResize() {
21
+ if (!ticking) {
22
+ window.requestAnimationFrame(updateRem);
23
+ ticking = true;
24
+ }
25
+ }
26
+
27
+ // 初始化
28
+ updateRem();
29
+
30
+ // 监听窗口变化
31
+ window.addEventListener('resize', onResize);
32
+ }
33
+
34
+ // 支持 CommonJS 和 ESM
35
+ if (typeof module !== 'undefined' && module.exports) {
36
+ module.exports = setRem;
37
+ } else if (typeof window !== 'undefined') {
38
+ window.setRem = setRem;
39
+ }
40
+
41
+ module.exports = setRem;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * 动态设置根字体大小工具
3
+ * @param {Object} options 配置项
4
+ * @param {number} options.designWidth 设计稿宽度,默认 750
5
+ * @param {number} options.baseSize 基准值,默认 100
6
+ */
7
+ function setRem(options = {}) {
8
+ const designWidth = options.designWidth || 750;
9
+ const baseSize = options.baseSize || 100;
10
+ let ticking = false;
11
+
12
+ function updateRem() {
13
+ const scale = document.documentElement.clientWidth / designWidth;
14
+ document.documentElement.style.fontSize = baseSize * scale + 'px';
15
+ ticking = false;
16
+ }
17
+
18
+ function onResize() {
19
+ if (!ticking) {
20
+ window.requestAnimationFrame(updateRem);
21
+ ticking = true;
22
+ }
23
+ }
24
+
25
+ // 初始化
26
+ updateRem();
27
+
28
+ // 监听窗口变化
29
+ window.addEventListener('resize', onResize);
30
+ }
31
+
32
+ // 支持 CommonJS 和 ESM
33
+ if (typeof module !== 'undefined' && module.exports) {
34
+ module.exports = setRem;
35
+ } else if (typeof window !== 'undefined') {
36
+ window.setRem = setRem;
37
+ }
38
+
39
+ export { setRem as default };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "fu-rem",
3
+ "version": "1.0.0",
4
+ "description": "动态设置根字体大小的工具",
5
+ "main": "src/index.js",
6
+ "module": "src/index.js",
7
+ "repository": "https://github.com/weijunfu/fu-rem.git",
8
+ "keywords": [
9
+ "rem",
10
+ "responsive",
11
+ "font-size",
12
+ "root",
13
+ "fu",
14
+ "ijunfu",
15
+ "rollup",
16
+ "commonjs",
17
+ "ESM"
18
+ ],
19
+ "author": "ijunfu",
20
+ "license": "MIT",
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "scripts": {
26
+ "build": "rollup -c",
27
+ "prepublishOnly": "npm run build"
28
+ },
29
+ "devDependencies": {
30
+ "@rollup/plugin-commonjs": "^29.0.0",
31
+ "@rollup/plugin-node-resolve": "^16.0.3",
32
+ "rollup": "^4.55.2"
33
+ }
34
+ }
package/src/index.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * 动态设置根字体大小工具
3
+ * @param {Object} options 配置项
4
+ * @param {number} options.designWidth 设计稿宽度,默认 750
5
+ * @param {number} options.baseSize 基准值,默认 100
6
+ */
7
+ function setRem(options = {}) {
8
+ const designWidth = options.designWidth || 750;
9
+ const baseSize = options.baseSize || 100;
10
+ let ticking = false;
11
+
12
+ function updateRem() {
13
+ const scale = document.documentElement.clientWidth / designWidth;
14
+ document.documentElement.style.fontSize = baseSize * scale + 'px';
15
+ ticking = false;
16
+ }
17
+
18
+ function onResize() {
19
+ if (!ticking) {
20
+ window.requestAnimationFrame(updateRem);
21
+ ticking = true;
22
+ }
23
+ }
24
+
25
+ // 初始化
26
+ updateRem();
27
+
28
+ // 监听窗口变化
29
+ window.addEventListener('resize', onResize);
30
+ }
31
+
32
+ // 支持 CommonJS 和 ESM
33
+ if (typeof module !== 'undefined' && module.exports) {
34
+ module.exports = setRem;
35
+ } else if (typeof window !== 'undefined') {
36
+ window.setRem = setRem;
37
+ }
38
+ export default setRem;