index.vue
2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<template>
<div id="ykite-full-screen-container" :style="style">
<slot />
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
// 用于getScale()函数
const fixedWidth = ref(document.documentElement.clientWidth.toString());
const fixedHeight = ref(document.documentElement.clientHeight.toString());
// 默认行内样式
const style = ref({
'width': `${fixedWidth.value}px`,
'height': `${fixedHeight.value}px`,
'transform': 'scale(1) translate(-50% -50%)',
})
onMounted(()=>{
setScale(); // 初始化时就执行一次setScale
window.onresize = debounce(setScale, 300); // 监听onresize事件,执行setSacle
})
const debounce = (fn, delay)=>{
let timer;
console.log('执行了debounce'); // 只执行一次,下面紧邻函数内的代码会在每次触发resize事件时执行
return function(){
// const args = arguments; // fn函数(即setScale函数)不必使用args作为fn的入参,
// apply()方法作用:https://blog.csdn.net/weixin_43877799/article/details/120282509
if(timer){
clearTimeout(timer);
}
const context = this;
timer = setTimeout(()=>{
timer = null;
console.log('正在执行自适应');
fn.apply(context); // 或者可以直接运行fn();
}, delay);
}
}
// 获取放大缩小比例
const getScale = ()=>{
const w = window.innerWidth / fixedWidth.value;
const h = window.innerHeight / fixedHeight.value;
return w<h? w: h;
}
// 修改样式
const setScale = ()=>{
style.value.transform = `scale${getScale()})) translate(-50% -50%)`;
// 宽高自适应,window.innerWidth见文章:https://juejin.cn/post/6844903598489337869
style.value.width = `${(window.innerWidth).toString()}px`; // 赋值时不要使用document.documentElement.clientWidth
style.value.height = `${(window.innerHeight).toString()}px`;// 赋值时不要使用document.documentElement.clientHeight
}
</script>
<style scoped lang="less">
#ykite-full-screen-container{
transform-origin: 0 0;
transition: 0.2s; // 增大该值可以比较明显地看到自适应过程
}
</style>