Vue項(xiàng)目如何改變屏幕尺寸重新刷新頁(yè)面-計(jì)算頁(yè)面尺寸
改變屏幕尺寸重新刷新頁(yè)面-計(jì)算頁(yè)面尺寸
效果

在app.vue中設(shè)置
方式一
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
name: "App",
components: {},
data() {
return {};
},
created() {},
mounted() {
//窗口尺寸改變
window.onresize = () => {
return (() => {
// this.$forceUpdate();//強(qiáng)制更新數(shù)據(jù)
this.$router.go(0);
})();
};
},
methods: {},
destroyed() {
// 銷毀
window.onresize = null;
},
};
</script>多次使用window.onresize,會(huì)造成其他的失效
方式二
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
export default {
name: "App",
components: {},
data() {
return {};
},
created() {},
mounted() {
//窗口尺寸改變
window.addEventListener("resize", () => this.Refresh());
},
methods: {
Refresh() {
this.$router.go(0);
},
},
destroyed() {
// 銷毀
window.removeEventListener("resize", this.Refresh());
},
};
</script>尺寸的自適應(yīng) 大屏自適應(yīng)
我在一些大屏的項(xiàng)目中,碰見自己電腦寫好的樣式,但是在大屏中出現(xiàn)了變形。
通過 postcss-px2rem 插件,實(shí)現(xiàn)項(xiàng)目的自適應(yīng)
使用:
** 1、安裝包**
npm install postcss-px2rem px2rem-loader --save
2、文件創(chuàng)建
在src目錄下創(chuàng)建一個(gè)util的工具文件夾,然后創(chuàng)建一個(gè)js文件,這里我叫pxtorem.js,文件中寫上一下代碼
// rem等比適配配置文件
// 基準(zhǔn)大小
const baseSize = 16
// 設(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文件中引入剛剛創(chuàng)建好的文件
import './util/pxtorem'
4、在vue.config.js文件中配置
如果項(xiàng)目中沒有這個(gè)文件,可以自己手動(dòng)在根目錄下創(chuàng)建一下
// 引入等比適配插件
const px2rem = require('postcss-px2rem')
// 配置基本大小
const postcss = px2rem({
? // 基準(zhǔn)大小 baseSize,需要和rem.js中相同
? remUnit: 16
})
// 使用等比適配插件
module.exports = {
? lintOnSave: true,
? css: {
? ? loaderOptions: {
? ? ? postcss: {
? ? ? ? plugins: [
? ? ? ? ? postcss
? ? ? ? ]
? ? ? }
? ? }
? }
}通過transform,可用于echarts的項(xiàng)目中
ps:echart中的適應(yīng),你也可以用echarts自帶的resize,然后對(duì)內(nèi)部的文字字體大小設(shè)置一個(gè)值,進(jìn)行變化
<template>
<div id="app">
<div ref="newCatalog" class="newCatalog">
<router-view />
</div>
</div>
</template>
<script>
export default {
name: 'App',
data() {
return {
scale: ''
}
},
mounted() {
this.setScale()
window.addEventListener('resize', this.setScale)
},
methods: {
getScale() {
const width = window.screen.width
const height = window.screen.height
let ww = window.innerWidth / 1920
let wh = window.innerHeight / 1080
return ww < wh ? ww : wh
},
setScale() {
this.scale = this.getScale()
this.$refs.newCatalog.style.setProperty('--scale', this.scale)
}
}
}
</script>
<style lang="scss">
@import '~style/init.scss';
#app {
width: 100vw;
height: 100vh;
font-family: Alibaba-PuHuiTi-R, Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
background: url('~assets/images/bg.png');
background-size: cover;
}
.newCatalog {
--scale: 1;
width: 1920px;
height: 1080px;
background-size: 100% 100%;
position: absolute;
transform: scale(var(--scale)) translate(-50%, -50%);
display: flex;
flex-direction: column;
transform-origin: 0 0;
left: 50%;
top: 50%;
}
</style>
通過 postcss-px-to-viewport 插件,實(shí)現(xiàn)項(xiàng)目的自適應(yīng)
和vue.config.js 文件同級(jí),建一個(gè) postcss.config.js 的文件
module.exports = {
plugins: {
'postcss-px-to-viewport': {
unitToConvert: 'px', // 需要轉(zhuǎn)換的單位,默認(rèn)為"px"
viewportWidth: 1920, // 設(shè)計(jì)稿的視口寬度
unitPrecision: 5, // 單位轉(zhuǎn)換后保留的精度
propList: ['*'], // 能轉(zhuǎn)化為vw的屬性列表
viewportUnit: 'vw', // 希望使用的視口單位
fontViewportUnit: 'vw', // 字體使用的視口單位
selectorBlackList: [], // 需要忽略的CSS選擇器,不會(huì)轉(zhuǎn)為視口單位,使用原有的px等單位。
minPixelValue: 1, // 設(shè)置最小的轉(zhuǎn)換數(shù)值,如果為1的話,只有大于1的值會(huì)被轉(zhuǎn)換
mediaQuery: false, // 媒體查詢里的單位是否需要轉(zhuǎn)換單位
replace: true, // 是否直接更換屬性值,而不添加備用屬性
exclude: [/node_modules/, /LargeScreen/], // 忽略某些文件夾下的文件或特定文件,例如 'node_modules' 下的文件,數(shù)組中寫正則
include: undefined, // 如果設(shè)置了include,那將只有匹配到的文件才會(huì)被轉(zhuǎn)換
landscape: false, // 是否添加根據(jù) landscapeWidth 生成的媒體查詢條件 @media (orientation: landscape)
landscapeUnit: 'vw', // 橫屏?xí)r使用的單位
landscapeWidth: 1920, // 橫屏?xí)r使用的視口寬度
},
},
};
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE 實(shí)現(xiàn)element upload上傳圖片到阿里云
這篇文章主要介紹了VUE 實(shí)現(xiàn)element upload上傳圖片到阿里云,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-08-08
基于vue+h5實(shí)現(xiàn)車牌號(hào)輸入框功能(demo)
最近開發(fā)項(xiàng)目是學(xué)校校內(nèi)車輛超速方面的統(tǒng)計(jì)檢測(cè)方面的系統(tǒng),在開發(fā)過程中發(fā)現(xiàn)有個(gè)小功能,就是用戶移動(dòng)端添加車牌號(hào),剛開始想著輸入框,提交時(shí)正則效驗(yàn)一下格式,最后感覺不方便,所以就簡(jiǎn)單自己手寫了一個(gè)H5車牌號(hào)軟鍵盤,對(duì)vue車牌號(hào)輸入框?qū)崿F(xiàn)代碼感興趣的朋友一起看看吧2025-03-03
基于Vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的方位動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了如何基于Vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的方位動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式
這篇文章主要介紹了vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
詳解Vue如何將多個(gè)空格被合并顯示成一個(gè)空格
這篇文章主要為大家詳細(xì)介紹了在Vue中如何將多個(gè)空格被合并顯示成一個(gè)空格,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
手把手教你使用electron將vue項(xiàng)目打包成exe
Electron相當(dāng)于一個(gè)瀏覽器的外殼,可以把現(xiàn)有的vue程序嵌入到殼里面,下面這篇文章主要給大家介紹了關(guān)于如何使用electron將vue項(xiàng)目打包成exe的相關(guān)資料,需要的朋友可以參考下2023-01-01
Vue彈窗的兩種實(shí)現(xiàn)方式實(shí)例詳解
這篇文章主要介紹了Vue彈窗的兩種實(shí)現(xiàn)方式,一種使用.sync修飾符另一種使用v-model,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

