vue?px轉(zhuǎn)rem配置詳解
方法一
一、配置與安裝步驟:
1、在 Vue 項目的 src 文件夾下創(chuàng)建一個 config 文件夾:
2、在 config 文件夾中創(chuàng)建 rem.js:
3、將以下代碼復制到 rem.js 中:
// 基準大小 const baseSize = 32 // 設置 rem 函數(shù) function setRem () { // 當前頁面寬度相對于 750 寬的縮放比例,可根據(jù)自己需要修改。 const scale = document.documentElement.clientWidth / 750 // 設置頁面根節(jié)點字體大小 document.documentElement.style.fontSize = (baseSize * Math.min(scale, 2)) + 'px' } // 初始化 setRem() // 改變窗口大小時重新設置 rem window.onresize = function () { setRem() }
4、在 src 文件夾下的 main.js 中引入:
import './config/rem'
5、在 Vue 項目根目錄終端引入:
npm install postcss-pxtorem -D
6、在 Vue 項目文件夾下的 postcss.config.js 中加入:
module.exports = { plugins: { autoprefixer: {}, "postcss-pxtorem": { "rootValue": 16, "propList": ["*"] } } }
方法二
第一步 安裝 lib-flexible
npm i lib-flexible --save
第二步 安裝 px2rem-loader
npm install px2rem-loader --save-dev
第三步 引入lib-flexible
import 'lib-flexible/flexible'
第四步 最重要的一步 配置utils文件
const px2remLoader = { loader: 'px2rem-loader', options: { remUnit: 37.5 } }<br>//在generateLoaders方法中添加px2remLoader 1 const loaders = [cssLoader,px2remLoader]
或者第四步:Create new “vue.config.js” file if without “vue.config.js
” (目錄: hello-world/vue.config.js
)
module.exports = { chainWebpack: (config) => { config.module .rule('css') .test(/\.css$/) .oneOf('vue') .resourceQuery(/\?vue/) .use('px2rem') .loader('px2rem-loader') .options({ remUnit: 75 // 75表示750的設計稿,37.5表示375的設計稿 }) } }
1.按照px來編寫都會轉(zhuǎn)化成rem的形式,但是有些地方我們不想轉(zhuǎn)換,可以用下面兩種方法。
在px后面添加/no/,不會轉(zhuǎn)化px,會原樣輸出。 — 一般border需用這個
在px后面添加/px/,會根據(jù)dpr的不同,生成三套代碼。---- 一般字體需用這個
2 使用過程中,發(fā)現(xiàn)某些import外聯(lián)樣式不會被轉(zhuǎn)化,注意避開這些坑。
<style src='../assets/style.css'> /* px2rem能正常轉(zhuǎn)換 */ </style> <style> /* px2rem不能正常轉(zhuǎn)換 */ @import '../assets/style.css'; </style> <style> /* px2rem不能正常轉(zhuǎn)換 */ @import url('../assets/style.css'); </style>
方法三
第一步 安裝 amfe-flexible
npm i amfe-flexible -S
第二步 安裝 postcss-pxtorem
npm install postcss-pxtorem --save-dev
第三步 引入amfe-flexible
import 'amfe-flexible'
第四步根目錄下創(chuàng)建postcss.config.js文件
module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 37.5, propList: ['*'] } } }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
vue項目預覽excel表格功能(file-viewer插件)
這篇文章主要介紹了vue項目預覽excel表格功能(file-viewer插件),本文分步驟結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10詳解vue中父子組件傳遞參數(shù)props的實現(xiàn)方式
這篇文章主要給大家介紹了在vue中,父子組件傳遞參數(shù)?props?實現(xiàn)方式,文章通過代碼示例介紹的非常詳細,對我們的學習或工作有一定的參考價值,需要的朋友可以參考下2023-07-07vue?router進行路由跳轉(zhuǎn)并攜帶參數(shù)的實例詳解(params/query)
在使用`router.push`進行路由跳轉(zhuǎn)到另一個組件時,可以通過`params`或`query`來傳遞參數(shù),這篇文章主要介紹了vue?router進行路由跳轉(zhuǎn)并攜帶參數(shù)(params/query),需要的朋友可以參考下2023-09-09