-
-
Save NE-SmallTown/71939ca0763946e322a0d787bae03cee to your computer and use it in GitHub Desktop.
flexible and ant-design-mobile mix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(win, lib) { | |
var doc = win.document; | |
var docEl = doc.documentElement; | |
var metaEl = doc.querySelector('meta[name="viewport"]'); | |
var tid; | |
var flexible = lib.flexible || (lib.flexible = {}); | |
var ua = navigator.userAgent; | |
var matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i); | |
var UCversion = ua.match(/U3\/((\d+|\.){5,})/i); | |
var isUCHd = UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80; | |
var isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi); | |
var dpr = win.devicePixelRatio || 1; | |
if (!isIos && !(matches && matches[1] > 534) && !isUCHd) { | |
// 如果非iOS, 非Android4.3以上, 非UC内核, 就不执行高清, dpr设为1; | |
dpr = 1; | |
} | |
var 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 + ', user-scalable=no'); | |
if (docEl.firstElementChild) { | |
docEl.firstElementChild.appendChild(metaEl); | |
} else { | |
var wrap = doc.createElement('div'); | |
wrap.appendChild(metaEl); | |
doc.write(wrap.innerHTML); | |
} | |
} | |
function refreshRem(){ | |
var width = docEl.getBoundingClientRect().width; | |
if (width / dpr > 540) { | |
width = 540 * dpr; | |
} | |
var rem = width / 10; | |
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 = 12 * dpr + 'px'; | |
} else { | |
doc.addEventListener('DOMContentLoaded', function(e) { | |
doc.body.style.fontSize = 12 * 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'] = {})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment