Dynamic font-size on the mobile side

Source: Internet
Author: User

/**
* Created by Shimin on 2017/8/18.
*/
Calculate DPR
!Function (Win, Lib) {
VarTimer
Doc = win.Document
Docelem =Doc.DocumentElement,

Vpmeta =Doc.Queryselector (' Meta[name= ' viewport "]),
Flexmeta =Doc.Queryselector (' meta[name= ' flexible "]),

DPR =0,
Scale =0,

Flexible = lib.Flexible | | (Lib.Flexible = {});

Set the viewport meta
if (Vpmeta) {

Console.Warn"The scaling is set according to the existing META tag");
VarInitial =Vpmeta.GetAttribute ("Content").Match/initial\-scale= ([\d\.] +)/);

if (Initial) {
Scale =Parsefloat (initial[1]);Initialscale that have been set
DPR =parseint (1/scale);Device pixel ratio devicepixelratio
}

}
Set the flexible Meta
else if (Flexmeta) {
VarFlexmetacontent =Flexmeta.GetAttribute ("Content");
if (Flexmetacontent) {

VarInitial =Flexmetacontent.Match/initial\-dpr= ([\d\.] +)/),
Maximum =Flexmetacontent.Match/maximum\-dpr= ([\d\.] +)/);

if (Initial) {
DPR =Parsefloat (initial[1]);
Scale =Parsefloat ((1/DPR).ToFixed (2));
}

if (Maximum) {
DPR =Parsefloat (maximum[1]);
Scale =Parsefloat ((1/DPR).ToFixed (2));
}
}
}

Viewport or flexible
Meta is not set
if (!DPR &&!Scale) {
QST
What's the use of the first sentence here?
Have a hair relationship with Android?
VarU = (win.Navigator.AppVersion.Match/ANDROID/GI), win.Navigator.AppVersion.Match/IPHONE/GI)),
_DPR = Win.devicepixelratio;

So it seems to be that all Android devices are set to 1.
DPR =U? ( (_DPR >=3 && (!DPR | |DPR >=3))
?3
: (_DPR >=2 && (!DPR | |DPR >=2))
?2
:1
)
:1;

Scale =1/Dpr
}

Docelem.SetAttribute ("DATA-DPR",DPR);

Insert Viewport meta
if (!Vpmeta) {
Vpmeta =Doc.CreateElement ("Meta");

Vpmeta.SetAttribute ("Name","Viewport");
Vpmeta.SetAttribute ("Content",
"Initial-scale=" +Scale +", maximum-scale=" +Scale +", minimum-scale=" +Scale +", User-scalable=no");

if (Docelem.Firstelementchild) {
Docelem.Firstelementchild.AppendChild (Vpmeta)
}else {
Vardiv =Doc.CreateElement ("Div");
Div.AppendChild (Vpmeta);
Doc.WriteDiv.InnerHTML);
}
}

functionSetfontsize () {
VarWinwidth =Docelem.Getboundingclientrect ().Width

if (WINWIDTH/DPR > 540) {(Winwidth = 540 * DPR);}//root node fontSize depends on the width of var basesize = WINWIDTH/10; DocElem.style.fontSize = basesize + "px"; Flexible.rem = Win.rem = Basesize; }//Reset Win.addeventlistener ("Resize", function () {cleartimeout (timer), timer = SetTimeout (setfontsize, +) when adjusting the window, FAL SE); This paragraph is my own Add//Orientationchange also need to re-calculate Win.addeventlistener ("Orientationchange", function () {cleartimeout (timer); Timer = SetTimeout (setfontsize, 300); }, False); Pageshow//Keyword: Backward cache related Win.addeventlistener ("Pageshow", function (e) {if (e.persisted) {cleartimeout (timer); Tim ER = setTimeout (setfontsize, 300); }}, False); Sets the base font if ("complete" = = = doc.readystate) {doc.body.style.fontSize = * DPR + "px";} else {Doc.addeventlistener ("D Omcontentloaded ", function () {doc.body.style.fontSize = * DPR +" px ";}, False); } setfontsize (); FLEXIBLE.DPR = WIN.DPR = DPR; Flexible.refreshrem = setfontsize; flexible.rem2px = function (d) {var c = parsefloat (d) * this.reM if ("string" = = typeof D && d.match (/rem$/)) {c + = "px";} return C; }; Flexible.px2rem = function (d) {var c = parsefloat (d)/this.rem; if ("string" = = typeof D && d.match (/px$/)) {C + = "REM"; } return C; }} (window, Window.lib | | (Window.lib = {})); /Calculation viewport! (Function (doc, win) {var docele = doc.documentelement, evt = "onorientationchange" in window? "Orientationchange": "Resize", fn = function () {var width = docele.clientwidth; width && (docEle.style.fontSize = * (width/320) + "px"); }; Win.addeventlistener (EVT, FN, false); Doc.addeventlistener ("domcontentloaded", FN, false);} (document, window)); /media query settings [email protected] screen and (min-width:320px) {html {font-size:14px;}} @media screen and (min-width:360px) {html {font-size:16px;}} @media screen and (min-width:400px) {html {font-size:18px;}} @media screen and (min-width:440px) {html {font-size:20px;}} @media screen and (min-width:480px) {html {font-size:22px;}} @media screenand (min-width:640px) {html {font-size:28px;}}
< Span class= "Hljs-number" > < Span class= "Hljs-attribute" >< Span class= "Hljs-selector-tag" > < Span class= "Hljs-number" >< Span class= "Hljs-attribute" >&NBSP;         

Dynamic font-size on the mobile side

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.