Iphone6 and iphone 6 Plus have been out for some time. Many mobile sites, previously written dead body 320px, is now estimated to be busy with the adaptation.
big screen Mobile phone has always been, just before everyone, but the majority of the mobile H5 page to 320px for the base width of the layout, those big screen cock Silk Android users are too lazy to reason, and now the iphone also make a multi-screen, the boss is not the same degree of attention.
back to the point, the best way to be compatible with iphone versions is adaptive.
1, viewport simple rough way:
<meta name= "viewport" content= "Width=320,maximum-scale=1.3,user-scalable=no" >
set the viewport directly to 1.3 times times 320px and enlarge the page 1.3 times times.
Why is it 1.3?
most of the current pages are 320px-based layouts, while the width of iphone6 is 375/320 = 1.171875,iphone6+ is 414/320 = 1.29375
then the 1.29 times-fold is approximately equal to 1.3.
2. ip6+ CSS Media query
@media (min-device-width:375px) and (MAX-DEVICE-WIDTH:667PX) and (-webkit-min-device-pixel-ratio:2) {
/*iphone 6*/
}
@media (min-device-width:414px) and (max-device-width:736px) and (-webkit-min-device-pixel-ratio:3) {
/*iphone 6 plus*/
}
PS: can also directly use the actual device-width: such as device-width:375px
On the basis of the original page, and then for the corresponding screen size of the individual write style to do the matching.
3. REM Layout
REM is a new unit of CSS3, and the mobile side is highly supported by android2.x+,ios5+.
REM is relative to the root element of the DOM structure to set the size, that is, the HTML element. Compared to EM units, REM is easier to understand and use.
the REM and PX conversions can be viewed at: https://offroadcode.com/prototypes/rem-calculator/
Suppose that HTML we set font-size:12px, which means that 12px is relative to 1rem, then 18px is 18/12 = 1.5rem.
then we set the 320px layout as the benchmark, setting the HTML to font-size:100px, which is 100px = 1rem. (set 100px for ease of calculation) you can change the majority of PX units by 100 directly to REM units.
How does REM do responsive layout?
1. If the ip6+ device is only suitable, then use media query.
The pseudo code is as follows:
- /*320px Layout */
- html{font-size:100px;}
- Body{font-size:0.14rem/* actual equivalent to 14px*/}
- /* iphone 6 */
- @media (min-device-width:375px) and (MAX-DEVICE-WIDTH:667PX) and (-webkit-min-device-pixel-ratio:2) {
- html{font-size:117.1875px;}
- }
- /* iphone6 plus */
- @media (min-device-width:414px) and (max-device-width:736px) and (-webkit-min-device-pixel-ratio:3) {
- html{font-size:129.375px;}
- }
Copy Code
2, if it is fully adaptive, it can be controlled by JS.
- (Function (doc, win) {
- var docel = doc.documentelement,
- resizeevt = ' orientationchange ' in window? ' Orientationchange ': ' Resize ',
- Recalc = function () {
- var clientwidth = docel.clientwidth;
- if (!clientwidth) return;
- docEl.style.fontSize = * (clientwidth/320) + ' px ';
- };
- Abort If browser does not support AddEventListener
- if (!doc.addeventlistener) return;
- Win.addeventlistener (Resizeevt, Recalc, false);
- Doc.addeventlistener (' domcontentloaded ', Recalc, false);
- }) (document, window);
Copy Code
when the page is initialized, the font-size is computed and then the Resize event is bound. This effect is the same as the percent layout.
So what are the advantages of using REM to do units and percentages?
The main advantage is the ability to control the element size better. (The general percentage is applied to the layout layer, and the general settings are mostly integers such as 50%,33.3%,25%, which are difficult to use in complex page widgets).
However, compared to the percent layout, it needs to be implemented with JS or media query with a slight flaw.
4. Adaptive Image
just finished with the REM layout, the same effect can be achieved with a percent layout, but with a percentage layout, you have to face a problem: Picture width 100%, there is a high level of collapse when the page loads.
The image height does not exist by default when the page loads.
you can then use Padding-top to set the percent value for adaptive.
The formula is as follows:
padding-top = (Image height/image Width) * 100%
principle: When the Padding-top value is a percentage, the value is relative to the width.
cover{position:relative; padding-top:100%; height:0; overflow:hidden;}
. cover img{position:absolute; top:0; width:100%;}
5, Picture HD
As we all know, Iphone6 Plus is 3 times times the HD figure, it's Devicepixelratio = 3. The introduction to DPR can be viewed in this article "device pixel ratio devicepixelratio simple Introduction"
under iOS8, the Srcset attribute of the IMG has already been supported (currently the mobile side iOS8 support), that is, a picture can be set to 2 URLs, the browser automatically load the corresponding image.
The level of support is as follows:
Yellow indicates that only the old Srcset specification is supported, and green indicates that the new Srcset specification is supported, including the sizes attribute, and the W descriptor. Don't start here, learn more about Google for yourself.
However, the current front-end picture of the implementation of the basic use of lazyload way to achieve. Srcset image loading method in the actual project is still relatively small.
6, the background map of HD
Media query for HD
img Label HD, you can use JS to determine the value of Devicepixelratio to load different sizes of pictures, but for the background map, written in CSS, with JS to judge a little trouble, fortunately, CSS through media query can also judge DPR.
currently the best compatibility of the background map of the implementation of the way, using media query-webkit-min-device-pixel-ratio to make judgments:
- /* Normal display (device pixel ratio is less than or equal to 1) Use 1 time times the figure */
- . css{
- Background-image:url (img_1x.png);
- }
- /* HD display (device pixel ratio greater than or equal to 2) use twice times graph */
- @media only screen and (-webkit-min-device-pixel-ratio:2) {
- . css{
- Background-image:url (img_2x.png);
- }
- }
- /* HD display (device pixel ratio greater than or equal to 3) use 3 times times graph */
- @media only screen and (-webkit-min-device-pixel-ratio:3) {
- . css{
- Background-image:url (img_3x.png);
- }
- }
Copy Code
Further, you can use the tool to generate the corresponding 3x,2x,1x of the picture and CSS, when using the direct reference. Who's got one?
For mobile device-webkit-min-device-pixel-ratio values, you can view the collation of the page: http://bjango.com/articles/min-device-pixel-ratio/
Image-set for HD
Image-set, which is a private property of WebKit and a property of CSS4, is intended to address the image display under the retina screen.
The way of use is also very simple. The pseudo code is as follows:
- . css {
- Background-image:url (1x.png); /* Display without support for Image-set */
- Background:-webkit-image-set (
- URL (1x.png) 1x,/* support for Image-set browser [normal screen] */
- URL (2x.png) 2x,/* support for Image-set's browser [twice times Retina screen] */
- URL (3x.png) 3x/* support for Image-set's browser [3 times times Retina screen] */
- );
- }
Copy Code
the current level of support on the mobile side, Ios7+,android 4.4+ has been supported. If you are only doing ip6+ 's HD adaptation scheme. Image-set is also an implementation scenario.
The general meaning is: Image-set does not need to tell the browser what image to use, but directly provides the image to let the browser choose. This means that the browser can choose to load low-resolution images at low speeds. (PS: A good smart look)
However, compared to the implementation of media query, Image-set only supports the HD of a single image and is not suitable for use in CSS sprites. And compatibility is also a big mishap.
but in general, in the logo area, a single picture icon area, is also a good choice.
7, the image list of adaptive
with regard to adaptation, that is, to make the layout more flexible, in the e-commerce website, the product list is a very common structure.
a smarter way to list is to align the two sides and adjust the spacing adaptively.
You can use the Flexbox layout to achieve the effect of justification, or you can use the text-align:justify approach.
flexbox Layout Way, on the PC side is not suitable, IE9 below are not supported, then the more friendly way can use text-align:justify to achieve, compatible with major mainstream browsers, including IE6.
For details, please visit my previous blog post: "Inline-block + Justify Implementation list Justification"
However, there are some limitations in these 2 layout methods. Is that the number of lists must be rounded up. There is no one can be compatible with an unlimited number of implementation options, if you crossing have a better way to achieve, also welcome the proposed, Exchange.
Summary
Mobile IP6 Adaptation scheme has many, there is no fixed routines and methods, according to the characteristics of their own business, choose some of the methods used in combination.
Iphone6 adaptation of the mobile H5 page