(function () {
var docEl = document.documentElement;
var isMobile = window.isMobile /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobi/i.test(navigator.userAgent);
function setScale() {
var pageScale = 1;
if (window.top !== window) {
return pageScale;
}
var width = docEl.clientWidth || 360;
var height = docEl.clientHeight || 640;
if (width / height >= 360 / 640) {
// 高度优先
pageScale = height / 640;
} else {
pageScale = width / 360;
}
var content = 'width=' + 360 + ', initial-scale=' + pageScale
+ ', maximum-scale=' + pageScale + ', user-scalable=no';
document.getElementById('viewport').setAttribute('content', content);
window.pageScale = pageScale;
}
if (isMobile) {
setScale();
} else {
docEl.className += ' pc';
}
})() (function(WIN) {
var setFontSize = WIN.setFontSize = function (_width) {
var docEl = document.documentElement;
// 获取当前窗口的宽度
var width = _width || docEl.clientWidth; // docEl.getBoundingClientRect().width;
// 大于 1080px 按 1080
if (width > 1080) {
width = 1080;
}
var rem = width / 10;
console.log(rem);
docEl.style.fontSize = rem + 'px';
// 部分机型上的误差、兼容性处理
var actualSize = win.getComputedStyle && parseFloat(win.getComputedStyle(docEl)["font-size"]);
if (actualSize !== rem && actualSize > 0 && Math.abs(actualSize - rem) > 1) {
var remScaled = rem * rem / actualSize;
docEl.style.fontSize = remScaled + 'px';
}
}
var timer;
//函数节流
function dbcRefresh() {
clearTimeout(timer);
timer = setTimeout(setFontSize, 100);
}
//窗口更新动态改变 font-size
WIN.addEventListener('resize', dbcRefresh, false);
//页面显示时计算一次
WIN.addEventListener('pageshow', function(e) {
if (e.persisted) {
dbcRefresh()
}
}, false);
setFontSize();
})(window) function adjustWarp(warpId = '#warp') {
// if (window.isMobile) return;
const $win = $(window);
const height = $win.height();
let width = $win.width();
// 考虑导航栏情况
if (width / height < 360 / 600) {
return;
}
width = Math.ceil(height * 360 / 640);
$(warpId).css({
height,
width,
postion: 'relative',
top: 0,
left: 'auto',
margin: '0 auto'
});
// 重新计算 rem
window.setFontSize(width);
} const px2rem = function(px, rem = 108) {
let remVal = parseFloat(px) / rem;
if (typeof px === "string" && px.match(/px$/)) {
remVal += 'rem';
}
return remVal;
} .img_demo {
position: absolute;
background-size: cover;
background-image: url('demo.png');
top: 2.97222rem;
left: 0.64814rem;
width: 4.25926rem;
height: 1.94444rem;
} // px 转 rem
.px2rem(@px, @attr: 'width', @rem: 108rem) {
@{attr}: (@px / @rem);
}
.px2remTLWH(@top, @left, @width, @height, @rem: 108rem) {
.px2rem(@top, top, @rem);
.px2rem(@left, left, @rem);
.px2rem(@width, width, @rem);
.px2rem(@height, height, @rem);
} .img_demo {
position: absolute;
background-size: cover;
background-image: url('demo.png');
.px2remTLWH(321, 70, 460, 210);
} // 字体响应式
@media screen and (max-width: 321px) {
body {
font-size: 13px;
}
}
@media screen and (min-width: 321px) and (max-width: 400px) {
body {
font-size: 14px;
}
}
@media screen and (min-width: 400px) {
body {
font-size: 16px;
}
} 相关推荐:
CSS网格布局的示例代码
CSS布局有哪些技巧
html中布局标记与列表标记的图文详解
以上就是详解H5 活动页之移动端 REM 布局适配方法的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于详解H5活动页之移动端REM布局适配方法的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did40561