vue3如何使用postcss-px-to-viewport適配屏幕
本教程只使用于vue3+vue-cli5創(chuàng)建的項(xiàng)目,說(shuō)白了就是使用的webpack環(huán)境,不適用于vite,后面會(huì)更新。
因?yàn)榧夹g(shù)總是更新迭代,大多數(shù)博主都沒(méi)有習(xí)慣寫(xiě)上自己的配置,總是把一段自認(rèn)為可以的配置貼出來(lái),很多時(shí)候看了不僅誤人子弟,還耽誤很多開(kāi)發(fā)時(shí)間,我,致力于做一個(gè)負(fù)責(zé)人的博主,都會(huì)貼出配置和使用環(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正在運(yùn)行,安裝完成以后記得重啟devserver。
配置
在項(xiàng)目根目錄下創(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)(\/|\\)/, } } }
重新運(yùn)行,打開(kāi)瀏覽器查看屬性值已經(jīng)變?yōu)関w計(jì)算,就是這么簡(jiǎn)單。
附上插件的github地址: https://github.com/evrone/postcss-px-to-viewport
項(xiàng)目 | 類型 | 描述 |
---|---|---|
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3和vue2掛載實(shí)例到全局(自定義消息提示框組件方式)
這篇文章主要介紹了vue3和vue2掛載實(shí)例到全局(自定義消息提示框組件方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04使用vue2.6實(shí)現(xiàn)抖音【時(shí)間輪盤(pán)】屏保效果附源碼
前段時(shí)間看抖音,有人用時(shí)間輪盤(pán)作為動(dòng)態(tài)的桌面壁紙,一時(shí)間成為全網(wǎng)最火的電腦屏保,后來(lái)小米等運(yùn)用市場(chǎng)也出現(xiàn)了【時(shí)間輪盤(pán)】,有點(diǎn)像五行八卦,感覺(jué)很好玩,于是突發(fā)奇想,自己寫(xiě)一個(gè)網(wǎng)頁(yè)版小DEMO玩玩,需要的朋友可以參考下2019-04-04vue中進(jìn)入詳情頁(yè)記住滾動(dòng)位置的方法(keep-alive)
今天小編就為大家分享一篇vue中進(jìn)入詳情頁(yè)記住滾動(dòng)位置的方法(keep-alive),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn)
我們最開(kāi)始拿到的element-ui是帶有下拉箭頭的,那么如何去除element-ui下拉框的下拉箭頭的實(shí)現(xiàn),本文就詳細(xì)的介紹一下,感興趣的可以了解一下2023-08-08vue?el-table中使用el-select選中后無(wú)效的解決
這篇文章主要介紹了vue?el-table中使用el-select選中后無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue3中關(guān)于路由hash與History的設(shè)置
這篇文章主要介紹了vue3中關(guān)于路由hash與History的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08