How to make mobile web page content adaptive

php中世界最好的语言
Release: 2018-01-23 10:34:01
Original
2865 people have browsed it

This time I will show you how to make the web content on the mobile terminal adaptive. What are the precautions for adapting the web content on the mobile terminal? Here are practical cases, let’s take a look.

Finally finished the project at hand, the missing person is back! In the process of working on the project, I encountered many points worth thinking about, so let’s quickly explain them. The first problem encountered is the problem of web page size adaptation.

Currently the more commonly used methods are:

• First, let the page size fill the screen without overflowing. Just add viewport (as shown below) in the html tag. The parameters respectively represent: page width = screen width. The maximum and minimum scaling ratios are both 1, and users are not allowed to zoom.

<meta name="viewport" content="width=device-width,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
Copy after login

• Percent adaptive: Convert the length unit to a percentage, so that the length and width of the element will change accordingly under different widths.

Advantages: Seamless connection between widths, and relatively easy to operate.

Disadvantages: The font size needs another set of adaptive methods to adjust; when the screen width is greater than 700px, the elements will be larger according to the percentage, which will be more troublesome to adjust at this time.

•rem, em adaptive: Use the media query method to determine the fontsize of or under different screen widths. Then use rem and em instead of px as the unit to achieve adaptation.

Advantages: It can be set according to different screen widths, which can perfectly solve the proportion problem when the screen is too large mentioned above. There is no problem with the font size either.

Disadvantages: Set according to the width interval, seamless transformation cannot be achieved.

-------------------------------------------------- ------------------------------------

These compatible methods each have their own advantages and disadvantages. None of them are perfect. How can we combine the advantages while avoiding the disadvantages?

When referring to Taobao's adaptive method, I accidentally discovered that the fontsize of the page will be automatically adjusted according to the width of the screen, and the quotient of the screen width and the set font size is certain.

So I guess it is to use JS to obtain the screen width, then reduce it according to a fixed ratio and use it as the unit length of rem to achieve self-adaptation.

Isn’t this a solution with all the advantages! ? Please allow me to get excited (☆_☆)

--------------------------------- --------------------------------------------------

JS code is very simple to write, and it perfectly solves the problem of being unable to achieve seamless connection when using rem settings.

But the problem arose after the mobile terminal test. The mobile terminal safari executed the JS with lightning speed before the HTML was loaded. The JS was read before the width of the page was set according to the viewport. The wrong width causes the element to become twice as big as 0^0. SetTimeout() needs to be used to solve the problem.

-------------------------------------------------- ------------------------------------

Final code

Zepto(function($){   
    var win = window,   
        doc = document;   
  
    function setFontSize() {   
        var winWidth =  $(window).width();   
        // 640宽度以上进行限制   
        var size = (winWidth / 640) * 100;   
        doc.documentElement.style.fontSize = (size < 100 ? size : 100) + &#39;px&#39; ;   
    };   
       
    //防止在html未加载完毕时执行,保证获取正确的页宽   
    setTimeout(function(){   
        // 初始化   
        setFontSize();   
           
    }, 200);   
    
});
Copy after login

Finally, a pitfall discovered during the adaptive process of using rem - when the html is set to a larger fontsize, additional inline elements margin and padding will appear in the block element. value, the solution is to set the fontsize of the outer block element to 0.

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How to deal with content overflow in the table

How to obtain the dynamic remaining word count of textarea

htmlImportant knowledge points you must know about PHP

The above is the detailed content of How to make mobile web page content adaptive. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template