1.自適屏幕糖儡,始終保持16:9
的比例
<!-- 大屏固定比例16:9自適應(yīng) -->
<template>
<div class="container">
<div class="content" :style="getAspectRatioStyle">
<!-- 數(shù)據(jù)展示內(nèi)容 -->
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
const contentWidth = ref(0);
const contentHeight = ref(0);
const calculateAspectRatio = () => {
const container = document.querySelector('.container');
// const containerWidth = container.offsetWidth;
const containerWidth: number = (<HTMLElement>container).offsetWidth;
// const containerHeight = container.offsetHeight;
const containerHeight: number = (<HTMLElement>container).offsetHeight;
const aspectRatio = 16 / 9; // 16:9 比例
const containerAspectRatio = containerWidth / containerHeight;
if (containerAspectRatio > aspectRatio) {
// 以高度為基準(zhǔn)擅威,按比例計(jì)算寬度
contentHeight.value = containerHeight;
contentWidth.value = Math.floor(containerHeight * aspectRatio);
} else {
// 以寬度為基準(zhǔn)篱瞎,按比例計(jì)算高度
contentWidth.value = containerWidth;
contentHeight.value = Math.floor(containerWidth / aspectRatio);
}
console.log('contentWidth',contentWidth.value)
console.log('contentHeight',contentHeight.value)
};
onMounted(() => {
calculateAspectRatio();
window.addEventListener('resize', calculateAspectRatio);
});
onBeforeUnmount(() => {
window.removeEventListener('resize', calculateAspectRatio);
});
const getAspectRatioStyle = computed(() => ({
width: `${contentWidth.value}px`,
height: `${contentHeight.value}px`,
margin: 'auto',
background: 'gray'
}
));
</script>
<style>
.container {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.content {
/* 根據(jù)計(jì)算得到的寬高樣式設(shè)置 */
}
</style>
2.使用CSS scale屬性對大屏幕做自適應(yīng)處理
<template>
<div class="login-container">
<div class="login-main" ref="dataScreenRef"></div>
</div>
</template>
<script setup>
const dataScreenRef = ref(null);
const width = 1920;
const height = 1080;
// 根據(jù)瀏覽器大小推斷縮放比例
// 首先要確定設(shè)計(jì)稿尺寸铭腕,默認(rèn)是 1920 x 1080
// 分別計(jì)算瀏覽器和設(shè)計(jì)圖寬高比
// 如果瀏覽器的寬高比大于設(shè)計(jì)稿的寬高比器钟,就取瀏覽器高度和設(shè)計(jì)稿高度之比
// 如果瀏覽器的寬高比小于設(shè)計(jì)稿的寬高比斑司,就取瀏覽器寬度和設(shè)計(jì)稿寬度之比
const getScale = (w = width, h = height) => {
let ww = window.innerWidth / w;
let wh = window.innerHeight / h;
return ww < wh ? ww : wh;
};
/* 瀏覽器監(jiān)聽 resize 事件 */
const resize = () => {
if (dataScreenRef.value) {
dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
}
};
onMounted(() => {
// 初始化時(shí)為外層盒子加上縮放屬性谴餐,防止刷新界面時(shí)就已經(jīng)縮放
if (dataScreenRef.value) {
dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
dataScreenRef.value.style.width = `${width}px`;
dataScreenRef.value.style.height = `${height}px`;
}
window.addEventListener("resize", resize);
});
</script>
<style scoped lang="scss">
.login-container {
width: 100%;
height: 100%;
transform-origin: 0 0;
position: relative;
}
.login-main {
width: 100%;
height: 100%;
position: absolute;
}
</style>
3.使用rem
(1)npm
下載插件,自動(dòng)將px
單位轉(zhuǎn)換成rem
單位
npm install postcss-px2rem --save
(2)在根目錄src中新建util目錄下新建rem.js等比適配文件
// rem等比適配配置文件
// 基準(zhǔn)大小
const baseSize = 14
// 設(shè)置 rem 函數(shù)
function setRem () {
// 當(dāng)前頁面寬度相對于 1920寬的縮放比例姻政,可根據(jù)自己需要修改。
const scale = document.documentElement.clientWidth / 1920
// 設(shè)置頁面根節(jié)點(diǎn)字體大衅裆ぁ(“Math.min(scale, 2)” 指最高放大比例為2汁展,可根據(jù)實(shí)際業(yè)務(wù)需求調(diào)整)
document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改變窗口大小時(shí)重新設(shè)置 `rem`
window.onresize = function () {
setRem()
}
(3)在main.js
中引入適配文件
import './util/rem'
(4)到vue.config.js
中配置插件
// 引入等比適配插件
const px2rem = require('postcss-px2rem')
// 配置基本大小
const postcss = px2rem({
// 基準(zhǔn)大小 baseSize,需要和rem.js中相同
// remUnit: 14 代表 1rem = 14px; 所以當(dāng)你一個(gè)14px值時(shí)厌殉,它會自動(dòng)轉(zhuǎn)成 (14px/14)rem
remUnit: 14
})
// 使用等比適配插件
module.exports = {
lintOnSave: true,
css: {
loaderOptions: {
less: {
javascriptEnabled: true,
},
postcss: {
plugins: [
postcss,
],
},
},
},
}