使用amfe-flexible和postcss-px2rem解决VUE移动/PC端适配

amfe-flexible

该插件将网页平均分成10等份,然后设置html根标签的font-size

html,body{
    color:#333;
    font-size: 14px!important;
    font-family: 'oppo';
}
postcss-px2rem

该插件将我们经常使用的px转换成rem
转换方法是rem = px /(我们设置的值)
例如我们设置的单位是37.5,设置div的宽度为75px,则换算后为2rem

1、安装

	npm i amfe-flexible postcss-px2rem

2、main.js

import rem from 'amfe-flexible'

Vue.use(rem)
function px2rem(px){
  if(/%/ig.test(px)){
    return px;
  }else{
    return (parseFloat(px) / 192) + 'rem';
  }
}
Vue.prototype.$px2rem = px2rem;

3、在package.json里添加

"postcss": {
    "plugins": {
      "autoprefixer": {},
      "postcss-px2rem": {
        "remUnit": 192
      }
    }
  }

4、页面中使用

//行内样式写法
<img
    src="../../assets/img/notice_icon.png"
    alt=""
    :style="{width:$px2rem('50px')}"
/>
<template>
  <div class="about">
    <div class="box"></div>
  </div>
</template>
<script>
  export default {
    mounted() {
    }
  }
</script>
<style lang="scss">
//正常写法
.box {
  width: 375px;
  height: 375px;
  background: rgba(0, 0, 0, .5);
}
</style>

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值