济菏高速业务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.2 KiB

1 year ago
<template>
1 year ago
<div class="Adaptation" :style="transformStyle" ref="AdaptationRef">
1 year ago
<slot />
</div>
</template>
<script>
export default {
name: "Adaptation",
props: {
defaultWidth: {
type: Number,
default: () => 1920
},
defaultHeight: {
type: Number,
default: () => 1080
},
headerHeight: {
type: Number,
default: () => 68
1 year ago
}
},
provide() {
return {
adpScale: this.getScale
}
},
data() {
return {
scale: {
scaleX: 1,
scaleY: 1,
}
}
},
computed: {
transformStyle() {
const obj = {};
obj["transform"] = `scaleX(${this.scale.scaleX}) scaleY(${this.scale.scaleY})`;
obj["width"] = this.defaultWidth + "px";
obj["height"] = this.defaultHeight - this.headerHeight + "px";
obj["top"] = this.headerHeight + "px";
1 year ago
// obj.top = `calc(50% - ${(this.defaultHeight / 2) * this.scaleY}px)`;
// obj.left = `calc(50% - ${(this.defaultWidth / 2) * this.scaleX}px)`;
return obj;
},
getScale() {
return this.scale
}
},
watch:{
headerHeight(){
}
},
1 year ago
methods: {
initScale() {
let winW = window.innerWidth;
let winH = window.innerHeight - this.headerHeight;
this.setScale(this.scale.scaleX = winW / this.defaultWidth, this.scale.scaleY = winH / (this.defaultHeight - this.headerHeight))
},
setScale(scaleX, scaleY) {
const cssGlobalVariable = [
[
'--keep-ratio',
''
],
[
'--reverse-scale',
''
],
1 year ago
[
'--middle-scale',
''
],
];
if (scaleX > scaleY) {
cssGlobalVariable[0][1] = `scaleX(${1 / scaleX * scaleY})`;
// cssGlobalVariable[0][1] = `scaleX(${1 - (scaleX - scaleY)})`;
cssGlobalVariable[1][1] = `scaleY(${1 / scaleX})`;
1 year ago
cssGlobalVariable[2][1] = `scaleX(${1 - (scaleX - scaleY) / 2})`;
} else {
cssGlobalVariable[0][1] = `scaleY(${1 / scaleY * scaleX})`;
// cssGlobalVariable[0][1] = `scaleX(${1 - (scaleY - scaleX)})`;
cssGlobalVariable[1][1] = `scaleX(${1 / scaleY})`;
1 year ago
cssGlobalVariable[2][1] = `scaleX(${1 - (scaleY - scaleX) / 2})`;
}
1 year ago
const AdaptationDom = this.$refs.AdaptationRef;
cssGlobalVariable.forEach((data) => {
1 year ago
AdaptationDom.style.setProperty(...data);
});
1 year ago
}
},
mounted() {
this.initScale();
1 year ago
window.addEventListener(
"resize",
_.debounce(this.initScale.bind(this), 360)
);
},
};
</script>
<style lang="scss">
body {
.keep-ratio {
transform: var(--keep-ratio);
1 year ago
}
1 year ago
.reverse-ratio {
transform: var(--reverse-scale);
}
1 year ago
.middle-ratio {
transform: var(--middle-scale);
}
1 year ago
.keep-ratio,
.reverse-ratio,
.middle-ratio {
transition: all .15s linear;
1 year ago
&[origin="left"] {
transform-origin: left;
}
1 year ago
&[origin="right"] {
transform-origin: right;
}
1 year ago
&[origin="top"] {
transform-origin: top;
}
1 year ago
&[origin="bottom"] {
transform-origin: bottom;
}
}
}
</style>
1 year ago
<style lang="scss" scoped>
.Adaptation {
transition: all 0.15s linear;
position: fixed;
1 year ago
transform-origin: 0 0;
}
</style>