资讯专栏INFORMATION COLUMN

H5 页面 rem 布局适配方法

acrazing / 1768人阅读

摘要:布局适配方案主要方法为按照设计稿与设备宽度的比例,动态计算并设置根标签的大小中,设计稿元素的宽高相对位置等取值,按照同等比例换算为为单位的值设计稿中的字体使用为单位,通过媒体查询稍作调整。

rem 布局适配方案

</>复制代码

  1. 主要方法为:

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

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

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

1 动态设置 html 标签 font-size 大小 精简通用版本:

</>复制代码

  1. !(function(win, doc){
  2. function setFontSize() {
  3. // 获取window 宽度
  4. // zepto实现 $(window).width()就是这么干的
  5. var winWidth = window.innerWidth;
  6. // doc.documentElement.style.fontSize = (winWidth / 640) * 100 + "px" ;
  7. // 640宽度以上进行限制 需要css进行配合
  8. var size = (winWidth / 640) * 100;
  9. doc.documentElement.style.fontSize = (size < 100 ? size : 100) + "px" ;
  10. }
  11. var evt = "onorientationchange" in win ? "orientationchange" : "resize";
  12. var timer = null;
  13. win.addEventListener(evt, function () {
  14. clearTimeout(timer);
  15. timer = setTimeout(setFontSize, 300);
  16. }, false);
  17. win.addEventListener("pageshow", function(e) {
  18. if (e.persisted) {
  19. clearTimeout(timer);
  20. timer = setTimeout(setFontSize, 300);
  21. }
  22. }, false);
  23. // 初始化
  24. setFontSize();
  25. }(window, document));
高配精确版本:

</>复制代码

  1. (function(WIN) {
  2. var setFontSize = WIN.setFontSize = function (_width) {
  3. var docEl = document.documentElement;
  4. // 获取当前窗口的宽度
  5. var width = _width || docEl.clientWidth; // docEl.getBoundingClientRect().width;
  6. // 大于 1080px 按 1080
  7. if (width > 1080) {
  8. width = 1080;
  9. }
  10. var rem = width / 10;
  11. console.log(rem);
  12. docEl.style.fontSize = rem + "px";
  13. // 误差、兼容性处理
  14. var actualSize = win.getComputedStyle && parseFloat(win.getComputedStyle(docEl)["font-size"]);
  15. if (actualSize !== rem && actualSize > 0 && Math.abs(actualSize - rem) > 1) {
  16. var remScaled = rem * rem / actualSize;
  17. docEl.style.fontSize = remScaled + "px";
  18. }
  19. }
  20. var timer;
  21. function dbcRefresh() {
  22. clearTimeout(timer);
  23. timer = setTimeout(setFontSize, 100);
  24. }
  25. //窗口更新动态改变 font-size
  26. WIN.addEventListener("resize", dbcRefresh, false);
  27. //页面显示时计算一次
  28. WIN.addEventListener("pageshow", function(e) {
  29. if (e.persisted) {
  30. dbcRefresh()
  31. }
  32. }, false);
  33. setFontSize();
  34. })(window)
  35. //对H5活动推过页面,宽高比例有所要求,可适当调整
  36. function adjustWarp(warpId = "#warp") {
  37. const $win = $(window);
  38. const height = $win.height();
  39. let width = $win.width();
  40. // 考虑导航栏情况
  41. if (width / height < 360 / 600) {
  42. return;
  43. }
  44. width = Math.ceil(height * 360 / 640);
  45. $(warpId).css({
  46. height,
  47. width,
  48. postion: "relative",
  49. top: 0,
  50. left: "auto",
  51. margin: "0 auto"
  52. });
  53. // 重新计算 rem
  54. window.setFontSize(width);
  55. }
2 通过 CSS3媒体查询设置 rem

简单易用 缺乏灵活度 请看demo 你懂的

</>复制代码

  1. @media screen and ( min-width: 320px){html{font-size:50px}}
  2. @media screen and ( min-width: 360px){html{font-size:56.25px}}
  3. @media screen and ( min-width: 375px){html{font-size:58.59375px}}
  4. @media screen and ( min-width: 384px){html{font-size:60px}}
  5. @media screen and ( min-width: 400px){html{font-size:62.5px}}
  6. @media screen and ( min-width: 414px){html{font-size:64.6875px}}
  7. @media screen and ( min-width: 424px){html{font-size:66.25px}}
  8. @media screen and ( min-width: 480px){html{font-size:75px}}
  9. @media screen and ( min-width: 540px){html{font-size:84.375px}}
  10. @media screen and ( min-width: 640px){html{font-size:100px}}

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41220.html

相关文章

  • H5 页面 rem 布局适配方法

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

    Lowky 评论0 收藏0
  • H5 页面 rem 布局适配方法

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

    littlelightss 评论0 收藏0
  • H5 页面 rem 布局适配方法

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

    mtunique 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<