Talk about the adaptation of a mobile page

Source: Internet
Author: User

Flexible-bearer Mission

Lexible to today also has a few years of history, save a lot of students for the H5 page layout of the adaptation problem. And this package is relatively a relatively mature plan. Simple recall, the original in order to allow the page to better fit a variety of different terminals, through the hack means to change the value of the device according to the DPR value of the corresponding changes in the label viewport values:

<!-- dpr = 1--> <meta name="viewport" content="initial-scale=scale,maximum-scale=scale,minimum-scale=scale,user-scalable=no"><!-- dpr = 2--> <meta name="viewport" content="initial-scale=0.5,maximum-scale=0.5,minimum-scale=0.5,user-scalable=no"> <!-- dpr = 3--> <meta name="viewport" content="initial-scale=0.3333333333,maximum-scale=0.3333333333,minimum-scale=0.3333333333,user-scalable=no">

In order to make the page zoom effect, but also in disguise to achieve the function of the page. And its main idea has three points:

    • Modify viewport to implement 1px line according to the value of DPR
    • Modifies the font-size of the HTML according to the value of DPR, thus using REM to achieve equal-to-scale
    • Simulating VW characteristics with REM using the Hack method

About the flexible program to achieve adaptation, interested students can read the "use of flexible to achieve the H5 page of the terminal adaptation" article. Although flexible solves the adaptation terminal many problems, but it is not omnipotent, is not the best, he still has some problems, such as the reference of the IFRAME, sometimes we bury ourselves in. For some of these shortcomings, some students have been related to the transformation, in the online search can find relevant solutions.

So the times are changing, the front-end technology is constantly changing, ask:flexible or the best solution? Is there any need for flexible? recently has been exploring this aspect, here first told everyone flexible has completed his own historical mission, we can put down flexible, embrace new changes . Next, I will share some of my recent discussion of the new adaptation scheme, perhaps a lot of team students have started to use, if there is something wrong, I hope to get the ladies to correct; If you have a better plan, hope to share the discussion together.

Serve first, then chit chat when.

First two-dimensional code:

You can scan the QR code above using the app, Youku app, browser, UC Browser, QQ browser, Safari browser and Chrome browser, and you'll see the effect:

iphone series effects

Some Android effects

Note: If the QR code above does not have any effect, you can click here to open the online page and regenerate the QR code number that your device can recognize.

Above the demo, tested the TOP30 model. Not supported at the moment:

TOP30 the model is not in the list, you will see the effect as shown. As for the dare to use, it depends on the kiss. Must be the first to eat crab people need a certain courage!

Adaptation Program

The current support and effectiveness of the program are described in the previous article. Also pulled a lot of nonsense, then go to the point.

On the mobile side of the layout, we need to face two of the most important issues:

    • Adaptation issues under each terminal
    • Detail handling of Retina screen

Different terminals, we face the screen resolution, DPR, 1px, 2x graphs and a series of problems. Then this layout solution is also targeted to solve these problems, but to solve these problems is not to use hack means to deal with, but directly using the native CSS technology to deal with.

Adapter Terminal

The primary solution is the adaptation terminal. Recall that the previous flexible scheme used JavaScript to simulate VW's features, so so far, VW has been supported by many browsers, which means that VW units can be directly considered for use in our adaptation layouts.

As we all know, VW is based on the length unit of the Viewport window, where the window (Viewport) refers to the area of the browser visualization, and this viewable area is window.innerWidth/window.innerHeight的大小 . With a simple hint:

Because viewport involves a lot of knowledge, to introduce clear knowledge of this area, all need a few articles to elaborate. @PPK Great God has two articles detailing this knowledge. Chinese can be read here.

There are four related units in CSS Values and Units Module level 3 and viewport, respectively, VW, VH, Vmin, and Vmax.

    • VW: Viewport's width shorthand, 1vw equals window.innerwidth 1%
    • VH: Similar to VW, is a shorthand for viewport's height, 1VH equals window.innerheihgt 1%
    • The value of Vmin:vmin is the smaller value in the current VW and VH
    • The value of Vmax:vmax is the larger value in the current VW and VH

