How to use mobile end rem.js

Source: Internet
Author: User

The following code one is I based on the use of REM experience, I wrote a rem.js, found very useful, can be applied to all mobile H5 page Adaptive Requirements:

code one: Window.onload=function () {/*720 on behalf of the designer to give the width of the design manuscript, your design draft is how much, write how many, 100 represents the conversion ratio, here write 100 is to calculate later, for example, you measure a width is 100px, can write as 1rem, and 1px=0.01rem and so on*/Getrem (720, -)};window.onresize=function () {Getrem (720, -)};function Getrem (pwidth,prem) {varhtml = document.getElementsByTagName ("HTML")[0]; varOwidth = Document.body.clientWidth | |Document.documentElement.clientWidth; Html.style.fontSize= Owidth/pwidth*prem +"px";}

The following code two, I saw on the Xiaomi Web mobile H5 page Adaptive Code, the effect is the same as mine, you can also use:

code Two: The writing of Xiaomi's official website!function (n) {varE=n.document, T=e.documentelement, I=720, D=i/ -, O="Orientationchange"inchN?"Orientationchange":"Resize", a=function () {varn=t.clientwidth| | the;n>720&& (n=720); T.style.fontsize=n/d+"px"         }; E.addeventlistener&& (N.addeventlistener (o,a,!1), E.addeventlistener ("domcontentloaded"A!1) } (window);

Look at these two functions, put the code into JS, the rule is, call the function, put two parameters, the first parameter, is the width of the design draft, the second parameter is PX and REM conversion ratio, usually write 100 (because good); Of course, to the JS code is best encapsulated in a separate JS file, And put in all the CSS files before the introduction of loading.
The actual application is, #box1 {height:100px;} And call REM is #box1{Height:1rem;} And so on 100px = 1rem. 1px = 0.01rem. In the page, the size of the padding, margin, width, height and so on, can use REM to write units, so when the different resolution of the mobile phone in the same page, the effect is almost the same.

<!doctype html>"en">"UTF-8"> <title>Document</title> <meta name="Viewport"Content="Width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/> <link rel="stylesheet"href=".. /css/reset-min.css"/> <script>window.onload=function () {Getrem (720, -)        }; Window.onresize=function () {Getrem (720, -)        }; function Getrem (pwidth,prem) {varhtml = document.getElementsByTagName ("HTML")[0]; varOwidth = Document.body.clientWidth | |Document.documentElement.clientWidth; Html.style.fontSize= Owidth/pwidth*prem +"px"; }        /*//Xiaomi's official website!function (n) {var e=n.document, t=e.documentelement, i=720, d=i/100, o= "Orientationchange" in n? " Orientationchange ":" Resize ", a=function () {var n=t.clientwidth| |                     320;n>720&& (n=720);                 t.style.fontsize=n/d+ "px"}; e.addeventlistener&& (N.addeventlistener (o,a,!1), E.addeventlistener ("domcontentloaded", a,!1)} (window); */</script> <style>. Wrap{position:absolute;top:0; Left:0; Bottom:0; Right:0; background: #fefefe;} . Title{width: -%;height:0.98rem;line-height:0.98rem;color: #fff; background: #e02222; Text-align:center;font-size:0. 32rem;} </style>class="Wrap"> <divclass="title"> Home </div> </div></body>

Here the demo only on the font size, height, line height did a rem conversion, the other did not do, just to show you a effect.

How to use mobile end rem.js

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.