A summary of some basic knowledge points of mobile-H5

Source: Internet
Author: User

After coming to this company, and the previous work has undergone a great change. I used to be a PC-side page. After coming to the present company, the main thing is to do mobile phone page.

Mobile page in my habit of the PC-side page of the people, it should be very simple things. But in the process of getting started, still found that the original mobile side although the DOM structure is much simpler, but need to pay attention to the details, or very many. The reason is very simple, that is, You have to consider the effects of different resolutions on all your phones.

What are the resolutions on the mobile side?

The iphone, for example, has the following resolution

iphone4 640960
Iphone5 640
1136
Iphone61334
Iphone6plus 1242
2208

The Android faction has a variety of resolutions, and is now basically divided into three

720P 7201280
1080P
1920
2 k 1440*2560

This is an illusion, in fact, when doing mobile phone H5, the resolution at all times this look like .... The real resolution they have in the browser is to worry about the dead

The iphone, for example, has the following resolution

iphone4480
Iphone5
568
Iphone6 375667
Iphone6plus 414
736

The Android faction has a variety of resolutions, and is now basically divided into three

720P640
1080P
640
2 k 360*640

See this you are not dizzy ... It turns out that Androbi apples are much stronger ... Oh, that's the way it is.

In fact, we have to do is an adaptive Web page, and, this adaptive range of the minimum width of 320px, that is, your code must be guaranteed to be able to browse the width of 320, can not squeeze deformation

What unit does the mobile end use?

In the PC-side production page, we are accustomed to the unit is px , then is not on the mobile side we also use this unit? I can only say that you can do it. But it's better not to. Because you don't know what will happen in the future.

We have been discussing, and referring to the current international programme, the units used arerem

So, what is rem ? We all know the unit em of dimension relative to the parent. rem so root-em the abbreviation, that is, is the unit of the root relative unit, its relative is html font-size .

Okay, so what's html the font-size default? Yes. That is 16px , all of your dimensions depend on 16px The result of the relative operation.

Knowing this, we began a complex calculation, for example, I want to set a 12px text, what percentage to use. will find that this calculation is really a pit dad, I am in a project in the first phase is to do so. But this calculation is really tiring.

So, we used the workaround, we give the HTML to modify the default font-size . So, we wrote that.

html {font-size: 10px;}

The whole world is quiet, according to this method of calculation, really very convenient, for example, I need 12px I will write 1.2rem on it.

But I quickly thought about the problem and felt like we were taking off our pants and farting.

Let's go back to the beginning, why should we use it rem instead px ? The reason is very simple, we are to make the page not because of the particularity of some equipment px , is to adapt to a variety of equipment.

And we have html added px to the unit, it is not back to the original? That's better to use directly px , we also write less than two characters.

So, is there a solution to the problem, or is it going back to the anti-human calculation?

I quickly switched my mind and changed the code to

html {font-size: 62.5%;}

After the change to a percentage, the problem was quickly resolved. That way, when the browser sets a different default text size, our pages can follow the changes, not the limits.

In the PC-side Chrome browser, the default minimum text is 12px when you set up to use the developer tools to debug, you will find some weird places. Therefore, you need to set the minimum text of the browser to 10 or 6, which is generally set to 6, This solves the problem of debugging on the PC side. There is no such limit on the mobile side (yes, but the default is off)

Mobile HTML5 page meta-settings

This has a special article, can Baidu a bit, here I emphasize a point, need to add the following code.

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

Some people, including some well-known front-end frameworks, add such a piece of code to the CSS:

* {box-sizing:border-box;}

I personally strongly do not recommend this. Because it destroys the box model, and even with this, it is convenient for you to write code, but when you introduce some external resources, you will find yourself morphing into a rhythm that you simply cannot tune. The reason is simple, and this code changes the box model of all the elements of the page.

Once because of this code, let our front-end people to troubleshoot a problem for two days, and can not be resolved. When I took over the project, the question was given to me, and I was baffled at first. But then, after careful examination, it was found that this code is a curse.

If we need to write a box, the default is hundred percent wide, how do we do it?

The answer is, we do nothing, then it is hundred percent.

When doing the mobile page, try not to set the width, let it naturally occupy a line, this time we can set the outer fill and fill as needed.

Of course, not all things can be done this way, always encounter multi-column layout of the time

This needs to be noted, do not give the rem width of such units, but to give 50% or 33.3% the width.

After setting this width, do not set padding and padding for this element. If necessary, write an element inside this element and add the fill you need for the element.

In addition, in the layout, to minimize the use of floating layout, appropriate in the details of the location layout, and do a good job of hiding overflow and so on. To prevent problems that may occur with floating layouts. For positioning problems, you can refer to my other blog post Css for detailed interpretation of positioning attributes position and parameters;

handling of borders

In the PC-side Web page production, we generally use the border property border: 1px solid #ddd; but on the mobile side, you need to pay special attention, because the width of the border is calculated in the box model, so if you use inadvertently, may cause your layout of the dilemma.

Therefore, we need some other parameters to set the border, the general rectangle element, we can use outline: 1px solid #ddd; to make the border, this property is not calculated in the box model.

In addition, when you try to make a list of two columns of layout, using this parameter, you will find that the border between the two elements appears to be two pixels. Yes, you're not mistaken, it's really two pixels.

How do you solve this problem? In general, you only need to add a background to the element to solve this problem. The background of the element behind it obscures the border of the previous element. outline

This is a very practical technique.

But what if the element is not rectangular?

In fact, very good solution. Because, in CSS, not only outline can simulate the border, there is a property, but also can simulate the border, that is the CSS3 of the box-shadow outer glow properties.

For example, I want to add a 1px solid border to the element, and you can't do that, you box-shadow:0 0 1px #ddd; 'll find that the frame you're simulating is a little hollow.

The correct box-shadow:0 0 0 1px #ddd; way to write is to see this is very strange? Go to the W3school and see it.

Of course, both of these practices are flawed, such as I would like to set a left-hand border line, and now and then the whole has a frame to do?

There is still a way, we can use pseudo-elements to achieve simulation, :before and :after How to implement, here do not do a demonstration.

Here to explain, there are many online 0.5px border implementation tutorials, the advantage is that you can make a relatively thin line, so that our H5 look more like the original app. My personal advice is, don't try, because it's troublesome, and compatibility is problematic, Not worth the candle. So what's the right thing to do? It's simple--tell your designer to try not to design lines-_-| | | Anyway, that's what I did with the design of our team.

using jquery or Zepot?

Why use Zepot?

There is only one reason, it is small. Is there any other benefit? not found.

My personal advice is to use the jquery2.x version, the benefits are as follows

    1. With a large number of plugins to use
    2. It's much more powerful than zepot.
    3. Greater efficiency than zepot
    4. Used to jquery.

For more information, see why we gave up Zepto

Summary

There is a big difference between making a Web page on the mobile side and on the PC side. Many of the experiences on the mobile side are not working, and may also be counterproductive. When we move from the front end of the PC to the front-end engineers, we need to be patient to solve some problems, a pit to tread, to continue to progress.

Fungleo original, reproduced please keep the version of the Declaration, and the starting address http://blog.csdn.net/fungleo/article/details/50811739

A summary of some basic knowledge points of mobile-H5

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.