前端怎么设置手机端页面高度自适应
5个回答
展开全部
js代码
?
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 50 51 52 53 54 !function(win) { function resize() { var domWidth = domEle.getBoundingClientRect().width; if(domWidth / v > 540){ domWidth = 540 * v; } win.rem = domWidth / 16; domEle.style.fontSize = win.rem + "px"; } var v, initial_scale, timeCode, dom = win.document, domEle = dom.documentElement, viewport = dom.querySelector('meta[name="viewport"]'), flexible = dom.querySelector('meta[name="flexible"]'); if (viewport) { //viewport:<meta name="viewport"content="initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5,user-scalable=no,minimal-ui"/> var o = viewport.getAttribute("content").match(/initial-scale=(["']?)([d.]+)1?/); if(o){ initial_scale = parseFloat(o[2]); v = parseInt(1 / initial_scale); } } else { if (flexible) { var o = flexible.getAttribute("content").match(/initial-dpr=(["']?)([d.]+)1?/); if(o){ v = parseFloat(o[2]); initial_scale = parseFloat((1 / v).toFixed(2)) } } } if (!v && !initial_scale) { var n = (win.navigator.appVersion.match(/android/gi), win.navigator.appVersion.match(/iphone/gi)); v = win.devicePixelRatio; v = n ? v >= 3 ? 3 : v >= 2 ? 2 : 1 : 1, initial_scale = 1 / v } //没有viewport标签的情况下 if (domEle.setAttribute("data-dpr", v), !viewport) { if (viewport = dom.createElement("meta"), viewport.setAttribute("name", "viewport"), viewport.setAttribute("content", "initial-scale=" + initial_scale + ", maximum-scale=" + initial_scale + ", minimum-scale=" + initial_scale + ", user-scalable=no"), domEle.firstElementChild) { domEle.firstElementChild.appendChild(viewport) } else { var m = dom.createElement("div"); m.appendChild(viewport), dom.write(m.innerHTML) } } win.dpr = v; win.addEventListener("resize", function() { clearTimeout(timeCode), timeCode = setTimeout(resize, 300) }, false); win.addEventListener("pageshow", function(b) { b.persisted && (clearTimeout(timeCode), timeCode = setTimeout(resize, 300)) }, false); /* 个人觉得没必要完成后就把body的字体设置为12 "complete" === dom.readyState ? dom.body.style.fontSize = 12 * v + "px" : dom.addEventListener("DOMContentLoaded", function() { //dom.body.style.fontSize = 12 * v + "px" }, false); */ resize(); }(window);
?
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 50 51 52 53 54 !function(win) { function resize() { var domWidth = domEle.getBoundingClientRect().width; if(domWidth / v > 540){ domWidth = 540 * v; } win.rem = domWidth / 16; domEle.style.fontSize = win.rem + "px"; } var v, initial_scale, timeCode, dom = win.document, domEle = dom.documentElement, viewport = dom.querySelector('meta[name="viewport"]'), flexible = dom.querySelector('meta[name="flexible"]'); if (viewport) { //viewport:<meta name="viewport"content="initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5,user-scalable=no,minimal-ui"/> var o = viewport.getAttribute("content").match(/initial-scale=(["']?)([d.]+)1?/); if(o){ initial_scale = parseFloat(o[2]); v = parseInt(1 / initial_scale); } } else { if (flexible) { var o = flexible.getAttribute("content").match(/initial-dpr=(["']?)([d.]+)1?/); if(o){ v = parseFloat(o[2]); initial_scale = parseFloat((1 / v).toFixed(2)) } } } if (!v && !initial_scale) { var n = (win.navigator.appVersion.match(/android/gi), win.navigator.appVersion.match(/iphone/gi)); v = win.devicePixelRatio; v = n ? v >= 3 ? 3 : v >= 2 ? 2 : 1 : 1, initial_scale = 1 / v } //没有viewport标签的情况下 if (domEle.setAttribute("data-dpr", v), !viewport) { if (viewport = dom.createElement("meta"), viewport.setAttribute("name", "viewport"), viewport.setAttribute("content", "initial-scale=" + initial_scale + ", maximum-scale=" + initial_scale + ", minimum-scale=" + initial_scale + ", user-scalable=no"), domEle.firstElementChild) { domEle.firstElementChild.appendChild(viewport) } else { var m = dom.createElement("div"); m.appendChild(viewport), dom.write(m.innerHTML) } } win.dpr = v; win.addEventListener("resize", function() { clearTimeout(timeCode), timeCode = setTimeout(resize, 300) }, false); win.addEventListener("pageshow", function(b) { b.persisted && (clearTimeout(timeCode), timeCode = setTimeout(resize, 300)) }, false); /* 个人觉得没必要完成后就把body的字体设置为12 "complete" === dom.readyState ? dom.body.style.fontSize = 12 * v + "px" : dom.addEventListener("DOMContentLoaded", function() { //dom.body.style.fontSize = 12 * v + "px" }, false); */ resize(); }(window);
展开全部
那高度你就不要去赋值了,不用写高度不就是高度自适应内容吗,
如果你想的是高度和屏幕高度一样,那就设置成100%;
如果你想的是高度和屏幕高度一样,那就设置成100%;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以参考一下rem.js
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
整个页面还是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那高度你就不要去赋值了,不用写高度不就是高度自适应内容吗,
如果你想的是高度和屏幕高度一样,那就设置成100%;
如果你想的是高度和屏幕高度一样,那就设置成100%;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询