Skip to content

Instantly share code, notes, and snippets.

@Robin-front
Created February 11, 2018 08:25
Show Gist options
  • Select an option

  • Save Robin-front/9f07905683789c7b8d40494eca26a4da to your computer and use it in GitHub Desktop.

Select an option

Save Robin-front/9f07905683789c7b8d40494eca26a4da to your computer and use it in GitHub Desktop.
vw detect,if support, vw cooperate with rem; otherwise use flexible.js for rem
!function(){var a="@charset \"utf-8\";html{color:#000;background:#fff;overflow-y:scroll;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}html *{outline:0;-webkit-text-size-adjust:none;-webkit-tap-highlight-color:rgba(0,0,0,0)}html,body{font-family:sans-serif}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{margin:0;padding:0}input,select,textarea{font-size:100%}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}abbr,acronym{border:0;font-variant:normal}del{text-decoration:line-through}address,caption,cite,code,dfn,em,th,var{font-style:normal;font-weight:500}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:500}q:before,q:after{content:''}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}a:hover{text-decoration:underline}ins,a{text-decoration:none}",b=document.createElement("style");if(document.getElementsByTagName("head")[0].appendChild(b),b.styleSheet)b.styleSheet.disabled||(b.styleSheet.cssText=a);else try{b.innerHTML=a}catch(c){b.innerText=a}}(),function(a,b){function s(){var c,b=d.getBoundingClientRect().width;c=b/10,d.style.fontSize=c+"px",j.rem=a.rem=c}var i,k,l,m,n,p,q,r,c=a.document,d=c.documentElement,e=c.querySelector('meta[name="viewport"]'),f=c.querySelector('meta[name="flexible"]'),g=0,h=0,j=b.flexible||(b.flexible={});e?(console.warn("将根据已有的meta标签来设置缩放比例"),k=e.getAttribute("content").match(/initial\-scale=([\d\.]+)/),k&&(h=parseFloat(k[1]),g=parseInt(1/h))):f&&(l=f.getAttribute("content"),l&&(m=l.match(/initial\-dpr=([\d\.]+)/),n=l.match(/maximum\-dpr=([\d\.]+)/),m&&(g=parseFloat(m[1]),h=parseFloat((1/g).toFixed(2))),n&&(g=parseFloat(n[1]),h=parseFloat((1/g).toFixed(2))))),g||h||(a.navigator.appVersion.match(/android/gi),p=a.navigator.appVersion.match(/iphone/gi),q=a.devicePixelRatio,g=p?q>=3&&(!g||g>=3)?3:q>=2&&(!g||g>=2)?2:1:1,h=1/g),d.setAttribute("data-dpr",g),e||(e=c.createElement("meta"),e.setAttribute("name","viewport"),e.setAttribute("content","initial-scale="+h+", maximum-scale="+h+", minimum-scale="+h+", user-scalable=no"),d.firstElementChild?d.firstElementChild.appendChild(e):(r=c.createElement("div"),r.appendChild(e),c.write(r.innerHTML))),a.addEventListener("resize",function(){clearTimeout(i),i=setTimeout(s,300)},!1),a.addEventListener("pageshow",function(a){a.persisted&&(clearTimeout(i),i=setTimeout(s,300))},!1),"complete"===c.readyState?c.body.style.fontSize=12*g+"px":c.addEventListener("DOMContentLoaded",function(){c.body.style.fontSize=12*g+"px"},!1),s(),j.dpr=a.dpr=g,j.refreshRem=s,j.rem2px=function(a){var b=parseFloat(a)*this.rem;return"string"==typeof a&&a.match(/rem$/)&&(b+="px"),b},j.px2rem=function(a){var b=parseFloat(a)/this.rem;return"string"==typeof a&&a.match(/px$/)&&(b+="rem"),b}}(window,window["lib"]||(window["lib"]={}));
/**
* only for mobile
* 不支持vw时,降级使用js
*/
(function() {
function computedStyle(elem, pseudo, prop) {
var result;
if ("getComputedStyle" in window) {
result = getComputedStyle.call(window, elem, pseudo);
var console = window.console;
if (result !== null) {
if (prop) {
result = result.getPropertyValue(prop);
}
} else {
if (console) {
var method = console.error ? "error" : "log";
console[method].call(
console,
"getComputedStyle returning null, its possible modernizr test results are inaccurate"
);
}
}
} else {
result = !pseudo && elem.currentStyle && elem.currentStyle[prop];
}
return result;
}
var div = document.createElement("div");
div.id = "detect";
var style = document.createElement("style");
style.type = "text/css";
var rule = "#detect{width:50vw}";
if (style.styleSheet) {
style.styleSheet.cssText = rule;
} else {
style.appendChild(document.createTextNode(rule));
}
div.appendChild(style);
var body = document.body;
if(!body){
body = document.createElement('body');
}
body.appendChild(div);
document.documentElement.appendChild(body);
var width = parseInt(window.innerWidth / 2, 10);
var compStyle = parseInt(computedStyle(div, null, "width"), 10);
if (compStyle != width) {
var script = document.createElement("script");
script.src = "./js/flexible.min.js";
document.querySelector("head").appendChild(script);
}
body.removeChild(div);
document.documentElement.removeChild(body);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment