Vue中大屏適配和局部適配的方案總結(jié)
1.使用Mixins混入的方式解決自適應(yīng)適配功能
通用的 css3:scale 縮放方案,通過 ref 指向頁面,屏幕改變時(shí)縮放內(nèi)容。項(xiàng)目的基準(zhǔn)尺寸是 1920px*1080px,所以支持同比例屏幕 100%填充,如果非同比例則會自動計(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); }, }, };
頁面使用
<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> // 必需寫寬高,如有單位轉(zhuǎn)換在style中寫 .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)聽窗口變化,重新計(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
electron-vue?項(xiàng)目添加啟動loading動畫的實(shí)現(xiàn)思路
electron-vue腳手架搭建的項(xiàng)目,在開發(fā)階段可能你注意不到項(xiàng)目啟動慢的問題,但是在build?生成的exe可執(zhí)行文件,啟動后,要反應(yīng)很久才能進(jìn)入到app.vue?中加載的頁面,體驗(yàn)性很差,本文給大家介紹electron?vue啟動動畫效果的實(shí)例代碼,感興趣的朋友一起看看吧2022-01-01Vue使用fabric.js實(shí)現(xiàn)局部截圖與大圖預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了Vue如何使用fabric.js實(shí)現(xiàn)局部截圖與el-image-viewer大圖預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的可以了解下2024-02-02vue開發(fā)中如何在js文件里使用pinia和組件同步
在JavaScript文件中封裝涉及到使用Pinia的方法時(shí),發(fā)現(xiàn)Pinia和組件內(nèi)容并不同步,二者的狀態(tài)是獨(dú)立的,解決辦法是,在新建對象的時(shí)候,將Pinia作為參數(shù)傳入,本文給大家介紹vue開發(fā)中如何在js文件里使用pinia和組件同步,感興趣的朋友一起看看吧2024-10-10vue-element-admin?登陸及目錄權(quán)限控制的實(shí)現(xiàn)
本文主要介紹了vue-element-admin?登陸及目錄權(quán)限控制的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04vue路由跳轉(zhuǎn)到新頁面實(shí)現(xiàn)置頂
這篇文章主要介紹了vue路由跳轉(zhuǎn)到新頁面實(shí)現(xiàn)置頂問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Vue中splice()方法對數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)
vue中對數(shù)組的元素進(jìn)行刪除,以前一直以為這個(gè)方法是vue中特有的,后來百度之后才知道原來是js的一個(gè)寫法,下面這篇文章主要給大家介紹了關(guān)于Vue中splice()方法對數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-05-05