<div class="header-layout">
<div class="header">
<i class="iconfont icon-return"></i>
<div>
<i class="iconfont icon-collect"></i>
<a href="javascript:;">
<i class="iconfont icon-share"></i>
</a>
</div>
</div>
</div>
.header-layout {
position: fixed;
left: 0;
top: 0;
z-index: 1000;
width: 100%;
height: 54px;
}
.header {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
transition: background 0.5s;
background-color: red;
}
.header-transition {
background-color:rgba(25,196,138,0.9);
}
(function () {
// 严格模式
'use strict';
// 以下这块是通用适配原理
// dpr-> scale=1/dpr
var docEl = document.documentElement;
var viewportEl = document.querySelector('meta[name="viewport"]');
var dpr = window.devicePixelRatio || 1;
var maxWidth = 540;
var minWidth = 320;
dpr = dpr >= 3 ? 3 : (dpr >= 2 ? 2 : 1);
docEl.setAttribute('data-dpr', dpr);
docEl.setAttribute('max-width', maxWidth);
docEl.setAttribute('min-width', minWidth);
var scale = 1 / dpr;
var content = "width=device-width, initial-scale=" + scale + ",maximum-scale=" + scale + ",minimum-scale=" + scale + ",user-scalable=no";
if (viewportEl) {
viewportEl.setAttribute('content', content);
} else {
viewportEl = document.createElement('meta');
viewportEl.setAttribute('name', 'viewport');
viewportEl.setAttribute('content', content);
document.head.appendChild(viewportEl);
}
// 以下是核心原理
setRemUnit();
window.addEventListener('resize', setRemUnit);
function setRemUnit() {
var ratio = 18.75;
var viewWidth = docEl.getBoundingClientRect().width || window.innerWidth;
// console.log(viewWidth);
if (maxWidth && (viewWidth / dpr > maxWidth)) {
viewWidth = maxWidth * dpr;
} else if (minWidth && (viewWidth / dpr < minWidth)) {
viewWidth = minWidth * dpr;
}
docEl.style.fontSize = viewWidth / ratio + 'px';
}
})();
这些是有两个公共样式,项目有
import 'styles/reset.css'; //重置样式(多网页共用)
import 'styles/base.css'; //公共样式(自己的网页使用)
import './details.css';
import 'icons/iconfont.css';
// 屏幕适配
import './flexible';
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星