Web pages do mobile adaptation, that is, in different sizes of mobile devices, the page "to achieve a reasonable display of relativity (adaptive)" or "to maintain a uniform effect of the scale (looks similar)." There are several points that must be mastered, one is different sizes, one is clear, and the other is reasonable. Why the emphasis on these words is, of course, for a reason.
First, the goal
1. The page layout always follows a certain rule. But mobile devices are varied, screen sizes are different, resolutions are different, pixels are different, and width and height are different (the concept is described later). So getting a set of code to fit all the devices is the most tricky. 、
2. To ensure clarity, small screen mobile phones and large-screen mobile phones with the same picture, by scaling the effect of the size of the display is consistent, but the image amplification will be distorted, high-definition image to reduce the preview and waste. This needs to be considered in a comprehensive context.
3. The so-called reasonable display means that no matter what equipment can have the original design of the same visual and experience effect. If you simply set the width to a fixed pixel px, the result will be what. Either the small screen mobile phone appears scroll bar, or the big screen phone has left white. It is output as is, but this is not the effect of the design draft. The design draft is a full screen perfect display, can not be turned into a half-screen or two screens on the phone. Therefore, the restoration of the design manuscript can be understood as proportional scaling.
Second, thinking
Our purpose was clear, but we had to pour cold water on it first. First of all, there is no perfect fit solution. It's impossible to make all the devices really 100% revert to the design. Because the device's wide and high proportions are not the same, there is no way to solve the light, for example, you want to use the same picture to square and rectangular wallpaper, there will always be a stretch or stay white. So at present the general principle is the text flow, the control elasticity, the picture and so on zoom. Use the Universal network film to show the effect.
With the goal and the general idea next almost on the plan, in order to better explain, but also have a few knowledge of science points.
Iii. Basic Concepts
Screen size (inch): The diagonal length of a mobile device, such as iphone5, is 4 inches.
Screen aspect ratio: The ratio of the phone's height to the width is 16:9
Physical pixels (px): The smallest physical display unit on a display (phone screen), such as Iphone5 's physical pixels, is 640*1136
Pixel density (PPI): Represents the number of physical pixels (Pixel) per inch along the diagonal, the lower the value, the stronger the particle, the more than 300 eyes are hard to tell.
Device independent pixels | density independent pixels | logical pixels (PX): It can be thought of as a point in a computer coordinate system that represents a virtual pixel (such as a CSS pixel) that can be used by a program and then converted from a related system to a physical pixel. For example, Iphone5 's physical pixels are 320*568.
Pixel ratio (PDR): The correspondence between physical pixels and device independent pixels, device pixel ratio = physical pixel/device independent pixels, such as iphone5 pixel ratio is 2.
The above concept if you can not accept self-baidu it. Here the main explanation under PDR, the original display equipment pdr are 1, later introduced the HD screen with retina, this pdr appeared. Of course, the higher the value, the clearer the display, and also means that when you control a pixel with CSS, you actually control the number of physical pixels. The simplest example is to use Iphone5 to intercept a map, although the maximum width of the CSS is only 320px, but the picture on the computer has become 640px.
Four, in-depth understanding of the adaptation of the HD problem
How much image can you use to make it clear?
If the PDR is 1, to display 100*100 's picture, use 50*50,100*100 or 200*200 's picture. Of course we can all understand that 50*50 must be ruled out. However, the reason is what we need to discuss. In other words, to fill 2 pits with the required red and yellow pigments, whether to use a single pigment or 2 or 4 species. If a pigment at the same time show the effect of red and yellow, then the two pigments can only be mixed and then poured, the results will certainly be distorted, if used in two, respectively, with red and yellow pour, then the difference; if you use 4 kinds, you have to compress half into, that is, red and yellow has been compressed half, but also infiltration into other This time, the display effect will lose sharpness. So whether the picture is big or small, it will not blur.
But a lot of equipment is not just pdr 1, if equal to 2, to display 100*100 pictures, you need to 200*200 pictures, if PDR 3, will introduce 300*300 pictures, there are more like silverhalide equipment, can not be satisfied, so the common practice is to provide 2x, 3x picture.
V. Adaptation PROGRAMME
Finally to talk about the plan, the above also talked about to achieve adaptive is proportional scaling. So we think of three options.
One, set the width height by the percentage. May achieve the adaptation effect, the disadvantage is that there will be stretching, blurring, because the device aspect ratio is different, height is difficult to control with a percentage.
Two, viewport zoom
You can set the scale of viewport in the page
property to control the page display scale. Similar to zooming in and out of a picture, the scale is calculated by calculating the ratio of the device width to the design draft, and finally the adaptation effect is achieved. It is a waste of resources in order to fit most devices and ensure that the clarity is designed with a larger resolution.
Iii. REM Relative units
Because REM is a relative unit, it measures length in the size of the font-size of the HTM. So calculate the device screen width and design draft ratio to the font-size of dynamic device HTML. This can be displayed on different devices in the upper proportion. It is equivalent to dividing the width of the screen into a fixed number of copies, and then counting as units of copies.
In fact, now the adaptation scheme is just a few, especially the third kind of operation is very convenient (the following will be attached code). However, the Ali team in order to pursue clarity and solve 1px problems and the introduction of high-definition adaptation scheme, mainly to consider the pixel ratio, combined with the second third program to achieve. By multiplying the device width with the PDR and then taking the ratio to the design, the font-size of the HTML is set dynamically, and the scale is dynamically set according to the PDR value.
Note: 1px problem refers to the use of REM, the pixel value will appear in the case of decimals, the decimal place may be directly ignored by the system, the phenomenon of wide and narrow.
With general adaptation scheme (code from the network):
1<script type= "Text/javascript" >2 New function (){3 var_self = This;4_self.width = 1080;//set the baseline dimensions provided by the UI5_self.fontsize = 200;//The default font size, divided by this parameter as REM, is recommended to take 10 or 100 for easy calculation. To ensure a good fit for Google, 320/_self.width*_self.fontsize>126_self.widthproportion =function(){7 varp = (document.body&&8Document.body.clientWidth | |9document.getElementsByTagName ("HTML") [0].offsetwidth]/_self.width;Ten One returnp>0.71?0.71: (P<0.3?0.3:P);//set security dimensions up to 768px, minimum 320px A }; - Console.log (_self.widthproportion ()); -_self.changepage =function(){ thedocument.getElementsByTagName ("HTML") [0].setattribute ("Style", "Font-size:" +_self.widthproportion () *_ self.fontsize+ "px!important"); - } - _self.changepage (); -Window.addeventlistener ("Resize",function() {_self.changepage ();},false); + }; - +</script>
HD Adaptation Scheme (code from the network):
1<script>2(function(Doc, Win, Designsize) {//Designsize for Design draft size (W)3 varDocel =Document.documentelement,4Devwidth = docel.clientwidth > 1080? 1080: Docel.clientwidth,5DPR = Devicepixelratio | | 1,6Scale = 1/DPR,7width = DPR *Devwidth,8resizeevt = ' Orientationchange 'inchWindow? ' Orientationchange ': ' OnResize ',//Judging horizontal screen and window reset9Recalc =function() { Ten varClientWidth =Docel.clientwidth; One if(!clientwidth)return; ADocument.queryselector (' meta[name= ' viewport "]). SetAttribute (' content ', ' width= ' + width + ', -Initial-scale= ' + scale + ', maximum-scale= ' + scale + ', minimum-scale= ' + scale + ', User-scalable=no '); -DocEl.style.fontSize = devwidth/(designsize/100) * DPR + ' px '; the }; - if(!doc.addeventlistener)return; -Win.addeventlistener (Resizeevt, Recalc,false); -Doc.addeventlistener (' domcontentloaded ', Recalc,false); +}) (document, window, 750); -</script>
Reference article:
1190000008767416
http://www.aliued.com/?p=3166
Www.jianshu.com/p/07669cb3e7c5