Vue項(xiàng)目如何改變屏幕尺寸重新刷新頁面-計(jì)算頁面尺寸
改變屏幕尺寸重新刷新頁面-計(jì)算頁面尺寸
效果
在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)前頁面寬度相對于 1920寬的縮放比例,可根據(jù)自己需要修改。 const scale = document.documentElement.clientWidth / 1920 // 設(shè)置頁面根節(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,然后對內(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 文件同級,建一個(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)小球滑動(dòng)交叉效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)小球滑動(dòng)交叉,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09el-tree?loadNode懶加載的實(shí)現(xiàn)
本文主要介紹了el-tree?loadNode懶加載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08vue跳轉(zhuǎn)頁面的幾種常用方法實(shí)例總結(jié)
Vue是一種流行的JavaScript框架,用于構(gòu)建用戶界面,在Vue中,頁面跳轉(zhuǎn)通常使用路由(Router)來實(shí)現(xiàn),除此之外還有很多方法,這篇文章主要給大家介紹了關(guān)于vue跳轉(zhuǎn)頁面的幾種常用方法,需要的朋友可以參考下2024-05-05vue.js在標(biāo)簽屬性中插入變量參數(shù)的方法
這篇文章主要介紹了vue.js在標(biāo)簽屬性中插入變量參數(shù)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03vue.draggable實(shí)現(xiàn)表格拖拽排序效果
這篇文章主要為大家詳細(xì)介紹了vue.draggable實(shí)現(xiàn)表格拖拽排序效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12vue學(xué)習(xí)之Vue-Router用法實(shí)例分析
這篇文章主要介紹了vue學(xué)習(xí)之Vue-Router用法,結(jié)合實(shí)例形式分析了Vue-Router路由原理與常見操作技巧,需要的朋友可以參考下2020-01-01