Vue使得大屏自適應(yīng)的多種方法
VUE學(xué)習(xí)大屏自適應(yīng)的幾種方法
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屬性對(duì)大屏幕做自適應(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)聽(tīng) 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)前頁(yè)面寬度相對(duì)于 1920寬的縮放比例,可根據(jù)自己需要修改。
const scale = document.documentElement.clientWidth / 1920
// 設(shè)置頁(yè)面根節(jié)點(diǎn)字體大?。ā癕ath.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í),它會(huì)自動(dòng)轉(zhuǎn)成 (14px/14)rem
remUnit: 14
})
// 使用等比適配插件
module.exports = {
lintOnSave: true,
css: {
loaderOptions: {
less: {
javascriptEnabled: true,
},
postcss: {
plugins: [
postcss,
],
},
},
},
}到此這篇關(guān)于Vue使得大屏自適應(yīng)的多種方法的文章就介紹到這了,更多相關(guān)vue大屏幕自適應(yīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE 動(dòng)態(tài)組件的應(yīng)用案例分析
這篇文章主要介紹了VUE 動(dòng)態(tài)組件的應(yīng)用,結(jié)合具體案例形式分析了vue.js動(dòng)態(tài)組件的應(yīng)用場(chǎng)景、解決方案及相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
vue中關(guān)于_ob_:observer的處理方式
這篇文章主要介紹了vue中關(guān)于_ob_:observer的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求loading操作示例
這篇文章主要介紹了vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求loading操作,結(jié)合實(shí)例形式分析了vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求過(guò)程中l(wèi)oading遮罩層相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-02-02
vue2 mint-ui loadmore實(shí)現(xiàn)下拉刷新,上拉更多功能
這篇文章主要介紹了vue2 mint-ui loadmore實(shí)現(xiàn)下拉刷新,上拉更多功能,需要的朋友可以參考下2018-03-03
關(guān)于vue中@click.native.prevent的說(shuō)明
這篇文章主要介紹了關(guān)于vue中@click.native.prevent的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

