let viewTimer = null
function viewPort(userAgent, pageWidth) {
let oView = document.getElementById('viewport')
if (oView) {
document.head.removeChild(oView)
}
if (!pageWidth) {
// 手机页面设计图的宽度,宽度多少设置多少
pageWidth = 375
}
let screenW = parseInt(window.screen.width)
let scale = screenW / pageWidth
if (/Android (d+.d+)/.test(userAgent)) {
let creatMeta = document.createElement('meta')
creatMeta.name = 'viewport'
creatMeta.id = 'viewport'
let version = parseFloat(RegExp.$1)
if (version > 2.3) {
creatMeta.content = 'width=' + pageWidth + ', initial-scale = ' + scale + ',user-scalable=1, minimum-scale = ' + scale + ', maximum-scale = ' + scale + ', target-densitydpi=device-dpi'
} else {
creatMeta.content = '"width=' + pageWidth + ', target-densitydpi=device-dpi'
}
document.head.appendChild(creatMeta)
} else {
let creatMeta = document.createElement('meta')
creatMeta.name = 'viewport'
creatMeta.id = 'viewport'
// 判断移动设备横屏竖屏
if (window.orientation === '-90' || window.orientation === '90') {
scale = window.screen.height / pageWidth
creatMeta.content = 'width=' + pageWidth + ', initial-scale = ' + scale + ' ,minimum-scale = ' + scale + ', maximum-scale = ' + scale + ', user-scalable=no, target-densitydpi=device-dpi'
} else {
creatMeta.content = 'width=' + pageWidth + ', initial-scale = ' + scale + ' ,minimum-scale = ' + scale + ', maximum-scale = ' + scale + ', user-scalable=no, target-densitydpi=device-dpi'
}
document.head.appendChild(creatMeta)
}
}
viewPort(navigator.userAgent)
window.onresize = function () {
clearTimeout(viewTimer)
viewTimer = setTimeout(function () {
viewPort(navigator.userAgent)
}, 500)
}