最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

html5页面rem布局适配方法详解

来源:懂视网 责编:小OO 时间:2020-11-27 15:06:35
文档

html5页面rem布局适配方法详解

rem 布局适配方案。主要方法为。按照设计稿与设备宽度的比例,动态计算并设置 html 根标签的 font-size 大小。css 中,设计稿元素的宽、高、相对位置等取值,按照同等比例换算为 rem 为单位的值。设计稿中的字体使用 px 为单位,通过媒体查询稍作调整。1 动态设置 html 标签 font-size 大小。精简通用版本。
推荐度:
导读rem 布局适配方案。主要方法为。按照设计稿与设备宽度的比例,动态计算并设置 html 根标签的 font-size 大小。css 中,设计稿元素的宽、高、相对位置等取值,按照同等比例换算为 rem 为单位的值。设计稿中的字体使用 px 为单位,通过媒体查询稍作调整。1 动态设置 html 标签 font-size 大小。精简通用版本。

本文主要介绍了详解html5页面 rem 布局适配方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。

rem 布局适配方案

主要方法为:

  1. 按照设计稿与设备宽度的比例,动态计算并设置 html 根标签的 font-size 大小;

  2. css 中,设计稿元素的宽、高、相对位置等取值,按照同等比例换算为 rem 为单位的值;

  3. 设计稿中的字体使用 px 为单位,通过媒体查询稍作调整。

1 动态设置 html 标签 font-size 大小

精简通用版本:

!(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 通过 CSS3媒体查询设置 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}}

根据个人项目需求和产品设计可适当修改,以上demo写法并不唯一固定。

文档

html5页面rem布局适配方法详解

rem 布局适配方案。主要方法为。按照设计稿与设备宽度的比例,动态计算并设置 html 根标签的 font-size 大小。css 中,设计稿元素的宽、高、相对位置等取值,按照同等比例换算为 rem 为单位的值。设计稿中的字体使用 px 为单位,通过媒体查询稍作调整。1 动态设置 html 标签 font-size 大小。精简通用版本。
推荐度:
标签: 页面 网页 html5
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top