vue 移动端屏幕适配 使用rem
2018-09-25 23:04
836 查看
要想移动端适配 并使用 rem 您需要先看这篇文章,配置好less ➡️ 在vue 中使用 less,就可以使用rem了
如果项目已经开发的差不多了,没有用到rem 又要使用rem,您用这招。
postcss-pxtorem:转换px为rem的插件
安装 postcss-pxtorem
npm install postcss-pxtorem --save
新建rem.js文件
const baseSize = 32 // 设置 rem 函数 function setRem () { // 当前页面宽度相对于 750 宽的缩放比例,可根据自己需要修改。 const scale = document.documentElement.clientWidth / 750 // 设置页面根节点字体大小 document.documentElement.style.fontSize = (baseSize * Math.min(scale, 2)) + 'px' } // 初始化 setRem() // 改变窗口大小时重新设置 rem window.onresize = function () { setRem() }
并引用进main.js文件内
import './rem'
修改.postcssrc.js
文件
在
.postcssrc.js文件内的plugins 添加以下配置,配后就可以在开发中直接使用
px单位开发了
"postcss-pxtorem": { "rootValue": 32, "propList": ["*"] }
helloworld.vue
<template> <div class="hello"> test </div> </template> <script> export default { name: 'HelloWorld', data() { return { msg: 'Welcome to Your Vue.js App' } } } </script> <style scoped> .hello { text-align: center; font-size: 20px; width: 300px; height: 400px; background:red; } </style>
效果
此随笔乃本人学习工作记录,如有疑问欢迎在下面评论,转载请标明出处。
如果对您有帮助请动动鼠标右下方给我来个赞,您的支持是我最大的动力
相关文章推荐
- vue使用rem实现 移动端屏幕适配
- 一步步教你使用rem适配不同屏幕的移动设备
- 使用rem实现移动端适配的简单方法
- Vue:将px转化为rem,适配移动端
- 移动端页面适配rem和vw的使用和转换
- rem与media标签实现移动端屏幕适配
- 移动端使用rem适配及相关问题
- 移动端屏幕适配rem
- 移动端页面使用rem来做适配
- 自使用移动端rem适配--并且vscode-px自动转化成rem
- 移动端web开发适配方法--使用rem
- Vue-cli 将px转化为rem,适配移动端
- 详解vue2.0 不同屏幕适配及px与rem转换问题
- 使用 rem 实现 适配各种屏幕布局
- vue项目中使用lib-flexible解决移动端适配的问题解决
- 使用rem实现全屏幕自动适配(等比例缩放布局样式)
- Vue:将px转化为rem,适配移动端
- 移动端不同分辨率适配--使用flexible
- Android dp方式的屏幕适配工具使用(bat批处理方式)
- 移动端屏幕适配方案