一、前置知識
- vw:與視口的寬度有關,1vw 就是視口寬度的 1%
- vh:與視口的高度有關,1vh 就是視口高度的 1%
- vmin:與當下視口的寬度和高度的最小值有關,取值為
vw和vh中較小的那個- vmax:與當下視口的寬度和高度的最大值有關,取值為
vw和vh中較大的那個- vmin 可以照顧移動端(手機端)橫屏和豎屏的顯示效果,保證移動開發中螢屏
旋轉之后的尺寸不變- vw 橫屏:顯示效果不好
- vmin 橫屏:顯示效果好,專案需要橫屏的時候,可以采用vmin
二、安裝依賴
yarn add postcss-px-to-viewport -D
復制代碼
二、轉換配置
- 根目錄下 新建 postcss.config.js
// postcss.config.js
module.exports = {
plugins: {
autoprefixer: {}, // 用來給不同的瀏覽器自動添加相應前綴,如-webkit-,-moz-等等
"postcss-px-to-viewport": {
unitToConvert: "px", // 要轉化的單位
viewportWidth: 750, // UI設計稿的寬度
unitPrecision: 6, // 轉換后的精度,即小數點位數
propList: ["*"], // 指定轉換的css屬性的單位,*代表全部css屬性的單位都進行轉換
viewportUnit: "vw", // 指定需要轉換成的視窗單位,默認vw
fontViewportUnit: "vw", // 指定字體需要轉換成的視窗單位,默認vw
selectorBlackList: ["wrap"], // 指定不轉換為視窗單位的類名,
minPixelValue: 1, // 默認值1,小于或等于1px則不進行轉換
mediaQuery: false, // 是否在媒體查詢的css代碼中也進行轉換,默認false
replace: true, // 是否轉換后直接更換屬性值
exclude: [/node_modules/], // 設定忽略檔案,用正則做目錄名匹配
landscape: false, // 是否處理橫屏情況
},
},
};
復制代碼
三、頁面效果測驗
<template>
<div>
<div>繼承body的字體</div>
<div class="test">這是測驗用的文字</div>
</div>
</template>
<script></script>
<style lang="less">
html,
body {
font-size: 12px;
}
.test {
font-size: 20px;
}
</style>
復制代碼
四、兼容vant配置
4.1 配置
- vant 設計稿螢屏寬度為375
- 移除 exclude 中的 node_modules
- 新增螢屏比適配
// postcss.config.js
const path = require("path");
module.exports = ({ file }) => {
const width = file.includes(path.join("node_modules", "vant")) ? 375 : 750;
return {
plugins: {
autoprefixer: {}, // 用來給不同的瀏覽器自動添加相應前綴,如-webkit-,-moz-等等
"postcss-px-to-viewport": {
unitToConvert: "px", // 要轉化的單位
viewportWidth: width, // UI設計稿的寬度
unitPrecision: 6, // 轉換后的精度,即小數點位數
propList: ["*"], // 指定轉換的css屬性的單位,*代表全部css屬性的單位都進行轉換
viewportUnit: "vw", // 指定需要轉換成的視窗單位,默認vw
fontViewportUnit: "vw", // 指定字體需要轉換成的視窗單位,默認vw
selectorBlackList: ["wrap"], // 指定不轉換為視窗單位的類名,
minPixelValue: 1, // 默認值1,小于或等于1px則不進行轉換
mediaQuery: false, // 是否在媒體查詢的css代碼中也進行轉換,默認false
replace: true, // 是否轉換后直接更換屬性值
exclude: [], // 設定忽略檔案,用正則做目錄名匹配
landscape: false, // 是否處理橫屏情況
},
},
};
};
復制代碼
<template>
<div>
<div>繼承body的字體</div>
<div class="test">這是測驗用的文字</div>
<van-button>vant按鈕</van-button>
</div>
</template>
<script></script>
<style lang="less">
html,
body {
font-size: 12px;
}
.test {
font-size: 20px;
}
</style>
復制代碼
4.2 效果
五、兼容行內樣式
5.1 未做兼容效果
- 默認情況下,不會對行內樣式進行轉換
5.2 使用 style-vw-loader 插件進行行內樣式轉換
5.2.1 安裝依賴
yarn add style-vw-loader
復制代碼
5.2.2 插件配置
// vue.config.js
const { defineConfig } = require("@vue/cli-service");
module.exports = defineConfig({
transpileDependencies: true,
devServer: {
port: 8181,
},
//配置loader
chainWebpack: (config) => {
config.module
.rule("vue")
.test(/\.vue$/)
.use("style-vw-loader")
.loader("style-vw-loader")
.options({
viewportWidth: 750, //傳參,根據設計稿螢屏寬度來配置
});
},
});
復制代碼
<template>
<div>
<div>繼承body的字體</div>
<div class="test">這是測驗用的文字</div>
<van-button>vant按鈕</van-button>
<div
style="
width: 200px;
height: 200px;
margin-top: 20px;
border: 1px solid skyblue;
"
>
測驗行內樣式
</div>
</div>
</template>
<script></script>
<style lang="less">
html,
body {
font-size: 12px;
}
.test {
font-size: 20px;
}
</style>
復制代碼
5.2.3 頁面效果
六、行內樣式 非插件適配方案
//js計算中進行vw適畫 375px設計圖基準
export function px2vw(px: number string) {
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth ||0)
return `${(Number(px) / 750 * vw}px`
// 示例 最后,將計算好的樣式值應用到元素中:
// const element = document.querySelector('.my-element');
// element.style.width = px2vw(100);
}
復制代碼
六、配置引數拓展
propList: 當有些屬性的單位我們不希望轉換的時候,可以添加在陣列后面,并在前面加上!號,如propList: ["*","!letter-spacing"],這表示:所有css屬性的屬性的單位都進行轉化,除了letter-spacing的selectorBlackList:轉換的黑名單,在黑名單里面的我們可以寫入字串,只要類名包含有這個字串,就不會被匹配,比如selectorBlackList: ['wrap'],它表示形如wrap,my-wrap,wrapper這樣的類名的單位,都不會被轉換
參考
- vmin/vmax/vw/wh
- 行內樣式適配-style-vw-loader
- viewport適配
作者:angelCopy
鏈接:https://juejin.cn/post/7213181814860398649
來源:稀土掘金
著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請注明出處,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/547960.html
標籤:其他
上一篇:從0搭建Vue3組件庫(七):使用 glup 打包組件庫并實作按需加載
下一篇:axios網路請求
