REM Turn PX plug-in for mobile phones

Source: Internet
Author: User

/**
* Created by Zengxaingrui on 2016/10/26.
*/
(Function (Win, Lib) {
var doc = win.document;
var docel = doc.documentelement;
var Metael = doc.queryselector (' meta[name= "viewport"] ");
var flexibleel = doc.queryselector (' meta[name= "flexible"] ");
var DPR = 0;
var scale = 0;
var Tid;
var flexible = Lib.flexible | | (lib.flexible = {});

if (Metael) {
/*console.warn (' will set the zoom ratio according to the existing META tag '); */
var match = Metael.getattribute (' content '). Match (/initial\-scale= ([\d\.] +)/);
if (match) {
Scale = parsefloat (match[1]);
DPR = parseint (1/scale);
}
} else if (Flexibleel) {
var content = Flexibleel.getattribute (' content ');
if (content) {
var INITIALDPR = Content.match (/initial\-dpr= ([\d\.] +)/);
var MAXIMUMDPR = Content.match (/maximum\-dpr= ([\d\.] +)/);
if (INITIALDPR) {
DPR = parsefloat (initialdpr[1]);
Scale = parsefloat ((1/DPR). toFixed (2));
}
if (MAXIMUMDPR) {
DPR = parsefloat (maximumdpr[1]);
Scale = parsefloat ((1/DPR). toFixed (2));
}
}
}

if (!DPR &&!scale) {
var isandroid = Win.navigator.appVersion.match (/ANDROID/GI);
var isiphone = Win.navigator.appVersion.match (/IPHONE/GI);
var isipad = Win.navigator.appVersion.match (/IPAD/GI);
var devicepixelratio = Win.devicepixelratio;
if (Isiphone) {
iOS, for 2 and 3 screen, with twice times the scheme, the remaining 1 time times the scheme
if (Devicepixelratio >= 3 && (!DPR | | DPR >= 3)) {
DPR = 3;
} else if (Devicepixelratio >= 2 && (!DPR | | DPR >= 2)) {
DPR = 2;
} else {
DPR = 1;
}
} else {
Other devices, still using 1 time times the scheme
DPR = 1;
}
scale = 1/DPR;
}

Docel.setattribute (' DATA-DPR ', DPR);
if (!metael) {
Metael = doc.createelement (' meta ');
Metael.setattribute (' name ', ' viewport ');
Metael.setattribute (' content ', ' initial-scale= ' + scale + ', maximum-scale= ' + scale + ', minimum-scale= ' + scale + ', use R-scalable=no ');
if (docel.firstelementchild) {
DocEl.firstElementChild.appendChild (Metael);
} else {
var wrap = doc.createelement (' div ');
Wrap.appendchild (Metael);
Doc.write (wrap.innerhtml);
}
}
function IsPC ()
{
var useragentinfo = navigator.useragent;
var Agents = new Array ("Android", "IPhone", "SymbianOS", "Windows Phone", "IPad", "IPod");
var flag = true;
for (var v = 0; v < agents.length; v++) {
if (Useragentinfo.indexof (Agents[v]) > 0) {flag = false; break;}
}
return flag;
}
function Refreshrem () {
var width = docel.getboundingclientrect (). width;
if (IsPC () && width < 2047) {
width = width > 540? 540:width;
}

var rem = width/7.5;
docEl.style.fontSize = rem + ' px ';
Flexible.rem = Win.rem = REM;
}

Win.addeventlistener (' Resize ', function () {
Cleartimeout (TID);
Tid = SetTimeout (Refreshrem, 300);
}, False);
Win.addeventlistener (' Pageshow ', function (e) {
if (e.persisted) {
Cleartimeout (TID);
Tid = SetTimeout (Refreshrem, 300);
}
}, False);

if (doc.readystate = = = ' complete ') {
Doc.body.style.fontSize = * DPR + ' px ';
} else {
Doc.addeventlistener (' domcontentloaded ', function (e) {
Doc.body.style.fontSize = * DPR + ' px ';
}, False);
}


Refreshrem ();

FLEXIBLE.DPR = WIN.DPR = DPR;
Flexible.refreshrem = Refreshrem;
flexible.rem2px = function (d) {
var val = parsefloat (d) * THIS.REM;
if (typeof d = = = ' String ' && D.match (/rem$/)) {
Val + = ' px ';
}
return Val;
}
Flexible.px2rem = function (d) {
var val = parsefloat (d)/this.rem;
if (typeof d = = = ' String ' && D.match (/px$/)) {
Val + = ' rem ';
}
return Val;
}
}) (window, window[' lib ') | | (window[' lib '] = {}));

REM Turn PX plug-in for mobile phones

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.