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) { // 以高度為基準,按比例計算寬度 contentHeight.value = containerHeight; contentWidth.value = Math.floor(containerHeight * aspectRatio); } else { // 以寬度為基準,按比例計算高度 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ù)計算得到的寬高樣式設(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è)計稿尺寸,默認是 1920 x 1080 // 分別計算瀏覽器和設(shè)計圖寬高比 // 如果瀏覽器的寬高比大于設(shè)計稿的寬高比,就取瀏覽器高度和設(shè)計稿高度之比 // 如果瀏覽器的寬高比小于設(shè)計稿的寬高比,就取瀏覽器寬度和設(shè)計稿寬度之比 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(() => { // 初始化時為外層盒子加上縮放屬性,防止刷新界面時就已經(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
下載插件,自動將px
單位轉(zhuǎn)換成rem
單位
npm install postcss-px2rem --save
(2)在根目錄src中新建util目錄下新建rem.js等比適配文件
// rem等比適配配置文件 // 基準大小 const baseSize = 14 // 設(shè)置 rem 函數(shù) function setRem () { // 當前頁面寬度相對于 1920寬的縮放比例,可根據(jù)自己需要修改。 const scale = document.documentElement.clientWidth / 1920 // 設(shè)置頁面根節(jié)點字體大小(“Math.min(scale, 2)” 指最高放大比例為2,可根據(jù)實際業(yè)務(wù)需求調(diào)整) document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px' } // 初始化 setRem() // 改變窗口大小時重新設(shè)置 `rem` window.onresize = function () { setRem() }
(3)在main.js
中引入適配文件
import './util/rem'
(4)到vue.config.js
中配置插件
// 引入等比適配插件 const px2rem = require('postcss-px2rem') // 配置基本大小 const postcss = px2rem({ // 基準大小 baseSize,需要和rem.js中相同 // remUnit: 14 代表 1rem = 14px; 所以當你一個14px值時,它會自動轉(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面
本文給大家分享了vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面的一個功能,有這方便需要的朋友學(xué)習(xí)參考下吧。2018-01-01Ant Design Vue如何生成動態(tài)菜單a-menu
這篇文章主要介紹了Ant Design Vue如何生成動態(tài)菜單a-menu問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01淺談vue2 單頁面如何設(shè)置網(wǎng)頁title
這篇文章主要介紹了淺談vue2 單頁面如何設(shè)置網(wǎng)頁title,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11解決axios發(fā)送post請求返回400狀態(tài)碼的問題
今天小編就為大家分享一篇解決axios發(fā)送post請求返回400狀態(tài)碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08