Mobile terminal solutions -rem adaptive page layout (advanced version, with source code examples)

A year ago I wrote an article  "Mobile Adaptive Solutions -rem end page layout" , unexpected by many friends and likes attention. But over time, the program is outdated, it is to introduce a present, I highly recommend the use of a more perfect program --rem layout (Advanced Edition)

In addition:
  • This embodiment is only applicable to a mobile terminal web
  • Frequently asked questions about the bottom of the article Article IV , the author has given a very convenient solution, welcome message exchange. (2017/9/9)

The use of fairly simple scheme, the following has been compressed this  native the JS (only 1KB, source code has been updated at the bottom of the article, 2017/5/3) into HTML tags to the head (Note: Do not manually set the viewport, the program automatically help you set up)

<script>!function(e){function t(a){if(i[a])return i[a].exports;var n=i[a]={exports:{},id:a,loaded:!1};return e[a].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var i={};returnt.m=e,t.c=i,t.p="",t(0)}([function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=window;t["default"]=i.flex=function(normal,e,t){vara=e||100,n=t||1,r=i.document,o=navigator.userAgent,d=o.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i),l=o.match(/U3\/((\d+|\.){5,})/i),c=l&&parseInt(l[1].split(".").join(""),10)>=80,p=navigator.appVersion.match(/(iphone|ipad|ipod)/gi),s=i.devicePixelRatio||1;p||d&&d[1]>534||c||(s=1);var u=normal?1:1/s,m=r.querySelector('meta[name="viewport"]');m||(m=r.createElement("meta"),m.setAttribute("name","viewport"),r.head.appendChild(m)),m.setAttribute("content","width=device-width,user-scalable=no,initial-scale="+u+",maximum-scale="+u+",minimum-scale="+u),r.documentElement.style.fontSize=normal?"50px": a/2*s*n+"px"},e.exports=t["default"]}]); flex(false,100, 1);</script>

Code Principle

This definition team Ali layout code program, the program is based on the so-called high-definition screen apparatus DPR (pixel bit devices, also known as DPPX, such as 2 dpr =, 1 represents CSS pixels of four physical pixels of)  dynamic setting html the font-size, while the apparatus according to the scaling value adjustments DPR page, so as to achieve the effect definition .

What are the advantages

  • References to simple, easy layout
  • According to DPR device screen, automatically sets the most suitable high-definition zoom.
  • Ensure the consistency of different devices under visual experience. (Old scheme, the greater the bigger the screen element; this scenario is that the bigger the screen, to see more)
  • 1px effectively solve real problems mobile terminal (here 1px physical pixel on the device display)

how to use

The important thing to say three times!
Each place is not to be used rem, rem only applies to a fixed size!
Each place is not to be used rem, rem only applies to a fixed size!
Each place is not to be used rem, rem only applies to a fixed size!
In a considerable number of layout context (such as the bottom of the navigation elements split the screen wide, large-sized elements), you must use a percentage or flex to the perfect layout!
Read the  "mobile terminal solutions -rem adaptive page layout," a friend, should be understood to rem, not repeat them here,
this program is also the default 1rem = 100px, so when your layout, you can follow the designer renderings of various sizes write it.
For example, the amount you take in rendering a button element in the long 55px, width 37px, then you can write directly to Style:

.myBtn {
   width: 0.55rem;
   height: 0.37rem; }

rem layout (Advanced Edition) practical application

IPhone5 page under the effect .png

iPhone 6 Plus page under the effect .png

In order to make friends, feel more clearly the great advantages of this program, the following is the source code and Demo

Practical Application 1 (please visit the better results in the end of the phone or Phone mode!)
2 practical application (please visit the better results in the end of the phone or Phone mode!)
Example Source
Online Demo

Frequently asked questions, it is necessary to look at the novice (2017/1/19)

There are many misconceptions lead many students to the program using a variety of problems arise, uniform reply at here.

1. Q: Why mobile web renderings width to 640 or 750, baa not I have to get hold of 666?

