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-scroller记录滚动位置的示例代码

    vue-scroller记录滚动位置的示例代码

    本篇文章主要介绍了vue-scroller记录滚动位置的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Vue中this.$nextTick()方法的使用及代码示例

    Vue中this.$nextTick()方法的使用及代码示例

    $nextTick()是Vue.js框架中的一个方法,它主要用于DOM操作,当我们修改Vue组件中的数据时,Vue.js会在下次事件循环前自动更新视图,并异步执行$nextTick()中的回调函数,本文主要介绍了Vue中this.$nextTick()方法的使用及代码示例,需要的朋友可以参考下
    2023-05-05
  • rollup打包vue组件并发布到npm的方法

    rollup打包vue组件并发布到npm的方法

    这篇文章主要介绍了rollup打包vue组件并发布到npm,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • web前端Vue报错:Uncaught (in promise) TypeError:Cannot read properties of nu解决

    web前端Vue报错:Uncaught (in promise) TypeError:Cannot read 

    这篇文章主要给大家介绍了关于web前端Vue报错:Uncaught (in promise) TypeError:Cannot read properties of nu的解决方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • Vue3中watchEffect的用途浅析

    Vue3中watchEffect的用途浅析

    这篇文章主要给大家介绍了关于Vue3中watchEffect用途的相关资料, watch和watchEffect都是监听器,但在写法和使用上有所区别,本文进行了详细的介绍,需要的朋友可以参考下
    2021-07-07
  • vue3使用elementPlus进行table合并处理的示例详解

    vue3使用elementPlus进行table合并处理的示例详解

    虚拟数据中公司下有多个客户,公司一样的客户,公司列需要合并,客户如果一样也需要合并进行展示,所以本文给大家介绍了vue3使用elementPlus进行table合并处理的实例,文中通过代码示例介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • Vue Router修改query参数url参数没有变化问题及解决

    Vue Router修改query参数url参数没有变化问题及解决

    这篇文章主要介绍了Vue Router修改query参数url参数没有变化问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Vue中Mustache插值语法与v-bind指令详解

    Vue中Mustache插值语法与v-bind指令详解

    在Vue中通过Mustache语法(双大括号)将data中的文本数据插入到HTML中,下面这篇文章主要给大家介绍了关于Vue中Mustache插值语法与v-bind指令的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • 极速上手 VUE 3 teleport传送门组件及使用语法

    极速上手 VUE 3 teleport传送门组件及使用语法

    teleport 传送门组件,提供一种简洁的方式,可以指定它里面的内容的父元素,也就是说teleport 中的内容允许我们控制在任意的DOM中,使用简单,对VUE 3 teleport传送门相关知识感兴趣的朋友一起看看吧
    2021-10-10
  • Vue项目利用axios请求接口下载excel

    Vue项目利用axios请求接口下载excel

    这篇文章主要为大家详细介绍了Vue项目利用axios请求接口下载excel,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11

最新评论