Vue CLI (三):使用vw适配移动端

使用postcss插件转换pxvw

安装

npm i -S postcss-px-to-viewport
# or
yarn add postcss-px-to-viewport
复制代码

配置

postcss.config.js中配置

module.exports = {
  plugins: {
    "postcss-px-to-viewport": {
      // 基准 - 视窗宽度
      viewportWidth: 750,
      // 转换后单位所带的小数点位数
      unitPrecision: 5,
      // 转换后单位
      viewportUnit: "vw",
      // 转换后字体的单位
      fontViewportUnit: "vmin", // ???暂时无效
      // 忽略的样式名
      selectorBlackList: [".ignore", ".hairlines"],
      // 最小px
      minPixelValue: 1,
      // 是否转换媒体查询的px单位
      mediaQuery: false
    }
  }
}
复制代码

解决宽高比

安装

npm i -S postcss-aspect-ratio-mini
# or
yarn add postcss-aspect-ratio-mini
复制代码

加入通用样式

[aspectratio] {
  position: relative;
}
[aspectratio]::before {
  content: '';
  display: block;
  width: 1px;
  margin-left: -1px;
  height: 0;
}
[aspectratio-content] {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
}
复制代码

使用

<div aspectratio w-188-246>
  <div aspectratio-content>content</div>
</div>
复制代码
[w-188-246] {
  aspect-ratio: '188:246';
}
复制代码

解决1px

安装

npm i -S postcss-write-svg
# or
yarn add postcss-write-svg
复制代码

加入通用样式

@svg 1px-border {
  height: 2px;
  @rect {
    fill: var(--color, black);
    width: 100%;
    height: 50%;
  }
}
复制代码

使用

.example {
  border: 1px solid red;
  border-image: svg(1px-border param(--color #00b1ff)) 2 2 stretch;
}
复制代码

转载于:https://juejin.im/post/5c10e8386fb9a049e82b60e5

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值