vue3如何使用postcss-px-to-viewport适配屏幕

 更新时间:2023年03月29日 09:39:06   作者:王天平·Jason Wong  
这篇文章主要介绍了vue3如何使用postcss-px-to-viewport适配屏幕问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

本教程只使用于vue3+vue-cli5创建的项目,说白了就是使用的webpack环境,不适用于vite,后面会更新。

因为技术总是更新迭代,大多数博主都没有习惯写上自己的配置,总是把一段自认为可以的配置贴出来,很多时候看了不仅误人子弟,还耽误很多开发时间,我,致力于做一个负责人的博主,都会贴出配置和使用环境,方便大家做参考。

使用环境

名称版本
vue3.2.13
vue-cli5.x
webpack5.x
nodejs16.15
postcss-px-to-viewport^1.1.1

安装

npm install postcss-px-to-viewport --save-dev
yarn add postcss-px-to-viewport --save-dev
pnpm add postcss-px-to-viewport --save-dev

如果devserver正在运行,安装完成以后记得重启devserver。

配置

在项目根目录下创建 postcss.config.js 文件,并填入一下内容:

module.exports = {
  plugins: {
    // ...
    'postcss-px-to-viewport': {
      // options
      unitToConvert: "px",
      viewportWidth: 1920,
      viewportHeight:1080,
      unitPrecision: 3,
      propList: [
        "*"
      ],
      viewportUnit: "vw",
      fontViewportUnit: "vw",
      selectorBlackList: [],
      minPixelValue: 1,
      mediaQuery: false,
      replace: true,
      exclude: /(\/|\\)(node_modules)(\/|\\)/,
    }
  }
}

重新运行,打开浏览器查看属性值已经变为vw计算,就是这么简单。

附上插件的github地址: https://github.com/evrone/postcss-px-to-viewport

项目类型描述
unitToConvert(String)unit to convert, by default, it is px.
viewportWidth(Number)The width of the viewport.
unitPrecision(Number)The decimal numbers to allow the vw units to grow to.
propList(Array)- The properties that can change from px to vw.
-Values need to be exact matches.
Use * at the start or end of a word. (['position'] will match background-position-y)
Use ! to not match a property. Example: ['*', '!letter-spacing']
Combine the "not" prefix with the other prefixes. Example: ['', '!font']
viewportUnit(String)Expected units.
fontViewportUnit(String)Expected units for font.
selectorBlackList(Array)The selectors to ignore and leave as px.If value is string, it checks to see if selector contains the string. ['body'] will match .body-class
If value is regexp, it checks to see if the selector matches the regexp. [/^body$/] will match body but not .body
minPixelValue(Number)Set the minimum pixel value to replace.
mediaQuery(Boolean)replaces rules containing vw instead of adding fallbacks
exclude(Regexp or Array of Regexp Ignore some files like 'node_modules')If value is regexp, will ignore the matches files.
If value is array, the elements of the array are regexp.
include(Regexp or Array of Regexp) If include is set, only matching files will be converted, for example, only files under src/mobile/ (include: /\/src\/mobile\//)If value is array, the elements of the array are regexp.
If value is regexp, will ignore the matches files.
landscape(Boolean)Adds @media (orientation: landscape) with values converted via landscapeWidth.
landscapeUnit(String)Expected unit for landscape option
landscapeWidth(Number)Viewport width for landscape orientation.

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解Vue CLI3 多页应用实践和源码设计

    详解Vue CLI3 多页应用实践和源码设计

    这篇文章主要介绍了详解Vue CLI3 多页应用实践和源码设计,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 浅析前端路由简介以及vue-router实现原理

    浅析前端路由简介以及vue-router实现原理

    路由就是用来跟后端服务器进行交互的一种方式,通过不同的路径,来请求不同的资源,请求不同的页面是路由的其中一种功能。这篇文章主要介绍了前端路由简介以及vue-router实现原理,需要的朋友可以参考下
    2018-06-06
  • 图文讲解用vue-cli脚手架创建vue项目步骤

    图文讲解用vue-cli脚手架创建vue项目步骤

    本次小编给大家带来的是关于用vue-cli脚手架创建vue项目步骤讲解内容,有需要的朋友们可以学习下。
    2019-02-02
  • 对vue事件的延迟执行实例讲解

    对vue事件的延迟执行实例讲解

    今天小编就为大家分享一篇对vue事件的延迟执行实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 详解Vue中CSS样式穿透问题

    详解Vue中CSS样式穿透问题

    这篇文章主要介绍了VUE中CSS样式穿透问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • Vue在H5 项目中使用融云进行实时个人单聊通讯

    Vue在H5 项目中使用融云进行实时个人单聊通讯

    这篇文章主要介绍了Vue在H5 项目中使用融云进行实时个人单聊通讯,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 基于vue实现页面滚动加载的示例详解

    基于vue实现页面滚动加载的示例详解

    页面内容太多会导致加载速度过慢,这时可考虑使用滚动加载即还没有出现在可视范围内的内容块先不加载,出现后再加载,所以本文给大家介绍了基于vue实现页面滚动加载的示例,需要的朋友可以参考下
    2024-01-01
  • vue3+TypeScript+vue-router的使用方法

    vue3+TypeScript+vue-router的使用方法

    本文详细讲解了vue3+TypeScript+vue-router的使用方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • vue获取data数据改变前后的值方法

    vue获取data数据改变前后的值方法

    今天小编就为大家分享一篇vue获取data数据改变前后的值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • vue中vant组件样式失效问题及解决

    vue中vant组件样式失效问题及解决

    这篇文章主要介绍了vue中vant组件样式失效问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04

最新评论