vue中使用postcss-px2rem的兩種方法
vue項目中使用postcss-px2rem的2種方法
在項目中為了屏幕適配,經(jīng)常會用到rem,postcss-px2rem就是為了讓我們直接在將代碼中px自動轉(zhuǎn)化成對應(yīng)的rem的一個插件.(下邊的方法適用于使用cli2腳手架搭建的項目,現(xiàn)在好多數(shù)項目使用cli3搭建,我在后邊的文章中增加了vue使用rem實現(xiàn) 移動端屏幕適配).)
如何使用:
1.安裝
npm i postcss-px2rem --save -dev
2.設(shè)置
1).找到項目根目錄下的.postcssrc文件
module.exports = { "plugins": { "postcss-import": {}, "postcss-url": {}, // to edit target browsers: use "browserslist" field in package.json "autoprefixer": { "browsers": ['last 10 Chrome versions', 'last 5 Firefox versions', 'Safari >= 8'] }, 'postcss-px2rem':{'remUnit':75} //配置rem基準(zhǔn)值,75是iphone6標(biāo)準(zhǔn) } }
remUnit: 75 代表 1rem = 75px; 所以當(dāng)你一個75px值時,它會自動轉(zhuǎn)成 (75px/75)rem,
轉(zhuǎn)化完之后,你還需要在根元素設(shè)置他的font-size,因為rem是相對根元素來設(shè)置大小的
html { font-size: 10vw; }
這樣的話我們設(shè)置的px值 就變成對應(yīng)的 10%的屏幕寬度 *(75px/75)rem
2) 找到根目錄下的vue-loader.conf.js
本人使用的是這種方法.
首先需要設(shè)置html的fontsize值,1rem = html的font-size,這里咱們動態(tài)設(shè)置一下,可以直接在index.html中設(shè)置
PC端
(function () { function setRootFontSize() { let rem, rootWidth; let rootHtml = document.documentElement; //限制展現(xiàn)頁面的最小寬度 rootWidth = rootHtml.clientWidth < 1366 ? 1366 : rootHtml.clientWidth; // 19.2 = 設(shè)計圖尺寸寬 / 100( 設(shè)計圖的rem = 100 ) rem = rootWidth / 19.2; // 動態(tài)寫入樣式 rootHtml.style.fontSize = `${rem}px`; } setRootFontSize(); window.addEventListener("resize", setRootFontSize, false); })();
移動端
(function () { function setRootFontSize() { let dpr, rem, scale, rootWidth; let rootHtml = document.documentElement; dpr = window.devicePixelRatio || 1; //移動端必須設(shè)置 //限制展現(xiàn)頁面的最小寬度 rootWidth = rootHtml.clientWidth < 1366 ? 1366 : rootHtml.clientWidth; rem = rootWidth * dpr / 19.2; // 19.2 = 設(shè)計圖尺寸寬1920 / 100(設(shè)計圖的rem = 100) scale = 1 / dpr; // 設(shè)置viewport,進行縮放,達(dá)到高清效果 (移動端添加) let vp = document.querySelector('meta[name="viewport"]'); vp.setAttribute('content', 'width=' + dpr * rootHtml.clientWidth + ',initial-scale=' + scale + ',maximum-scale=' + scale + ', minimum-scale=' + scale + ',user-scalable=no'); // 動態(tài)寫入樣式 rootHtml.style.fontSize = `${rem}px`; } setRootFontSize(); window.addEventListener("resize", setRootFontSize, false); window.addEventListener("orientationchange", setRootFontSize, false); //移動端 })();
'use strict' const utils = require('./utils') const config = require('../config') const isProduction = process.env.NODE_ENV === 'production' const sourceMapEnabled = isProduction ? config.build.productionSourceMap : config.dev.cssSourceMap const px2rem = require('postcss-px2rem') module.exports = { loaders: utils.cssLoaders({ sourceMap: sourceMapEnabled, extract: isProduction }), cssSourceMap: sourceMapEnabled, cacheBusting: config.dev.cacheBusting, transformToRequire: { video: ['src', 'poster'], source: 'src', img: 'src', image: 'xlink:href' }, postcss: function() { return [px2rem({remUnit: 100})]; } }
修改完成后 記得重新編譯
我還寫了一篇 vue使用rem實現(xiàn) 移動端屏幕適配的文章,有需要的可以去看看,希望對你有幫助,多多評論指教.
到此這篇關(guān)于vue項目中使用postcss-px2rem的方法總結(jié)的文章就介紹到這了,更多相關(guān)vue使用postcss-px2rem內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2結(jié)合echarts實現(xiàn)一個地圖的效果
這篇文章主要介紹了vue2結(jié)合echarts實現(xiàn)一個地圖的效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家何用vue和echarts實現(xiàn)一個地圖有一定的幫助,感興趣的朋友一起看看吧2024-03-03vue?內(nèi)置組件?component?的用法示例詳解
這篇文章主要介紹了vue內(nèi)置組件component的用法,本文給大家介紹了component內(nèi)置組件切換方法,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08vue3.0 CLI - 2.3 - 組件 home.vue 中學(xué)習(xí)指令和綁定
這篇文章主要介紹了vue3.0 CLI - 2.3 - 組件 home.vue 中學(xué)習(xí)指令和綁定的相關(guān)知識,本文通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì) ,需要的朋友可以參考下2018-09-09vue指令之表單控件綁定v-model v-model與v-bind結(jié)合使用
這篇文章主要介紹了vue指令之表單控件綁定v-model v-model與v-bind結(jié)合使用,需要的朋友可以參考下2019-04-04