Vmin and Vmax are calculated based on the dimension value that is larger in length in the Viewport, and if window.innerHeight > window.innerWidth the vmin takes 1% window.innerWidth , the Vmax takes 1% of the window.innerHeight calculation.

Or a picture to indicate, a picture is better than words:

So in this scenario, you boldly use VW to replace the REM scaling scheme in the previous flexible. First come back to our actual business. Currently out of visual design, we are all using 750px width, from the above principle, then 100vw = 750px, that is, 1VW = 7.5px. Then we can change the PX value directly into the corresponding VW value according to the design chart. See here, many students began to feel the crash, but also calculate, can be simple, can be a little bit simpler, in fact, we can use postcss plug-in Postcss-px-to-viewport, let us directly in the code to write PX, Like what:

[w-369]{     width: 369px; } [w-369] h2 span {   background: #FF5000;   color: #fff;   display: inline-block;   border-radius: 4px;   font-size: 20px;   text-shadow: 0 2px 2px #FF5000;   padding: 2px 5px;   margin-right: 5px; }

Postcss After compiling is what we need with the VW code:

[w-369] {     width: 49.2vw; } [w-369] h2 span {     background: #ff5000;     color: #fff;     display: inline-block;     border-radius: .53333vw;     text-shadow: 0 0.26667vw 0.26667vw #ff5000;     padding: .26667vw .66667vw; } [w-369] h2 span, [w-369] I {     font-size: 2.66667vw;     margin-right: .66667vw; }

In actual use, you can configure the plug-in for the relevant parameters:

"postcss-px-to-viewport": {     viewportWidth: 750,     viewportHeight: 1334,     unitPrecision: 5,     viewportUnit: ‘vw‘,     selectorBlackList: [],     minPixelValue: 1,     mediaQuery: false }

Assuming that your design is not 750px but 1125px, then you can modify the value of the vewportwidth. There is a detailed description of the plugin, which can be read in its official usage documentation.

The above solves the conversion calculation of PX to VW. So where can we use VW to fit our page? Based on the relevant tests:

    • Containers can be adapted to use VW
    • The text can be adapted to use a VW
    • Borders, fillets, and shadows larger than 1px can be used by VW
    • internal and external distances, you can use VW

There is another detail that needs to be put forward, for example, we have a design like this:

If we use directly:

[w-188-246] { width: 188px; } [w-187-246]{ width: 187px }

The final effect causes the [w-187-246] container's height to be less than the height of the [w-188-246] container. At this point we need to consider the scale of the container's aspect ratio. This is a lot of programs, but I still recommend the tool to deal with, here recommended @ A trace sister wrote a POSTCSS plugin Postcss-aspect-ratio-mini. This plugin is very simple to use, do not need to do any configuration, you just need to install a local on the OK. Use the following:

[aspectratio] {    position: relative;}[aspectratio]::before {    content: ‘‘;    display: block;    width: 1px;    margin-left: -1px;    height: 0;}[aspectratio-content] {    position: absolute;    top: 0;    left: 0;    right: 0;    bottom: 0;    width: 100%;    height: 100%;}[aspectratio][aspect-ratio="188/246"]{    aspect-ratio: ‘188:246‘;}

Compile it:

[aspectratio][aspect-ratio="188/246"]:before {    padding-top: 130.85106382978725%;}

This allows for a perfect aspect ratio effect. There is a principle here not to do too much elaboration, interested in the words can read earlier organized articles:

    • Several schemes for realizing aspect ratio of CSS
    • Container aspect ratio
    • How aspect ratios are implemented in the Web
    • achieve precise fluid typesetting principles

The current adoption of the POSTCSS plugin is only a transitional phase, in the future we can use the Aspect-ratio property directly in the CSS to achieve the aspect ratio.

Solving 1px scenarios

As mentioned earlier, for 1px is not recommended to convert it to the corresponding VW unit, but under the retina, we always have to face how to solve the problem of 1px. There are a variety of solutions for 1px in the "Talk about 1px solution for Retina" article. In this case, the individual recommends another solution to 1px. Still use POSTCSS plug-in, solve 1px can use Postcss-write-svg.

With Postcss-write-svg you can handle it in two ways, either Border-image or Background-image. Like what:

@svg 1px-border {    height: 2px;    @rect {        fill: var(--color, black);        width: 100%;        height: 50%;    }}.example {    border: 1px solid transparent;    border-image: svg(1px-border param(--color #00b1ff)) 2 2 stretch;}

This postcss will automatically help you compile the CSS:

.example {    border: 1px solid transparent;    border-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns=‘http://www.w3.org/2000/svg‘ height=‘2px‘%3E%3Crect fill=‘%2300b1ff‘ width=‘100%25‘ height=‘50%25‘/%3E%3C/svg%3E") 2 2 stretch;}

The use of POSTCSS plug-in is not more than we modify the image to come easy and convenient.

The above demonstrates the use of the border-image method, in addition to be used background-image to achieve. Like what:

@svg square {    @rect {        fill: var(--color, black);        width: 100%;        height: 100%;    }}#example {    background: white svg(square param(--color #00b1ff));}

Compiled is:

#example {    background: white url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns=‘http://www.w3.org/2000/svg‘%3E%3Crect fill=‘%2300b1ff‘ width=‘100%25‘ height=‘100%25‘/%3E%3C/svg%3E");}

This program is easy to use and is what I need. At present test down, basically can achieve I need of demand. But be sure to remember to add:

<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />

Described above is the technical point used in this adaptation scheme, a simple summary:

    • Use VW to make the page fit and convert PX to VW via the Postcss plugin postcss-px-to-viewport. The advantage is that we do not need to do any calculations when we are in the code, you only need to write the PX units according to the design diagram.
    • In order to achieve a better aspect ratio, especially for the IMG, vedio and IFRAME elements, through the POSTCSS plug-in Postcss-aspect-ratio-mini to achieve, in the actual use, only need to write the corresponding width and height in the
    • To solve the 1px problem, use the Postcss plugin postcss-write-svg to automatically generate Border-image or background-image images.
Viewport deficiencies

Using VW for adaptive processing is not only a benefit without any drawbacks. There are some flaws in some of the details. For example, when the container uses the VW unit, the margin uses PX units, it is easy to cause the overall width of more than 100VW, thus affecting the layout effect. For similar phenomena, we can use the relevant technology to circumvent.

For example, change the margin to padding, and cooperate with box-sizing. But this is not the best solution, as the future browser or application of its own webview to the Calc () function, when encountered when the VW and PX mixed use, can be combined with the Calc () function together, so it can be solved perfectly.

Another point, PX converted to VW units, how much will still exist a certain pixel difference, after all, many times can not be fully divisible.

So far, I have found two deficiencies. Perhaps in the later use, you will encounter some other non-human pits. It is also true that, no matter what the plan is, the more you step on the hole, the more powerful the program is. Want to like this adaptation program of students and I step on the pit, let it more perfect.

How to determine if your application supports

Although the example of this article has been tested in many ways. But many students still worry about whether their app supports the program, but not daring to try or use it. There is no need to be so worried, you can take your own device, or the app scans the QR code below:

When the page runs through the test, find the corresponding values and units list items:

If the VW bar is green on behalf of your device or app to support the scenario, the reverse is not supported. In addition, you can often focus on css3test related updates, which will update the test code according to the relevant specifications, so that you can quickly grasp which properties can be used boldly.

Summarize

The adaptation of the H5 page is always painful, in fact the layout of the page is always a pain in the egg. But technology is constantly innovating, and we can try to apply these new features to real projects as we keep our focus on new technologies, so that our solutions to problems become more and more perfect.

To write this article, although there are still one or two models do not support VW, but does not affect our use. Only by constantly trying, will there be progress. Here, I hope you will be bold to try and make the program more perfect together. If you have a better idea, or if you step into any pit, please share it with me in the comments below, or email me to discuss it.

Original

For more information please see my blog new address https://www.cssge.com/

Talk about the adaptation of a mobile page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.