This article mainly introduces the detailed explanation of the HTML5 page rem layout adaptation method. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look, I hope it can help everyone.
rem layout adaptation scheme
The main method is:
Dynamic calculation based on the ratio of the design draft to the width of the device And set the font-size size of the html root tag; in
#css, the width, height, relative position, etc. of the design draft elements are converted into values in rem units according to the same proportion. ;
The font in the design draft uses px as the unit and is slightly adjusted through media queries.
1 Dynamically set html tag font-size size
Simplified universal version:
##
!(function(win, doc){ function setFontSize() { // 获取window 宽度 // zepto实现 $(window).width()就是这么干的 var winWidth = window.innerWidth; // doc.documentElement.style.fontSize = (winWidth / 640) * 100 + 'px' ; // 640宽度以上进行限制 需要css进行配合 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; // 获取当前窗口的宽度 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) //对H5活动推过页面,宽高比例有所要求,可适当调整 function adjustWarp(warpId = '#warp') { 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); }
2 Set rem through CSS3 media query
Easy to use, lack of flexibility, please see demo you know@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}}
javascript uses rem to do responsive development example sharing
Two mobile terminal rem layout implementations Method
Example to explain js implementation of rem automatic matching calculation font-size
The above is the detailed content of Detailed explanation of HTML5 page rem layout adaptation method. For more information, please follow other related articles on the PHP Chinese website!