Mobile development adaptation of 2 scenarios summary
Solutions for mobile adaptation-
One: The first scenario is that all units are adapted with REM;
First, set the size of the HTML font-size on the page, as in the following settings in my project:
HTML {font-size:100px;} @media (min-width:320px) {html {font-size:100px;}} @media (min-width:360px) {html {font-size:112. 5px;}} @media (min-width:400px) {html {font-size:125px;}} @media (min-width:640px) {html {font-size:200px;}}
HTML default font size is 100px, is for 320px to set, then in the 360px,400px,640px need to scale;
The calculation formula is: 320px/100 = 360/x; So x = (360*100/320) px; The other proportional scaling is also the meaning;
So the designer gave us the design manuscript is the default 640px design, so we can know font-size:200px; So we can set the magnification to 200, and the PX to REM,
As long as you know how many pixels of the design, the height of the slice is 500px; we need to convert REM, the formula is as follows: REM = (500/200) rem; Other attributes, such as Font-size,margin, padding, and so on, can be converted to REM by such calculations, and we use a simple less precompiled language for computing.
For example, to retain 2 decimal places, you can write as follows XX = round (500rem/200,2);
II: The second option is: pure shredding using PX as the unit; (CSS3 Media query as follows:)
/*for 400-450 according to 400px to calculate*/@media (min-width:400px) and (max-width:450px) {/*640/400 = 1.6*/@multiple:1.6;. Mixin (@multiple);/*Below you can write some of the private CSS properties covered out*/}/*for 360-399 according to 360px to calculate*/@media (min-width:360px) and (max-width:399px) {/*640/360 = 1.78*/@multiple:1.78;. Mixin (@multiple);/*Below you can write some of the private CSS properties covered out*/}/*for 320-359 according to 320px to calculate*/@media (min-width:320px) and (max-width:359px) {/*640/320 = 2*/@multiple:2;. Mixin (@multiple);/*Below you can write some of the private CSS properties covered out*/}
/* The following mixin function uses the less precompiled language, which requires the use of the round method to automatically calculate each value */
. Mixin (@multiple) {
For example, if the height is 500px, then the formula is as follows:
Height:round (500px/@multiple, 2);
The other property values below are also a meaning, how many pixels the design manuscript gives, the number of pixels to write, and then the calculation can be;
}
Other properties that do not need to be computed can be written out of the function, so there is a drawback that you do not write outside and are written in the Mixin function, then each call to the Mixin function will generate
A copy of the code; If I do not write in it, if I do not need to calculate the style are written in the outside, then if many of the properties of the class need to calculate, there is no need to calculate, then will be
A CSS style is divided into 2 parts, which is not very friendly for code maintenance, so if you can use REM in your project, it is recommended to use REM as a unit to adapt to mobile development.
However, both of these options can be adapted to match the results ~
The demo that uses REM as a unit to fit your phone is as follows:
REM Adaptation Scheme reference
If the GIT code already has the demo, we can clone it from git:
such as: Git clone https://github.com/tugenhua0707/remAdapter.git
Then enter the directory using the command, and the local server can run the command gulp server-d; a server will be opened locally on a certain time, such as: Http://localhost:3000/
However, this is not accessible to the page, because I gulpfile.js the code all packaged into the build folder: such as the path of the project after the package:
Build
Css
Html
Images
Js
So we can access the index.html in http://localhost:3000/build/html/, and we can open the index.html under the HTML under the SRC source file, and we can see the corresponding CSS
<link type= "Text/css" rel= "stylesheet" href= "@@@[email protected]@@/css/index.css"/>
Image images are introduced using paths such as @@@[email protected]@@/images/heart.png, and the introduction of JS files is also introduced by @@@[email protected]@@ 作为 a prefix to the path. The advantage of using variables is that it can be judged by whether it is an online environment or a development environment, and if it is a development environment, @@@[email protected]@@ 在 automatically replaced with Http://localhost:3000/build when packaged, for the online environment You can change the response path according to the needs of your project, which can be seen within the gulpfile.js and replaced with the Gulp-replace plugin;
At the same time the page using the Browser-sync plug-in, real-time monitoring html,css,js dynamic change, just to press Ctrl+s save operation, the page will automatically refresh;
Also support JS modular load dependent files, using the Browserify plugin for JS modular loading;
A summary of mobile development adaptation