答:老实说当然可以,不过为了规范,640或者750是相对合适的。
拿Iphone 5s 举例,它的css像素宽度是320px,由于它的dpr=2,所以它的物理像素宽度为320 × 2 = 640px,这也就是为什么,你在5s上截了一张图,在电脑上打开,它的原始宽度是640px的原因。
那 iphone 6 的截图宽度呢? 375 × 2 = 750
那 iphone 6 sp 的截图宽度呢? 414 × 3 = 1242
以此类推,你现在能明白效果图为什么一般是 640 ,750 甚至是 1242 的原因了么?(真没有歧视安卓机的意思。。。)

2.问:宽度用rem写的情况下, 在 iphone6 上没问题, 在 iphone5上会有横向滚动条,何解?

答:假设你的效果图宽度是750,在这个效果图上可能有一个宽度为7rem(高清方案默认 1rem = 100px)的元素。我们知道,高清方案的特点就是几乎完美还原效果图,也就是说,你写了一个宽度为 7rem 的元素,那么在目前主流移动设备上都是7rem。然而,iphone 5 的宽度为640,也就是6.4rem。于是横向滚动条不可避免的出现了。
怎么办呢? 这是我目前推荐的比较安全的方式:如果元素的宽度超过效果图宽度的一半(效果图宽为640或750),果断使用百分比宽度,或者flex布局。就像把等屏宽的图片宽度设为100%一样。

3.问:不是 1rem = 100px吗,为什么我的代码写了一个宽度为3rem的元素,在电脑端的谷歌浏览器上宽度只有150px?

答:先说高清方案代码,再次强调咱们的高清方案代码是根据设备的dpr动态设置html 的 font-size,
如果dpr=1(如电脑端),则html的font-size为50px,此时 1rem = 50px
如果dpr=2(如iphone 5 和 6),则html的font-size为100px,此时 1rem = 100px
如果dpr=3(如iphone 6 sp),则html的font-size为150px,此时 1rem = 150px
如果dpr为其他值,即便不是整数,如3.4 , 也是一样直接将dpr 乘以 50 。

再来说说效果图,一般来讲,我们的效果图宽度要么是640,要么是750,无论哪一个,它们对应设备的dpr=2,此时,1 rem = 50 × 2 = 100px。这也就是为什么高清方案默认1rem = 100px。而将1rem默认100px也是好处多多,可以帮你快速换算单位,比如在750宽度下的效果图,某元素宽度为53px,那么css宽度直接设为53/100=0.53rem了。

然而极少情况下,有设计师将效果图宽定为1242px,因为他手里只有一个iphone 6 sp (dpr = 3),设计完效果图刚好可以在他的iphone 6 sp里查看调整。一切完毕之后,他将这个效果图交给你来切图。由于这个效果图对应设备的dpr=3,也就是1rem = 50 × 3 = 150px。所以如果你量取了一个宽度为90px的元素,它的css宽度应该为 90/150=0.6rem。由于咱们的高清方案默认1rem=100px,为了还原效果图,你需要这样换算。当然,一个技巧就是你可以直接修改咱们的高清方案的默认设置。在代码的最后 你会看到 flex(false, 100, 1) ,将其修改成flex(false, 66.66667, 1)(感谢简友:V旅行指出此处错误! 2017/3/24)就不用那么麻烦的换算了,此时那个90px的直接写成0.9rem就可以了。

4.问:在此方案下,我如果引用了别的UI库,那些UI库的元素会显得特别小,如何解决?

答:可以这样去理解问题的原因,如果不用高清方案,别的UI库的元素在移动设备上(假设这个设备是iphone 5好了)显示是正常的,这没有问题,然后我们在这个设备上将该页面截图放到电脑上看,发现宽度是640(问答1解释过了),根据你的像素眼大致测量,你发现这个设备上的某个字体大小应该是12px,而你在电脑上测量应该是24px。

现在我们使用高清方案去还原这个页面,那么字体大小应该写为 0.24rem 才对!

所以,如果你引用了其他的UI库,为了兼容高清方案,你需要对该UI库里凡是应用px的地方做相应处理,即: a px => a*0.02 rem
(具体处理方式因人而异,有模块化开发经验的同学可使用类似的 px2rem 的插件去转化,也可以完全手动处理)


**(2017/9/9更新)然而真实情况往往更为复杂,比如,你引入了百度地图(N个样式需要处理转换);或者你引入了一个 
framework;又或者你使用了 video 标签,上面默认的尺寸样式很难处理。等等这些棘手问题**

