适配多个设备的实例技巧
说明:该技巧在系统的main入口处进行全局配置,后面配合使用rem进行适配开发,即可达到自由适配缩放的效果
main.js文件:
(function (designWidth, maxWidth) {
var doc = document,
win = window,
docEl = doc.documentElement,
remStyle = document.createElement('style'),
tid
function refreshRem () {
var width = docEl.getBoundingClientRect().width
maxWidth = maxWidth || 540
width > maxWidth && (width = maxWidth)
var rem = width * 100 / designWidth
remStyle.innerHTML = 'html{font-size:' + rem + 'px;}'
}
if (docEl.firstElementChild) {
docEl.firstElementChild.appendChild(remStyle)
} else {
var wrap = doc.createElement('div')
wrap.appendChild(remStyle)
doc.write(wrap.innerHTML)
wrap = null
}
refreshRem()
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 = '16px'
} else {
doc.addEventListener('DOMContentLoaded', function (e) {
doc.body.style.fontSize = '16px'
}, false)
}
})(1920, 1920)