• vue和react项目中实现 px 转 vm


    vue和react项目中实现 px 转 vm

    vue中配置

    1、下载插件

    npm i postcss-px-to-viewport
    
    • 1

    2、vue项目配置

    const { defineConfig } = require('@vue/cli-service');
    const AutoImport = require('unplugin-auto-import/webpack');
    const Components = require('unplugin-vue-components/webpack');
    const { ElementPlusResolver } = require('unplugin-vue-components/resolvers');
    const { LayuiVueResolver } = require('unplugin-vue-components/resolvers');
    const WebpackBar = require('webpackbar');
    const CompressionPlugin = require('compression-webpack-plugin');
    
    module.exports = defineConfig({
        //...其它相关配置
    	transpileDependencies: true,
    	css: {
    		loaderOptions: {
    			postcss: {
    				postcssOptions: {
    					plugins: [
    						[
    							'postcss-px-to-viewport',
    							{
    								unitToConvert: 'px', // 需要转换的单位,默认为"px"
    								viewportWidth: 1920, // 设计稿的视窗宽度
    								unitPrecision: 5, // 单位转换后保留的精度
    								//propList: ['*', '!font*'] 不匹配 font 开头的属性
    								propList: ['*'], // 能转化为 vw 的属性列表
    								viewportUnit: 'vw', // 希望使用的视窗单位
    								fontViewportUnit: 'vw', // 字体使用的视窗单位
    								selectorBlackList: [], // 需要忽略的 CSS 选择器,不会转为视窗单位,使用原有的 px 等单位
    								minPixelValue: 1, // 设置最小的转换数值,如果为 1 的话,只有大于 1 的值会被转换
    								mediaQuery: true, // 媒体查询里的单位是否需要转换单位
    								replace: true, // 是否直接更换属性值,而不添加备用属性
    								exclude: undefined, // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
    								include: /\/src\//, // 如果设置了include,那将只有匹配到的文件才会被转换
    								landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件
    								landscapeUnit: 'vh', // 横屏时使用的单位
    								landscapeWidth: 1080, // 横屏时使用的视窗宽度
    							},
    						],
    					],
    				},
    			},
    		},
    	},
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    vite中配置

    1、下载插件

    npm i postcss-px-to-viewport
    
    • 1

    2、vite 项目配置

    import { fileURLToPath, URL } from 'node:url'
    import { defineConfig } from 'vite'
    import vue from '@vitejs/plugin-vue'
    import postcsspxtoviewport from 'postcss-px-to-viewport'
    
    export default defineConfig({
      plugins: [vue()],
      resolve: {
        alias: {
          '@': fileURLToPath(new URL('./src', import.meta.url))
        }
      },
      css: {
        postcss: {
          plugins: [
            postcsspxtoviewport({
              unitToConvert: 'px', // 需要转换的单位,默认为"px"
              viewportWidth: 375, // 设计稿的视窗宽度
              unitPrecision: 5, // 单位转换后保留的精度
              //propList: ['*', '!font*'] 不匹配 font 开头的属性
              propList: ['*'], // 能转化为 vw 的属性列表
              viewportUnit: 'vw', // 希望使用的视窗单位
              fontViewportUnit: 'vw', // 字体使用的视窗单位
              selectorBlackList: [], // 需要忽略的 CSS 选择器,不会转为视窗单位,使用原有的 px 等单位
              minPixelValue: 1, // 设置最小的转换数值,如果为 1 的话,只有大于 1 的值会被转换
              mediaQuery: false, // 媒体查询里的单位是否需要转换单位
              replace: true, // 是否直接更换属性值,而不添加备用属性
              exclude: undefined, // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
              include: /\/src\//, // 如果设置了include,那将只有匹配到的文件才会被转换
              landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件
              landscapeUnit: 'vw', // 横屏时使用的单位
              landscapeWidth: 1125 // 横屏时使用的视窗宽度
            })
          ]
        }
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    react中配置

    1、下载插件

    但是安装下面的插件有可能会出现不兼容的情况

    npm i postcss-px-to-viewport @craco/craco -D
    
    • 1

    如果不能安装下面这个

    npm install postcss-px-to-viewport-8-plugin -D
    
    • 1

    2、package.json中添加

    "scripts": {
      "start": "craco start",
      "build": "craco build"
      "test": "craco test"
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、在中添加

    const path = require("path");
    
    module.exports = {
      //...其它配置
      webpack: {
        alias: {
          "@": path.join(__dirname, "src"),
        },
      },
      style: {
        postcss: {
          mode: "exclude",
          loaderOptions: {
            postcssOptions: {
              ident: "postcss",
              plugins: [
                [
                  "postcss-px-to-viewport-8-plugin",
                  {
                    unitToConvert: "px", // 要转化的单位
                    viewportWidth: 375, // UI设计稿的宽度
                    viewportUnit: "vw", // 指定需要转换成的视窗单位,建议使用 rem
                    fontViewportUnit: "vw", // 字体使用的视口单位
                    unitPrecision: 13, // 指定`px`转换为视窗单位值的小数后 x位数
                    // propList: 当有些属性的单位我们不希望转换的时候,可以添加在数组后面,并在前面加上!号,如propList: ["*","!letter-spacing"],这表示:所有css属性的属性的单位都进行转化,除了letter-spacing的
                    propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
                    // 转换的黑名单,在黑名单里面的我们可以写入字符串,只要类名包含有这个字符串,就不会被匹配。比如selectorBlackList: ['wrap'],它表示形如wrap,my-wrap,wrapper这样的类名的单位,都不会被转换
                    selectorBlackList: ["ignore"], // 指定不转换为视窗单位的类名,
                    minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
                    mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
                    replace: true, // 是否转换后直接更换属性值
                    exclude: [/node_modules/], // 设置忽略文件,用正则做目录名匹配
                    landscape: false, // 是否处理横屏情况
                  },
                ],
              ],
            },
          },
        },
      },
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    Pytorch学习之路 - CNN
    Linux命令从入门到实战 ---- 磁盘管理类
    python内置函数+lambda函数
    [计算机网络]网络基础
    【背包问题】第十二届蓝桥杯省赛第一场C++ A组/B组/研究生组《砝码称重》(c++)
    网络安全(黑客)——2024自学
    赛宁党支部赴延安开展革命旧址学习主题党日活动
    vue-admin-better前端页面-菜单-权限配置
    linux上系统磁盘满了的问题
    1.1.4:DHTMLX Rich Text|JavaScript/HTML Rich Text Editor
  • 原文地址:https://blog.csdn.net/weixin_46533577/article/details/134423293