摘要:布局適配方案主要方法為按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置根標(biāo)簽的大小中,設(shè)計稿元素的寬高相對位置等取值,按照同等比例換算為為單位的值設(shè)計稿中的字體使用為單位,通過媒體查詢稍作調(diào)整。
rem 布局適配方案
1 動態(tài)設(shè)置 html 標(biāo)簽 font-size 大小 精簡通用版本:主要方法為:
按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置 html 根標(biāo)簽的 font-size 大小;
css 中,設(shè)計稿元素的寬、高、相對位置等取值,按照同等比例換算為 rem 為單位的值;
設(shè)計稿中的字體使用 px 為單位,通過媒體查詢稍作調(diào)整。
!(function(win, doc){
function setFontSize() {
// 獲取window 寬度
// zepto實(shí)現(xiàn) $(window).width()就是這么干的
var winWidth = window.innerWidth;
// doc.documentElement.style.fontSize = (winWidth / 640) * 100 + "px" ;
// 640寬度以上進(jìn)行限制 需要css進(jìn)行配合
var size = (winWidth / 640) * 100;
doc.documentElement.style.fontSize = (size < 100 ? size : 100) + "px" ;
}
var evt = "onorientationchange" in win ? "orientationchange" : "resize";
var timer = null;
win.addEventListener(evt, function () {
clearTimeout(timer);
timer = setTimeout(setFontSize, 300);
}, false);
win.addEventListener("pageshow", function(e) {
if (e.persisted) {
clearTimeout(timer);
timer = setTimeout(setFontSize, 300);
}
}, false);
// 初始化
setFontSize();
}(window, document));
高配精確版本:
(function(WIN) {
var setFontSize = WIN.setFontSize = function (_width) {
var docEl = document.documentElement;
// 獲取當(dāng)前窗口的寬度
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);
}
//窗口更新動態(tài)改變 font-size
WIN.addEventListener("resize", dbcRefresh, false);
//頁面顯示時計算一次
WIN.addEventListener("pageshow", function(e) {
if (e.persisted) {
dbcRefresh()
}
}, false);
setFontSize();
})(window)
//對H5活動推過頁面,寬高比例有所要求,可適當(dāng)調(diào)整
function adjustWarp(warpId = "#warp") {
const $win = $(window);
const height = $win.height();
let width = $win.width();
// 考慮導(dǎo)航欄情況
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);
}
2 通過 CSS3媒體查詢設(shè)置 rem
簡單易用 缺乏靈活度 請看demo 你懂的
@media screen and ( min-width: 320px){html{font-size:50px}}
@media screen and ( min-width: 360px){html{font-size:56.25px}}
@media screen and ( min-width: 375px){html{font-size:58.59375px}}
@media screen and ( min-width: 384px){html{font-size:60px}}
@media screen and ( min-width: 400px){html{font-size:62.5px}}
@media screen and ( min-width: 414px){html{font-size:64.6875px}}
@media screen and ( min-width: 424px){html{font-size:66.25px}}
@media screen and ( min-width: 480px){html{font-size:75px}}
@media screen and ( min-width: 540px){html{font-size:84.375px}}
@media screen and ( min-width: 640px){html{font-size:100px}}
根據(jù)個人項目需求和產(chǎn)品設(shè)計可適當(dāng)修改,以上demo寫法并不唯一固定。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.hztianpu.com/yun/112998.html
摘要:布局適配方案主要方法為按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置根標(biāo)簽的大小中,設(shè)計稿元素的寬高相對位置等取值,按照同等比例換算為為單位的值設(shè)計稿中的字體使用為單位,通過媒體查詢稍作調(diào)整。 rem 布局適配方案 主要方法為: 按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置 html 根標(biāo)簽的 font-size 大小; css 中,設(shè)計稿元素的寬、高、相對位置等取值,按照同等比例換算...
摘要:布局適配方案主要方法為按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置根標(biāo)簽的大小中,設(shè)計稿元素的寬高相對位置等取值,按照同等比例換算為為單位的值設(shè)計稿中的字體使用為單位,通過媒體查詢稍作調(diào)整。 rem 布局適配方案 主要方法為: 按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置 html 根標(biāo)簽的 font-size 大小; css 中,設(shè)計稿元素的寬、高、相對位置等取值,按照同等比例換算...
摘要:布局適配方案主要方法為按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置根標(biāo)簽的大小中,設(shè)計稿元素的寬高相對位置等取值,按照同等比例換算為為單位的值設(shè)計稿中的字體使用為單位,通過媒體查詢稍作調(diào)整。 rem 布局適配方案 主要方法為: 按照設(shè)計稿與設(shè)備寬度的比例,動態(tài)計算并設(shè)置 html 根標(biāo)簽的 font-size 大小; css 中,設(shè)計稿元素的寬、高、相對位置等取值,按照同等比例換算...
閱讀 909·2021-11-18 13:12
閱讀 1753·2021-11-15 11:39
閱讀 2720·2021-09-23 11:22
閱讀 6651·2021-09-22 15:15
閱讀 3969·2021-09-02 09:54
閱讀 2731·2019-08-30 11:10
閱讀 3490·2019-08-29 14:13
閱讀 3186·2019-08-29 12:49