前言
接收一个之前的项目,这里的适配布局使用的是原生监听,重写登录页面存在,键盘弹起导致页面适配变形
重写移动适配
(function(win) {
var tid;
function refreshRem() {
let designSize = 750;
let html = document.documentElement;
let wW = html.clientWidth;
let rem = wW * 100 / designSize;
document.documentElement.style.fontSize = rem + 'px';
}
win.addEventListener('resize', function() {
clearTimeout(tid);
tid = setTimeout(refreshRem);
}, false);
win.addEventListener('pageshow', function(e) {
if (e.persisted) {
clearTimeout(tid);
tid = setTimeout(refreshRem);
}
}, false);
refreshRem();
})(window);
pc端适配
!(function (doc, win) {
var docEle = doc.documentElement,
evt = "onorientationchange" in window ? "orientationchange" : "resize",
fn = function () {
var width = docEle.clientWidth;
var fs = (width / 1920) * 100
if (width < 960) {
fs = (960 / 1920) * 100
}
width && (docEle.style.fontSize = fs + "px");
};
fn()
win.addEventListener(evt, fn, false);
doc.addEventListener("DOMContentLoaded", fn, false);
document.addEventListener("error", function (e) {
var elem = e.target;
if (elem.tagName.toLowerCase() == 'img') {
elem.src = "static/images/imgLoadFail.jpg";
}
}, true);
}(document, window));