vfit 0.1.13 → 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/README.md +8 -2
- package/dist/fitscale.es.js +12 -5
- package/dist/fitscale.umd.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/scale.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm i vfit
|
|
|
13
13
|
import { createFitScale } from 'vfit'
|
|
14
14
|
import 'vfit/style.css'
|
|
15
15
|
|
|
16
|
-
app.use(createFitScale({ target: '#app', designHeight: 1080 }))
|
|
16
|
+
app.use(createFitScale({ target: '#app', designHeight: 1080, designWidth: 1920, scaleMode: 'auto' }))
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## 快速上手
|
|
@@ -63,9 +63,14 @@ app.use(createFitScale({ target: '#app', designHeight: 1080 }))
|
|
|
63
63
|
|
|
64
64
|
## API 精简版
|
|
65
65
|
|
|
66
|
-
- `createFitScale({ target?, designHeight? })`
|
|
66
|
+
- `createFitScale({ target?, designHeight?, designWidth?, scaleMode? })`
|
|
67
67
|
- `target`:监听缩放的容器(默认 `#app`)
|
|
68
68
|
- `designHeight`:设计稿高度(默认 `1080`)
|
|
69
|
+
- `designWidth`:设计稿宽度(默认 `1920`)
|
|
70
|
+
- `scaleMode`:`'height' | 'width' | 'auto'`(默认 `auto`)
|
|
71
|
+
- `height`:按高度缩放,比例为 `容器高度 / 设计稿高度`
|
|
72
|
+
- `width`:按宽度缩放,比例为 `容器宽度 / 设计稿宽度`
|
|
73
|
+
- `auto`:根据容器宽高比与设计比(`设计稿宽度 / 设计稿高度`)自动选择按宽或按高
|
|
69
74
|
- `useFitScale()`:在组件内获取当前缩放值 `Ref<number>`
|
|
70
75
|
- `FitContainer` 组件属性:
|
|
71
76
|
- `top/bottom/left/right?: number`
|
|
@@ -75,4 +80,5 @@ app.use(createFitScale({ target: '#app', designHeight: 1080 }))
|
|
|
75
80
|
## 小贴士
|
|
76
81
|
|
|
77
82
|
- 使用 `right` 时,缩放原点为右上角(更易实现右侧对齐元素)
|
|
83
|
+
- 同时设置 `bottom` 与 `right` 时,原点为右下角,对齐更稳定
|
|
78
84
|
- 首次接入时,务必引入样式:`import 'vfit/style.css'`
|
package/dist/fitscale.es.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { defineComponent, reactive, computed, inject, ref, watch, createElementBlock, openBlock, normalizeStyle, renderSlot } from "vue";
|
|
2
|
-
function observeScale(target, designHeight, onScale) {
|
|
2
|
+
function observeScale(target, designHeight, onScale, scaleMode = "auto", designWidth = 1920) {
|
|
3
3
|
const observer = new ResizeObserver((entries) => {
|
|
4
4
|
for (const entry of entries) {
|
|
5
5
|
const rectHeight = entry.contentRect.height;
|
|
6
6
|
const rectWidth = entry.contentRect.width;
|
|
7
7
|
let scaleVal;
|
|
8
|
-
if (
|
|
9
|
-
scaleVal = rectWidth / 1920 * 0.98;
|
|
10
|
-
} else {
|
|
8
|
+
if (scaleMode === "height") {
|
|
11
9
|
scaleVal = rectHeight / designHeight;
|
|
10
|
+
} else if (scaleMode === "width") {
|
|
11
|
+
scaleVal = rectWidth / designWidth;
|
|
12
|
+
} else {
|
|
13
|
+
const designRatio = designWidth / designHeight;
|
|
14
|
+
if (rectWidth / rectHeight < designRatio) {
|
|
15
|
+
scaleVal = rectWidth / designWidth;
|
|
16
|
+
} else {
|
|
17
|
+
scaleVal = rectHeight / designHeight;
|
|
18
|
+
}
|
|
12
19
|
}
|
|
13
20
|
onScale(scaleVal);
|
|
14
21
|
}
|
|
@@ -85,7 +92,7 @@ function createFitScale(options = {}) {
|
|
|
85
92
|
const target = rootEl || document.querySelector("#app");
|
|
86
93
|
observeScale(target, options.designHeight ?? 1080, (v) => {
|
|
87
94
|
fitScale.value = v;
|
|
88
|
-
});
|
|
95
|
+
}, options.scaleMode ?? "auto", options.designWidth ?? 1920);
|
|
89
96
|
app.provide(FitScaleKey, fitScale);
|
|
90
97
|
app.config.globalProperties.$fitScale = fitScale;
|
|
91
98
|
app.component("FitContainer", FitContainer);
|
package/dist/fitscale.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(t,e){typeof exports==="object"&&typeof module!=="undefined"?e(exports,require("vue")):typeof define==="function"&&define.amd?define(["exports","vue"],e):(t=typeof globalThis!=="undefined"?globalThis:t||self,e(t.FitScale={},t.Vue))})(this,function(t,e){"use strict";function o(t,e,o){const
|
|
1
|
+
(function(t,e){typeof exports==="object"&&typeof module!=="undefined"?e(exports,require("vue")):typeof define==="function"&&define.amd?define(["exports","vue"],e):(t=typeof globalThis!=="undefined"?globalThis:t||self,e(t.FitScale={},t.Vue))})(this,function(t,e){"use strict";function o(t,e,o,n="auto",i=1920){const r=new ResizeObserver(t=>{for(const r of t){const t=r.contentRect.height;const c=r.contentRect.width;let s;if(n==="height"){s=t/e}else if(n==="width"){s=c/i}else{const o=i/e;if(c/t<o){s=c/i}else{s=t/e}}o(s)}});r.observe(t);return r}const n=e.defineComponent({__name:"FitContainer",props:{scale:{type:Number,default:0},top:{type:Number},bottom:{type:Number},left:{type:Number},right:{type:Number},unit:{type:String,default:"px"},z:{type:Number,default:300}},setup(t){const o=e.reactive({scale:`scale(1)`,top:"auto",bottom:"auto",left:"auto",right:"auto"});const n=t;const i=e.computed(()=>{const t=n.right!==void 0;const e=n.bottom!==void 0;if(t&&e)return"100% 100%";if(t)return"100% 0";if(e)return"0 100%";return"0 0"});const r=e.inject(c,e.ref(1));e.watch([()=>n.scale,r],()=>{const t=n.scale&&n.scale>0?n.scale:(r==null?void 0:r.value)??1;o.scale=`scale(${t})`;const e=["top","bottom","left","right"];e.forEach(e=>{const i=n[e];if(n.unit==="%"){o[e]=i==void 0?"auto":`${i}${n.unit}`}else{o[e]=i==void 0?"auto":`${i*t}${n.unit}`}})},{immediate:true});return(t,r)=>(e.openBlock(),e.createElementBlock("div",{class:"fit-container",style:e.normalizeStyle({transform:o.scale,transformOrigin:i.value,top:o.top,bottom:o.bottom,left:o.left,right:o.right,zIndex:n.z})},[e.renderSlot(t.$slots,"default",{},void 0,true)],4))}});const i=(t,e)=>{const o=t.__vccOpts||t;for(const[n,i]of e){o[n]=i}return o};const r=i(n,[["__scopeId","data-v-1954a495"]]);const c=Symbol("FitScale");function s(t={}){const n=e.ref(1);return{install(e){const i=typeof t.target==="string"?document.querySelector(t.target):t.target;const s=i||document.querySelector("#app");o(s,t.designHeight??1080,t=>{n.value=t},t.scaleMode??"auto",t.designWidth??1920);e.provide(c,n);e.config.globalProperties.$fitScale=n;e.component("FitContainer",r)}}}function a(){const t=e.inject(c,e.ref(1));return t}t.FitContainer=r;t.FitScaleKey=c;t.createFitScale=s;t.useFitScale=a;Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export declare const FitScaleKey: unique symbol;
|
|
|
4
4
|
export type FitScaleOptions = {
|
|
5
5
|
target?: string | HTMLElement;
|
|
6
6
|
designHeight?: number;
|
|
7
|
+
designWidth?: number;
|
|
8
|
+
scaleMode?: 'height' | 'width' | 'auto';
|
|
7
9
|
};
|
|
8
10
|
export declare function createFitScale(options?: FitScaleOptions): {
|
|
9
11
|
install(app: App): void;
|
package/dist/scale.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function observeScale(target: HTMLElement, designHeight: number, onScale: (scale: number) => void): ResizeObserver;
|
|
1
|
+
export declare function observeScale(target: HTMLElement, designHeight: number, onScale: (scale: number) => void, scaleMode?: 'height' | 'width' | 'auto', designWidth?: number): ResizeObserver;
|