JavaScript controls REM Font size

Source: Internet
Author: User

Abstract: In the Writing Response H5 page, I often use REM font, in order to compatible with multiple resolutions of the device, need to write multiple @media tags, too cumbersome and not sophisticated, especially the mobile page often can't hit the effect I want, and then use JS instead of the CSS @media, the following is the code. (The following scenario is in 750px design)

Scenario one, the disadvantage: Browser use no problem, Android and iOS devices embedded in the time, will be affected by the system font settings, the principle is when the system set large font, will change the size of the HTML font-size, causing the page bug.

(function(doc,Win{    varDocel= Doc.documentelement,Resizeevt= ' Orientationchange ' inchWindow? ' Orientationchange ':' Resize ',Recalc= function(){            varClientWidth= Docel.clientwidth;            if(!clientwidth)return;            if(clientwidth>= -){                Docel.style.fontSize = ' 100px ';            }Else{                Docel.style.fontSize =  - *(ClientWidth/ -)+ ' px!important ';            }        };    if(!Doc.AddEventListener)return;    win.AddEventListener(resizeevt,Recalc, false);    Doc.AddEventListener(' domcontentloaded ',Recalc, false);}) (document,Window;

Scenario two, reset the system settings font, resolved the bug, no other problems found

function htmlfontsize(){     varH= Math.Max(Document.documentelement.clientheight, window.Innerheight || 0);     varW= Math.Max(Document.documentelement.clientwidth, window.innerwidth || 0);     varWidth=W>H?H:w;Width=Width>  - ?  -: Width;     varFz= ~~(width*100000/37.5)/20000;     Document.getElementsByTagName("HTML")[0].style.Csstext = ' font-size: ' +Fz+"px";     varRealfz= ~~(+window.getComputedStyle(Document.getElementsByTagName("HTML")[0]).fontSize.Replace(' px ',"')*10000)/10000;       if(FZ!==REALFZ){         Document.getElementsByTagName("HTML")[0].style.Csstext = ' font-size: ' +Fz*(FZ/REALFZ)+"px";     } }

Outside, WebView Plus this setting, you can block the system settings of the font effect, so do not use JS to judge.

webview.getSettings().setTextZoom(100);

Thanks to the open source, some of the text originated from the network, if there is infringement, please contact delete
Zhou Mingjie

JavaScript controls REM Font size

Related Article

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.