Home  >  Article  >  Web Front-end  >  Implementation method of mobile web page size adaptation

Implementation method of mobile web page size adaptation

高洛峰
高洛峰Original
2017-02-22 11:16:101793browse

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 I will quickly explain them. The first problem encountered is the problem of web page size adaptation.

Currently the more commonly used methods are:

• First, make the page size fill the screen without overflowing. You only need to add the viewport (as shown below) in the html93f0f5c25f18dab9d176bd4f6de5d30e 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 in or out.

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

• 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 media query method to determine the fontsize of 100db36a723c770d327fc0aef2ce13b1 or 6c04bd5ca3fcae76e30b72ad730ca86d 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 not being able to achieve seamless connection when using rem settings.

But the problem arose after testing on the mobile terminal. The mobile 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);   
    
});

Finally, I will add a pitfall discovered during the adaptive process of using rem - when the html is set to a larger fontsize, the margin and padding of the inline elements within the block element will have additional values. The solution is to The fontsize of the outer wrapped block element is set to 0.

The above article on how to implement self-adaptive size of mobile web pages is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more related articles on how to implement adaptive web page size on mobile terminals, please pay attention to the PHP Chinese website!

Statement:
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