参考博客地址
此方案仅适用于移动端web
原理 动态设置 html 的font-size, 同时根据设备DPR调整页面的缩放值,进而达到高清效果
注意:
重要的事情说n次
绝不是每个地方都要用rem,rem只适用于固定尺寸!
绝不是每个地方都要用rem,rem只适用于固定尺寸!
绝不是每个地方都要用rem,rem只适用于固定尺寸!1
2
3<div style='width:100px;height:100px;'>
<div style='width:100%;height:100px;background:red;'></div>
</div>
上诉代码中内层div在ios系统下无法显示出来,高度为0
解决办法:内层div使用固定高度或者用内部元素将其高度撑起来
在必要的地方必须使用flex或者百分比才能完美布局
产品效果图
首先在HTML头部引用这段js1
2<script>!function(e){function t(a){if(i[a])return i[a].exports;var n=i[a]={exports:{},id:a,loaded:!1};return e[a].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=window;t["default"]=i.flex=function(e,t){var a=e||100,n=t||1,r=i.document,o=navigator.userAgent,d=o.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i),l=o.match(/U3\/((\d+|\.){5,})/i),c=l&&parseInt(l[1].split(".").join(""),10)>=80,p=navigator.appVersion.match(/(iphone|ipad|ipod)/gi),s=i.devicePixelRatio||1;p||d&&d[1]>534||c||(s=1);var u=1/s,m=r.querySelector('meta[name="viewport"]');m||(m=r.createElement("meta"),m.setAttribute("name","viewport"),r.head.appendChild(m)),m.setAttribute("content","width=device-width,user-scalable=no,initial-scale="+u+",maximum-scale="+u+",minimum-scale="+u),r.documentElement.style.fontSize=a/2*s*n+"px"},e.exports=t["default"]}]);
flex(100, 1);</script>
重置浏览器默认样式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
44
45
46
47
48
49* {
box-sizing: border-box;
/* 在X5新内核Blink中,在排版页面的时候,会主动对字体进行放大,会检测页面中的主字体,当某一块字体在我们的判定规则中,认为字号较小,并且是页面中的主要字体,就会采取主动放大的操作。然而这不是我们想要的,可以采取给最大高度解决 */
max-height: 100000px;
}
*:before, *:after {
box-sizing: border-box;
max-height: 100000px;
}*, *:before, *:after {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp, small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th,textarea, td {
border: 0 none;
font-size: inherit;
color: inherit;
margin: 0;
padding: 0;
vertical-align: baseline;
}
h1, h2, h3, h4, h5, h6 {
font-weight: normal;
}
em, strong {
font-style: normal;
}
ul, ol, li {
list-style: none;
}
body {
font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","\5FAE\8F6F\96C5\9ED1",Arial,sans-serif;
line-height: 1.5;
color: #333;
background-color: #f2f2f2;
font-size: 0.24rem;
}
a {
text-decoration: none;
}
.box{
position: relative;
max-width: 10rem;
margin: 0 auto;
}
小结:
咱们的rem适合写固定尺寸。其余的根据需要换成flex或者百分比。
感觉要飞起来了 哈哈哈 SO EASY!