vue3如何使用postcss-px-to-viewport適配屏幕
本教程只使用于vue3+vue-cli5創(chuàng)建的項目,說白了就是使用的webpack環(huán)境,不適用于vite,后面會更新。
因為技術總是更新迭代,大多數(shù)博主都沒有習慣寫上自己的配置,總是把一段自認為可以的配置貼出來,很多時候看了不僅誤人子弟,還耽誤很多開發(fā)時間,我,致力于做一個負責人的博主,都會貼出配置和使用環(huán)境,方便大家做參考。
使用環(huán)境
| 名稱 | 版本 |
|---|---|
| vue | 3.2.13 |
| vue-cli | 5.x |
| webpack | 5.x |
| nodejs | 16.15 |
| postcss-px-to-viewport | ^1.1.1 |
安裝
npm install postcss-px-to-viewport --save-dev yarn add postcss-px-to-viewport --save-dev pnpm add postcss-px-to-viewport --save-dev
如果devserver正在運行,安裝完成以后記得重啟devserver。
配置
在項目根目錄下創(chuàng)建 postcss.config.js 文件,并填入一下內(nèi)容:
module.exports = {
plugins: {
// ...
'postcss-px-to-viewport': {
// options
unitToConvert: "px",
viewportWidth: 1920,
viewportHeight:1080,
unitPrecision: 3,
propList: [
"*"
],
viewportUnit: "vw",
fontViewportUnit: "vw",
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
replace: true,
exclude: /(\/|\\)(node_modules)(\/|\\)/,
}
}
}
重新運行,打開瀏覽器查看屬性值已經(jīng)變?yōu)関w計算,就是這么簡單。

附上插件的github地址: https://github.com/evrone/postcss-px-to-viewport
| 項目 | 類型 | 描述 |
|---|---|---|
| unitToConvert | (String) | unit to convert, by default, it is px. |
| viewportWidth | (Number) | The width of the viewport. |
| unitPrecision | (Number) | The decimal numbers to allow the vw units to grow to. |
| propList | (Array) | - The properties that can change from px to vw. |
| -Values need to be exact matches. | ||
| Use * at the start or end of a word. (['position'] will match background-position-y) | ||
| Use ! to not match a property. Example: ['*', '!letter-spacing'] | ||
| Combine the "not" prefix with the other prefixes. Example: ['', '!font'] | ||
| viewportUnit | (String) | Expected units. |
| fontViewportUnit | (String) | Expected units for font. |
| selectorBlackList | (Array)The selectors to ignore and leave as px. | If value is string, it checks to see if selector contains the string. ['body'] will match .body-class |
| If value is regexp, it checks to see if the selector matches the regexp. [/^body$/] will match body but not .body | ||
| minPixelValue | (Number) | Set the minimum pixel value to replace. |
| mediaQuery | (Boolean) | replaces rules containing vw instead of adding fallbacks |
| exclude | (Regexp or Array of Regexp Ignore some files like 'node_modules') | If value is regexp, will ignore the matches files. |
| If value is array, the elements of the array are regexp. | ||
| include | (Regexp or Array of Regexp) If include is set, only matching files will be converted, for example, only files under src/mobile/ (include: /\/src\/mobile\//) | If value is array, the elements of the array are regexp. |
| If value is regexp, will ignore the matches files. | ||
| landscape | (Boolean) | Adds @media (orientation: landscape) with values converted via landscapeWidth. |
| landscapeUnit | (String) | Expected unit for landscape option |
| landscapeWidth | (Number) | Viewport width for landscape orientation. |
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue3和vue2掛載實例到全局(自定義消息提示框組件方式)
這篇文章主要介紹了vue3和vue2掛載實例到全局(自定義消息提示框組件方式),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
使用vue2.6實現(xiàn)抖音【時間輪盤】屏保效果附源碼
前段時間看抖音,有人用時間輪盤作為動態(tài)的桌面壁紙,一時間成為全網(wǎng)最火的電腦屏保,后來小米等運用市場也出現(xiàn)了【時間輪盤】,有點像五行八卦,感覺很好玩,于是突發(fā)奇想,自己寫一個網(wǎng)頁版小DEMO玩玩,需要的朋友可以參考下2019-04-04
vue中進入詳情頁記住滾動位置的方法(keep-alive)
今天小編就為大家分享一篇vue中進入詳情頁記住滾動位置的方法(keep-alive),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue?el-table中使用el-select選中后無效的解決
這篇文章主要介紹了vue?el-table中使用el-select選中后無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

