vue项目实现部分页面使用rem_vue 中使用rem布局

要想移动端适配 并使用 rem  您需要先看这篇文章,配置好less ➡️ 在vue 中使用 less,就可以使用rem了

如果项目已经开发的差不多了,没有用到rem 又要使用rem,您用这招。

postcss-pxtorem:转换px为rem的插件

安装 postcss-pxtoremnpm install postcss-pxtorem --save

新建rem.js文件const baseSize = 32

// 设置 rem 函数

functionsetRem () {

// 当前页面宽度相对于 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

test

export default{

name: 'HelloWorld',

data() {

return{

msg: 'Welcome to Your Vue.js App'

}

}

}

.hello {

text-align: center;

font-size: 20px;

width: 300px;

height: 400px;

background:red;

}

效果

此处已vue为例,在使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值