Vue中大屏適配和局部適配的方案總結(jié)
1.使用Mixins混入的方式解決自適應(yīng)適配功能
通用的 css3:scale 縮放方案,通過(guò) ref 指向頁(yè)面,屏幕改變時(shí)縮放內(nèi)容。項(xiàng)目的基準(zhǔn)尺寸是 1920px*1080px,所以支持同比例屏幕 100%填充,如果非同比例則會(huì)自動(dòng)計(jì)算比例居中填充,不足的部分則留白。
實(shí)現(xiàn)代碼 screenmixin.js
// * 默認(rèn)縮放值
const scale = {
width: '1',
height: '1',
};
// * 設(shè)計(jì)稿尺寸(px)
// const baseWidth = document.body.clientWidth;
// const baseHeight = document.body.clientHeight;
const baseWidth = 1920;
const baseHeight = 1080;
// * 需保持的比例(默認(rèn)1.77778)
const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5));
export default {
data() {
return {
// * 定時(shí)函數(shù)
drawTiming: null,
};
},
mounted() {
setTimeout(() => {
this.calcRate();
}, 200);
window.addEventListener('resize', this.resize);
},
created() {},
beforeDestroy() {
window.removeEventListener('resize', this.resize);
},
methods: {
calcRate() {
const appRef = this.$refs['appRef'];
if (!appRef) return;
// 當(dāng)前寬高比
const currentRate = parseFloat(
(window.innerWidth / window.innerHeight).toFixed(5),
);
if (appRef) {
if (currentRate > baseProportion) {
// 表示更寬
scale.width = (
(window.innerHeight * baseProportion) /
baseWidth
).toFixed(5);
scale.height = (window.innerHeight / baseHeight).toFixed(5);
appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
} else {
// 表示更高
scale.height = (
window.innerWidth /
baseProportion /
baseHeight
).toFixed(5);
scale.width = (window.innerWidth / baseWidth).toFixed(5);
appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
}
}
},
resize() {
clearTimeout(this.drawTiming);
this.drawTiming = setTimeout(() => {
this.calcRate();
}, 200);
},
},
};
頁(yè)面使用
<template>
<div ref="appRef" class="wrapper">
<div class="home-canvas">
內(nèi)容
</div>
</div>
</template>
<script>
import screenMinxi from '@/utils/screenmixin';
export default {
mixins: [screenMinxi],
};
</script>
<style lang="scss" scoped>
// 必需寫(xiě)寬高,如有單位轉(zhuǎn)換在style中寫(xiě)
.wrapper{
width: 1920px;
height: 1080px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transform-origin: left top; /* 縮放基點(diǎn)為左上角 */
overflow: hidden;
}
</style>
2.局部適配方案 mixins.js
export const scaleMixin = {
methods: {
// 計(jì)算縮放比例
getScaleRatio() {
const baseWidth = 1920; // 基準(zhǔn)寬度
const baseHeight = 1080; // 基準(zhǔn)高度
const screenWidth = window.innerWidth; // 屏幕寬度
const screenHeight = window.innerHeight; // 屏幕高度
const ratioX = screenWidth / baseWidth;
const ratioY = screenHeight / baseHeight;
return Math.min(ratioX, ratioY); // 取最小比例作為縮放比例
},
},
mounted() {
// 監(jiān)聽(tīng)窗口變化,重新計(jì)算縮放比例
window.addEventListener('resize', () => {
const scaleRatio = this.getScaleRatio();
this.$refs.wrapper.style.transform = `scale(${scaleRatio})`;
});
// 初始化縮放比例
const scaleRatio = this.getScaleRatio();
this.$refs.wrapper.style.transform = `scale(${scaleRatio})`;
},
};
引入使用
import { scaleMixin } from './mixins';
mixins: [scaleMixin], <div class="canvas-wrapper wrapper" style="width:1600px;heibht:890px;" ref="wrapper" >
<div class="">內(nèi)容</div>
</div>
樣式style
.wrapper {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
transform-origin: left top; /* 縮放基點(diǎn)為左上角 */
transform: scale(1); /* 初始化縮放比例 */
}
到此這篇關(guān)于Vue中大屏適配和局部適配的方案總結(jié)的文章就介紹到這了,更多相關(guān)Vue大屏適配和局部適配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
electron-vue?項(xiàng)目添加啟動(dòng)loading動(dòng)畫(huà)的實(shí)現(xiàn)思路
electron-vue腳手架搭建的項(xiàng)目,在開(kāi)發(fā)階段可能你注意不到項(xiàng)目啟動(dòng)慢的問(wèn)題,但是在build?生成的exe可執(zhí)行文件,啟動(dòng)后,要反應(yīng)很久才能進(jìn)入到app.vue?中加載的頁(yè)面,體驗(yàn)性很差,本文給大家介紹electron?vue啟動(dòng)動(dòng)畫(huà)效果的實(shí)例代碼,感興趣的朋友一起看看吧2022-01-01
Vue使用fabric.js實(shí)現(xiàn)局部截圖與大圖預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了Vue如何使用fabric.js實(shí)現(xiàn)局部截圖與el-image-viewer大圖預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的可以了解下2024-02-02
vue開(kāi)發(fā)中如何在js文件里使用pinia和組件同步
在JavaScript文件中封裝涉及到使用Pinia的方法時(shí),發(fā)現(xiàn)Pinia和組件內(nèi)容并不同步,二者的狀態(tài)是獨(dú)立的,解決辦法是,在新建對(duì)象的時(shí)候,將Pinia作為參數(shù)傳入,本文給大家介紹vue開(kāi)發(fā)中如何在js文件里使用pinia和組件同步,感興趣的朋友一起看看吧2024-10-10
vue-element-admin?登陸及目錄權(quán)限控制的實(shí)現(xiàn)
本文主要介紹了vue-element-admin?登陸及目錄權(quán)限控制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
vue路由跳轉(zhuǎn)到新頁(yè)面實(shí)現(xiàn)置頂
這篇文章主要介紹了vue路由跳轉(zhuǎn)到新頁(yè)面實(shí)現(xiàn)置頂問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)
vue中對(duì)數(shù)組的元素進(jìn)行刪除,以前一直以為這個(gè)方法是vue中特有的,后來(lái)百度之后才知道原來(lái)是js的一個(gè)寫(xiě)法,下面這篇文章主要給大家介紹了關(guān)于Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-05-05

