詳解html5頁面 rem 布局適配方法
發(fā)布時(shí)間:2018-01-12 15:31:46 作者:海麗安娜
我要評論

本篇文章主要介紹了詳解html5頁面 rem 布局適配方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
rem 布局適配方案
主要方法為:
- 按照設(shè)計(jì)稿與設(shè)備寬度的比例,動態(tài)計(jì)算并設(shè)置 html 根標(biāo)簽的 font-size 大??;
- css 中,設(shè)計(jì)稿元素的寬、高、相對位置等取值,按照同等比例換算為 rem 為單位的值;
- 設(shè)計(jì)稿中的字體使用 px 為單位,通過媒體查詢稍作調(diào)整。
1 動態(tài)設(shè)置 html 標(biāo)簽 font-size 大小
精簡通用版本:
!(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); //頁面顯示時(shí)計(jì)算一次 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' }); // 重新計(jì)算 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ù)個(gè)人項(xiàng)目需求和產(chǎn)品設(shè)計(jì)可適當(dāng)修改,以上demo寫法并不唯一固定。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- 這篇文章主要介紹了Html5移動端網(wǎng)頁端適配(js+rem),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)2021-02-03
html5中JavaScript removeChild 刪除所有節(jié)點(diǎn)
通過JavaScript可以刪除所有節(jié)點(diǎn),本文里主要討論 removeChild 函數(shù),大家可以看看下面的示例2014-05-16HTML5中 rem適配方案與 viewport 適配問題詳解
這篇文章主要介紹了HTML5中 rem適配方案與 viewport 適配問題詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-26