面对这些情况,此时我们的高清方案如果不再压缩页面,那么以上问题将迎刃而解。
基于这样的思路,笔者对高清方案的源码做了如下修改,即添加一个叫做 normal 的参数,由它来控制页面是否压缩。
在文章顶部代码的最后,你会看到 flex(false, 100, 1),默认情况下页面是开启压缩的。

如果你需要禁止压缩,由于我们的源码执行后,直接将flex函数挂载到全局变量window上了,此时你直接在需要禁止压缩的页面执行 window.flex(true) 就可以了,而rem的用法保持不变。

有一点美中不足的是,如果禁止了页面压缩,高清屏的1像素就不能实现了,如果你必须要实现1像素,那么自行谷歌:css 0.5像素,有N多的解决方案,这里不再赘述。

5.问:有时候字体会不受控制的变大,怎么办?

答:在X5新内核Blink中,在排版页面的时候,会主动对字体进行放大,会检测页面中的主字体,当某一块字体在我们的判定规则中,认为字号较小,并且是页面中的主要字体,就会采取主动放大的操作。然而这不是我们想要的,可以采取给最大高度解决

解决方案:

*, *:before, *:after { max-height: 100000px }

补充:有同学反映,在一些情况下 textarea 标签内的字体大小即便加上上面的方案,字体也会变大,无法控制。此时你需要给 textarea 的 display 设为 table 或者 inline-table 即可恢复正常。(感谢 程序媛喵喵 对此的补充!2017/7/7)

6.问:我在底部导航用的flex感觉更合适一些,请问这样子混着用可以吗?

A: Let's rem for writing a fixed size. The remaining flex or replaced as required percentage. Source example, there is the integrated use of these three.

7. Q: In HD program, a standard, an ideal width of 640 page renderings should be like?

Click here: a standard 640 mobile phone reference design draft page (yes, in this scenario, you can completely write the layout design in accordance with the size of this draft is that simple.!)

8. Q: Can you provide source code under this definition programs do?
'use strict';

/**
 * @param {Boolean} [normal = false] - 默认开启页面压缩以使页面高清;  
 * @param {Number} [baseFontSize = 100] - 基础fontSize, 默认100px;
 * @param {Number} [fontscale = 1] - 有的业务希望能放大一定比例的字体;
 */
const win = window;
export default win.flex = (normal, baseFontSize, fontscale) => { const _baseFontSize = baseFontSize || 100; const _fontscale = fontscale || 1; const doc = win.document; const ua = navigator.userAgent; const matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i); const UCversion = ua.match(/U3\/((\d+|\.){5,})/i); const isUCHd = UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80; const isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi); let dpr = win.devicePixelRatio || 1; if (!isIos && !(matches && matches[1] > 534) && !isUCHd) { // 如果非iOS, 非Android4.3以上, 非UC内核, 就不执行高清, dpr设为1; dpr = 1; } const scale = normal ? 1 : 1 / dpr; let metaEl = doc.querySelector('meta[name="viewport"]'); if (!metaEl) { metaEl = doc.createElement('meta'); metaEl.setAttribute('name', 'viewport'); doc.head.appendChild(metaEl); } metaEl.setAttribute('content', `width=device-width,user-scalable=no,initial-scale=${scale},maximum-scale=${scale},minimum-scale=${scale}`); doc.documentElement.style.fontSize = normal ? '50px' : `${_baseFontSize / 2 * dpr * _fontscale}px`; };
9. Q: I have a problem when using XXX in rem advanced layout program, how to solve?
  • This program proven, universally applicable, the case itself is rarely a fatal problem, at least I is not met.
  • Most problems you encounter are due to rem understanding is not in place due to the layout. In this paper, the layout rem done a lot of explanation, a number of demo configuration, you can put your problems into the demo in the test. When they encounter problems, first ask yourself why this obvious mistake we did not encounter it I met? ?
  • If you are really well-proven, alignment, indeed rem layout itself is a problem, then please private letter to me, the problem of reducing the demo scene or file to me. Thank you!

 

 

https://segmentfault.com/a/1190000007350680

Guess you like

Origin www.cnblogs.com/2019gdiceboy/p/11091468.html