小編給大家分享一下html5頁面中rem布局適配的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

rem 布局適配方案
主要方法為:
按照設(shè)計(jì)稿與設(shè)備寬度的比例,動(dòng)態(tài)計(jì)算并設(shè)置 html 根標(biāo)簽的 font-size 大小;
css 中,設(shè)計(jì)稿元素的寬、高、相對(duì)位置等取值,按照同等比例換算為 rem 為單位的值;
設(shè)計(jì)稿中的字體使用 px 為單位,通過媒體查詢稍作調(diào)整。
1 動(dòng)態(tài)設(shè)置 html 標(biāo)簽 font-size 大小
精簡(jiǎn)通用版本:
!(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);
}
//窗口更新動(dòng)態(tài)改變 font-size
WIN.addEventListener('resize', dbcRefresh, false);
//頁面顯示時(shí)計(jì)算一次
WIN.addEventListener('pageshow', function(e) {
if (e.persisted) {
dbcRefresh()
}
}, false);
setFontSize();
})(window)
//對(duì)H5活動(dòng)推過頁面,寬高比例有所要求,可適當(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'
});
// 重新計(jì)算 rem
window.setFontSize(width);
}2 通過 CSS3媒體查詢?cè)O(shè)置 rem
簡(jiǎn)單易用 缺乏靈活度 請(qǐng)看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ù)個(gè)人項(xiàng)目需求和產(chǎn)品設(shè)計(jì)可適當(dāng)修改,以上demo寫法并不固定。
看完了這篇文章,相信你對(duì)“html5頁面中rem布局適配的示例分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:html5頁面中rem布局適配的示例分析-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://www.chinadenli.net/article42/dosphc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、小程序開發(fā)、ChatGPT、網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)