效果
先用脚手架安装基础框架 并实现一个水平居中的效果 如下图所示
结构目录如下
reset.css
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
demo.vue
<template>
<div class="demo">
<div class="bigBox">
<div class="smallBox"></div>
</div>
</div>
</template>
<script >
</script>
<style>
.demo{
height:100vh;
display: flex;
justify-content: center;
align-items: center;
}
.bigBox{
width:400px;
height:400px;
background:pink;
display: flex;
justify-content: center;
align-items: center;
}
.bigBox>.smallBox{
width:200px;
height:200px;
background:blue;
}
</style>
现在拖动屏幕是没效果的 应该是这样
安装 lib-flexible(px转换成rem)
npm install lib-flexible --save-dev
引入 lib-flexible
在main.js
中引入lib-flexible
import 'lib-flexible'
import '@/assets/css/reset.css'
接下来会看着这个问题
注:html的font-size为 宽度/10 即正常
手机端的font-size是正常的
pc端的font-size始终不正确
找到源码
打开./node_modules/lib-flexible/flexible.js
,找到如下片段源码:
function refreshRem(){
var width = docEl.getBoundingClientRect().width;
if (width / dpr > 540) {
width = 540 * dpr;
}
var rem = width / 10;
docEl.style.fontSize = rem + 'px';
flexible.rem = win.rem = rem;
}
修改源码
function refreshRem(){
var width = docEl.getBoundingClientRect().width;
if (width / dpr > 1920) {
width = 1920 * dpr;
}
var rem = width / 10;
docEl.style.fontSize = rem + 'px';
flexible.rem = win.rem = rem;
}
现在pc端应该是正常的
安装 px2rem-loader
npm install px2rem-loader --save-dev
配置 px2rem-loader
1.在build/utils.js
中,找到exports.cssLoaders
,作出如下修改:
exports.cssLoaders = function (options) {
options = options || {}
const px2remLoader = {
loader: 'px2rem-loader',
options: {
remUint: 192
}
}
....
2.继续找到generateLoaders
中的loaders
配置,作出如下配置:
// const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
const loaders = [cssLoader, px2remLoader]
运行项目 ... 新的bug来了 元素变得超级大
继续修改源码
目录:node_modules/px2rem/lib/px2rem.js
修改为
var defaultConfig = {
baseDpr: 1, // base device pixel ratio (default: 2)
remUnit: 192, // rem unit value (default: 75)
remPrecision: 6, // rem value precision (default: 6)
forcePxComment: 'px', // force px comment (default: `px`)
keepComment: 'no' // no transform value comment (default: `no`)
};
重启! 正常啦
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!