在使用vue-cli搭建好項目框架后,在目錄結構的index.html文件中添加一段js代碼:
fnResize(); window.onresize = function () { fnResize(); } function fnResize() { var deviceWidth = document.documentElement.clientWidth || window.innerWidth; if (deviceWidth >= 750) { deviceWidth = 750; } if (deviceWidth <= 320) { deviceWidth = 320; } document.documentElement.style.fontSize = (deviceWidth / 7.5) + 'px'; }
然后在寫css就可以將px單位換成rem.
這里設置的比例是100px=1rem,
例如:寬度為100px時,可以直接寫成1rem
=============================================================
2==vue cli3.0 rem 使用
vue cli3.0 rem 使用
首先安裝amfe-flexible插件,在main.js里引入
1、npm i amfe-flexible
2、import 'amfe-flexible'
然后再,安裝postcss-px2rem插件
npm i postcss-px2rem
在package.json中配置
"postcss": {
"plugins": {
"autoprefixer": {},
"postcss-px2rem": {
"remUnit": 37.5
}
}
}
說明,我這里用的是vue-cli3.0腳手架。在.vue文件里。樣式直接寫px單位就可以了。在瀏覽器查看時會自動轉換為rem單位。如果字體還想用px。那就這樣將px大寫。就不會編譯為rem單位了。樣式就可以實現px。