Vue屏幕自適應(yīng)三種實(shí)現(xiàn)方法詳解
使用 scale-box 組件
屬性:
width寬度 默認(rèn)1920height高度 默認(rèn)1080bgc背景顏色 默認(rèn)"transparent"delay自適應(yīng)縮放防抖延遲時(shí)間(ms) 默認(rèn)100
vue2版本:vue2大屏適配縮放組件(vue2-scale-box - npm)
npm install vue2-scale-box
或者
yarn add vue2-scale-box
使用方法:
<template>
<div>
<scale-box :width="1920" :height="1080" bgc="transparent" :delay="100">
<router-view />
</scale-box>
</div>
</template>
<script>
import ScaleBox from "vue2-scale-box";
export default {
components: { ScaleBox },
};
</script>
<style lang="scss">
body {
margin: 0;
padding: 0;
background: url("@/assets/bg.jpg");
}
</style>vue3版本:vue3大屏適配縮放組件(vue3-scale-box - npm)
npm install vue3-scale-box
或者
yarn add vue3-scale-box
使用方法:
<template>
<ScaleBox :width="1920" :height="1080" bgc="transparent" :delay="100">
<router-view />
</ScaleBox>
</template>
<script>
import ScaleBox from "vue3-scale-box";
</script>
<style lang="scss">
body {
margin: 0;
padding: 0;
background: url("@/assets/bg.jpg");
}
</style>設(shè)置設(shè)備像素比例(設(shè)備像素比)
在項(xiàng)目的 utils 下新建 devicePixelRatio.js 文件
class devicePixelRatio {
/* 獲取系統(tǒng)類(lèi)型 */
getSystem() {
const agent = navigator.userAgent.toLowerCase();
const isMac = /macintosh|mac os x/i.test(navigator.userAgent);
if (isMac) return false;
// 目前只針對(duì) win 處理,其它系統(tǒng)暫無(wú)該情況,需要?jiǎng)t繼續(xù)在此添加即可
if (agent.indexOf("windows") >= 0) return true;
}
/* 監(jiān)聽(tīng)方法兼容寫(xiě)法 */
addHandler(element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
} else {
element["on" + type] = handler;
}
}
/* 校正瀏覽器縮放比例 */
correct() {
// 頁(yè)面devicePixelRatio(設(shè)備像素比例)變化后,計(jì)算頁(yè)面body標(biāo)簽zoom修改其大小,來(lái)抵消devicePixelRatio帶來(lái)的變化
document.getElementsByTagName("body")[0].style.zoom =
1 / window.devicePixelRatio;
}
/* 監(jiān)聽(tīng)頁(yè)面縮放 */
watch() {
const that = this;
// 注意: 這個(gè)方法是解決全局有兩個(gè)window.resize
that.addHandler(window, "resize", function () {
that.correct(); // 重新校正瀏覽器縮放比例
});
}
/* 初始化頁(yè)面比例 */
init() {
const that = this;
// 判斷設(shè)備,只在 win 系統(tǒng)下校正瀏覽器縮放比例
if (that.getSystem()) {
that.correct(); // 校正瀏覽器縮放比例
that.watch(); // 監(jiān)聽(tīng)頁(yè)面縮放
}
}
}
export default devicePixelRatio;在App.vue 中引入并使用即可
<template>
<div>
<router-view />
</div>
</template>
<script>
import devPixelRatio from "@/utils/devicePixelRatio.js";
export default {
created() {
new devPixelRatio().init(); // 初始化頁(yè)面比例
},
};
</script>
<style lang="scss">
body {
margin: 0;
padding: 0;
}
</style>通過(guò)JS設(shè)置zoom屬性調(diào)整縮放比例
在項(xiàng)目的 utils 下新建 monitorZoom.js 文件
export const monitorZoom = () => {
let ratio = 0,
screen = window.screen,
ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
} else if (~ua.indexOf("msie")) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
} else if (
window.outerWidth !== undefined &&
window.innerWidth !== undefined
) {
ratio = window.outerWidth / window.innerWidth;
}
if (ratio) {
ratio = Math.round(ratio * 100);
}
return ratio;
};在main.js 中引入并使用即可
import { monitorZoom } from "@/utils/monitorZoom.js";
const m = monitorZoom();
if (window.screen.width * window.devicePixelRatio >= 3840) {
document.body.style.zoom = 100 / (Number(m) / 2); // 屏幕為 4k 時(shí)
} else {
document.body.style.zoom = 100 / Number(m);
}完整代碼
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
/* 調(diào)整縮放比例 start */
import { monitorZoom } from "@/utils/monitorZoom.js";
const m = monitorZoom();
if (window.screen.width * window.devicePixelRatio >= 3840) {
document.body.style.zoom = 100 / (Number(m) / 2); // 屏幕為 4k 時(shí)
} else {
document.body.style.zoom = 100 / Number(m);
}
/* 調(diào)整縮放比例 end */
Vue.config.productionTip = false;
new Vue({
router,
render: (h) => h(App),
}).$mount("#app");獲取屏幕的分辨率
獲取屏幕的寬:
window.screen.width * window.devicePixelRatio
獲取屏幕的高:
window.screen.height * window.devicePixelRatio
移動(dòng)端適配(使用 postcss-px-to-viewport 插件)
官網(wǎng):https://github.com/evrone/postcss-px-to-viewport
npm install postcss-px-to-viewport --save-dev
或者
yarn add -D postcss-px-to-viewport
配置適配插件的參數(shù)(在項(xiàng)目根目錄創(chuàng)建 .postcssrc.js 文件[與 src 目錄平級(jí)])粘貼以下代碼即可
module.exports = {
plugins: {
autoprefixer: {}, // 用來(lái)給不同的瀏覽器自動(dòng)添加相應(yīng)前綴,如-webkit-,-moz-等等
"postcss-px-to-viewport": {
unitToConvert: "px", // 需要轉(zhuǎn)換的單位,默認(rèn)為"px"
viewportWidth: 390, // UI設(shè)計(jì)稿的寬度
unitPrecision: 6, // 轉(zhuǎn)換后的精度,即小數(shù)點(diǎn)位數(shù)
propList: ["*"], // 指定轉(zhuǎn)換的css屬性的單位,*代表全部css屬性的單位都進(jìn)行轉(zhuǎn)換
viewportUnit: "vw", // 指定需要轉(zhuǎn)換成的視窗單位,默認(rèn)vw
fontViewportUnit: "vw", // 指定字體需要轉(zhuǎn)換成的視窗單位,默認(rèn)vw
selectorBlackList: ["wrap"], // 需要忽略的CSS選擇器,不會(huì)轉(zhuǎn)為視口單位,使用原有的px等單位
minPixelValue: 1, // 默認(rèn)值1,小于或等于1px則不進(jìn)行轉(zhuǎn)換
mediaQuery: false, // 是否在媒體查詢(xún)的css代碼中也進(jìn)行轉(zhuǎn)換,默認(rèn)false
replace: true, // 是否直接更換屬性值,而不添加備用屬性
exclude: [/node_modules/], // 忽略某些文件夾下的文件或特定文件,用正則做目錄名匹配,例如 'node_modules' 下的文件
landscape: false, // 是否處理橫屏情況
landscapeUnit: "vw", // 橫屏?xí)r使用的視窗單位,默認(rèn)vw
landscapeWidth: 2048 // 橫屏?xí)r使用的視口寬度
}
}
};到此這篇關(guān)于Vue屏幕自適應(yīng)三種實(shí)現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)Vue屏幕自適應(yīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中的局部過(guò)濾器和全局過(guò)濾器代碼實(shí)操
這篇文章主要分享的是vue中的局部過(guò)濾器和全局過(guò)濾器代碼實(shí)操,下面文章主要以代碼展現(xiàn),具有一的的知識(shí)參考性,需要的小伙伴可以參考一下2022-02-02
vue3+element?Plus使用el-tabs標(biāo)簽頁(yè)解決頁(yè)面刷新不回到默認(rèn)頁(yè)的問(wèn)題
這篇文章主要介紹了vue3+element?Plus使用el-tabs標(biāo)簽頁(yè)頁(yè)面刷新不回到默認(rèn)頁(yè)的操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
在vue中多次調(diào)用同一個(gè)定義全局變量的實(shí)例
今天小編就為大家分享一篇在vue中多次調(diào)用同一個(gè)定義全局變量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue組件實(shí)現(xiàn)旋轉(zhuǎn)木馬動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Vue組件實(shí)現(xiàn)旋轉(zhuǎn)木馬動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
如何解決d3.event在v7版本無(wú)效影響zoom拖拽縮放問(wèn)題
這篇文章主要介紹了如何解決d3.event在v7版本無(wú)效影響zoom拖拽縮放問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue中用js如何實(shí)現(xiàn)循環(huán)可編輯表格
這篇文章主要介紹了vue中用js如何實(shí)現(xiàn)循環(huán)可編輯表格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Element Table 自適應(yīng)高度的實(shí)現(xiàn)示例
el-table的高度不能適應(yīng)不同電腦的分辨率,也不能跟隨瀏覽器的高度變化而變化的問(wèn)題,本文就來(lái)解決一下Element Table 自適應(yīng)高度,感興趣的可以了解一下2024-07-07
vue-cli如何快速構(gòu)建vue項(xiàng)目
本篇文章主要介紹了vue-cli如何快速構(gòu)建vue項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

