vue项目中使用了vw适配方案,引入第三方ui框架mint-ui时,适配问题解决
问题分析:
一般第三方ui框架用的都是不同的适配方式,如果我们使用了vw适配,那么在使用mint-ui框架时,就会发现px单位会被转换成vw,从而导致样式变小的问题,如图

解决方案
网上看到了很多种解决方案,这里推荐第四种
1、重写第三方组件ui样式大小
2、在postcss.config.js中的selectorBlackList选项中增加不需要vw转换的类名
selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px.
3、使用rem适配方案,将原本750的宽度设置为一半,配置成37.5
https://www.jianshu.com/p/8f9aab666c4a
4、添加exclude选项,将node_modules目录排除掉,即不会受影响
在node_mudule中找到postcss-px-to-viewport,修改index.js新增对exclude选项的处理
module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
var opts = objectAssign({}, defaults, options);
var pxReplace = createPxReplace(opts.viewportWidth, opts.minPixelValue, opts.unitPrecision, opts.viewportUnit);
return function (css) {
css.walkDecls(function (decl, i) {
if (options.exclude) { // 添加对exclude选项的处理
if (Object.prototype.toString.call(options.exclude) !== '[object RegExp]') {
throw new Error('options.exclude should be RegExp!')
}
if (decl.source.input.file.match(options.exclude) !== null) return;
}
// This should be the fastest test and will remove most declarations
if (decl.value.indexOf('px') === -1) return;
if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return;
decl.value = decl.value.replace(pxRegex, pxReplace);
});
if (opts.mediaQuery) {
css.walkAtRules('media', function (rule) {
if (rule.params.indexOf('px') === -1) return;
rule.params = rule.params.replace(pxRegex, pxReplace);
});
}
};
});
然后在.postcssrc.js添加postcss-px-to-viewport的exclude选项
"postcss-px-to-viewport": {
viewportWidth: 750,
viewportHeight: 1334,
unitPrecision: 3,
viewportUnit: 'vw',
selectorBlackList: ['.ignore', '.hairlines'],
minPixelValue: 1,
mediaQuery: false,
exclude: /(\/|\\)(node_modules)(\/|\\)/
},
这里需要注意了,在没有修改postcss-px-to-viewport的index.js文件,直接在.postcssrc.js中添加了以下代码也成功了
exclude: /(\/|\\)(node_modules)(\/|\\)/
然后我去node_mudele下找到postcss-px-to-viewport的index.js打开发现了如下代码,看来是postcss-px-to-viewpor这个插件增加了对这个问题的处理, 我使用的版本是"postcss-px-to-viewport": "^1.1.0",
if (opts.exclude && file) {
if (Object.prototype.toString.call(opts.exclude) === '[object RegExp]') {
if (isExclude(opts.exclude, file)) return;
} else if (Object.prototype.toString.call(opts.exclude) === '[object Array]') {
for (let i = 0; i < opts.exclude.length; i++) {
if (isExclude(opts.exclude[i], file)) return;
}
} else {
throw new Error('options.exclude should be RegExp or Array.');
}
}

参考阅读
vue项目中使用了vw适配方案,引入第三方ui框架mint-ui时,适配问题解决的更多相关文章
- 基于vue 的 UI框架 -- Mint UI
网址: http://mint-ui.github.io/docs/#!/zh-cn 官网: http://mint-ui.github.io/#!/zh-cn vue2.0实例: http://bl ...
- vue项目中在同一页面多次引入同一个echarts图表的自适应问题
在父组件页面引入两次该图表子组件显示的效果: 由于是百分比宽高,所以在窗口发生变化时,需要让图表也跟着自适应,所以才出现了本次讨论的问题啦. 先看下完整的图表子组件代码(在父组件就是直接引入,不需要传 ...
- Vue项目中应用TypeScript
一.前言 与如何在React项目中应用TypeScript类似 在VUE项目中应用typescript,我们需要引入一个库vue-property-decorator, 其是基于vue-class-c ...
- 转:如何在Vue项目中使用vw实现移动端适配
https://www.w3cplus.com/mobile/vw-layout-in-vue.html 有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flex ...
- 在Vue项目中使用vw实现移动端适配
有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着viewport单位越来越 ...
- 如何在Vue项目中使用vw实现移动端适配(转)
有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着viewport单位越来越 ...
- Vue项目中使用vw实现移动端适配
我们在vue移动端项目中的适配一般都采用rem,但是rem也不是能兼容所有的终端. 随着viewport单位越来越受到众多浏览器的支持,下面将简单介绍怎么实现vw的兼容问题,用vw代替rem 当我们采 ...
- 如何在Vue项目中使用vw实现移动端适配
有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在< 使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着 viewport 单位 ...
- vue项目中postcss-pxtorem的使用及webpack中的配置 css中单位px和em,rem的区别
移动手机版要求我们在制作嵌入h5的时候去适配不同的手机.适配有多重模式,有flex.百分比等.字体大小的控制也有px.百分比.rem等单位,webpack中 px转rem. vue项目中postcss ...
随机推荐
- 问题:oracle DECLARE 变量重复利用;结果:Oracle 定义变量总结
首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...
- leetcode230
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- jQuery-图片的放大镜显示效果(需要大小图)
1.default.aspx <%@ Page Language=.2em; height:.1em; text-align:center; font-size:128px;} .zxx_ ...
- Redis Cluster 伪集群的搭建
简介 为何要搭建Redis集群?Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系统的Hbase或者是MogoDB ...
- Solr搜索的排序打分规则探讨
使用Solr搭建搜索引擎很容易,但是如何制定合理的打分规则(boost)做排序却是一个很头痛的事情.Solr本身的排序打分规则是继承自Lucene的文本相关度的打分即boost,这一套算法对于通用的提 ...
- css背景图片位置:background的position(转)
css背景图片位置:background的position position的两个参数:水平方向的位置,垂直方向的位置----------该位置是指背景图片相对于前景对象的 1.backgroun ...
- centos 端口iptables配置
1.安装iptables yum install iptables* -y 2.打开端口 iptables -I INPUT -p tcp --dport -j ACCEPT 3.查看本机关于IPTA ...
- Spring 框架学习 有用
1.1.1 spring的优势 方便解耦,简化开发 通过Spring提供的IoC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合.用户也不必再为单例模式类.属性文件 ...
- html 5 data-* (dataset) 属性和 jquery data方法比较
一些文章在介绍html 5 data-* (dataset)属性时,会提到jquery的data方法,认为data方法能够很好的利用html 5的这个特性.但实际上,二者的兼容性是很差的.下面给出一段 ...
- python3-函数的参数的四种简单用法:
def print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1,arg2) ...