-
-
Save mtmzorro/2b3dc557fe78b2d70c18 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
if (navigator.userAgent.indexOf('Firefox') > 0 || window.navigator.msPointerEnabled) { | |
var head = document.getElementsByTagName('head'); | |
var viewport = document.createElement('meta'); | |
viewport.name = 'viewport'; | |
viewport.id = 'viewport'; | |
viewport.content = "width=640"; | |
head.length > 0 && head[head.length - 1].appendChild(viewport); | |
$(window).resize(function () { | |
resizeWindow(); | |
}); | |
function resizeWindow() { | |
if ($(window).width() > $(window).height()) { | |
$("#wrongLayer").show(); | |
} else { | |
$("#wrongLayer").hide(); | |
} | |
} | |
$(function () { | |
resizeWindow(); | |
}); | |
} else { | |
var adaptUILayout = (function () { | |
//根据校正appVersion或userAgent校正屏幕分辨率宽度值 | |
var regulateScreen = (function () { | |
var cache = {}; | |
//默认尺寸 | |
var defSize = { | |
width: window.screen.width, | |
height: window.screen.height | |
}; | |
var ver = window.navigator.appVersion; | |
var s = window.orientation; //屏幕方向 | |
var _ = null; | |
var check = function (key) { | |
return key.constructor == String ? ver.indexOf(key) > -1 : ver.test(key); | |
}; | |
var add = function (name, key, size) { | |
if (name && key) | |
cache[name] = { | |
key: key, | |
size: size | |
}; | |
}; | |
var del = function (name) { | |
if (cache[name]) { | |
delete cache[name]; | |
} | |
}; | |
var cal = function () { | |
if (_ != null) { | |
return _; | |
} | |
for (var name in cache) { | |
if (check(cache[name].key)) { | |
_ = cache[name].size; | |
break; | |
} | |
} | |
if (_ == null) | |
_ = defSize; | |
return _; | |
}; | |
return { | |
add: add, | |
del: del, | |
cal: cal, | |
s: s | |
}; | |
})(); | |
//实现缩放 | |
var adapt = function (uiWidth) { | |
var | |
deviceWidth, | |
devicePixelRatio, | |
targetDensitydpi, | |
initialContent, | |
head, | |
viewport, | |
ua; | |
ua = navigator.userAgent.toLowerCase(); | |
//whether it is the iPhone or iPad | |
isiOS = ua.indexOf('ipad') > -1 || ua.indexOf('iphone') > -1; | |
//获取设备信息,并矫正参数值 | |
devicePixelRatio = window.devicePixelRatio; | |
devicePixelRatio < 1.5 ? 2 : devicePixelRatio; | |
if (window.orientation == 0 || window.orientation == 180) { | |
$("#wrongLayer").hide(); | |
$("#main").show(); | |
if (regulateScreen.s != 0) { | |
if (regulateScreen.cal().width < regulateScreen.cal().height) { | |
deviceWidth = regulateScreen.cal().width; | |
} else { | |
deviceWidth = regulateScreen.cal().height; | |
} | |
} else { | |
deviceWidth = regulateScreen.cal().width; | |
} | |
} else { | |
$("#wrongLayer").show(); | |
if (regulateScreen.s != 0) { | |
$(function () { | |
$("#wrongLayer").fadeIn(); | |
}); | |
if (regulateScreen.cal().width > regulateScreen.cal().height) { | |
deviceWidth = regulateScreen.cal().width; | |
} else { | |
deviceWidth = regulateScreen.cal().height; | |
} | |
} else { | |
deviceWidth = regulateScreen.cal().height; | |
} | |
} | |
if (devicePixelRatio == 2 && (deviceWidth == 320 || deviceWidth == 360 || deviceWidth == 592 || deviceWidth == 640)) { | |
deviceWidth *= 2; | |
} | |
if (devicePixelRatio == 1.5 && (deviceWidth == 320)) { | |
deviceWidth *= 2; | |
devicePixelRatio = 2; | |
} | |
if (devicePixelRatio == 1.5 && (deviceWidth == 640)) { | |
devicePixelRatio = 2; | |
} | |
//获取最终dpi | |
targetDensitydpi = uiWidth / deviceWidth * devicePixelRatio * 160; | |
initialContent = isiOS | |
? 'width=' + uiWidth + 'px, user-scalable=no' | |
: 'target-densitydpi=' + targetDensitydpi + ', width=' + uiWidth + ', user-scalable=no'; | |
$("#wrongLayer").remove(); | |
head = document.getElementsByTagName('head'); | |
viewport = document.createElement('meta'); | |
viewport.name = 'viewport'; | |
viewport.id = 'viewport'; | |
viewport.content = initialContent; | |
if (isiOS && window.orientation != 0 && window.orientation != 180) { | |
viewport.content = 'width=1280'; | |
head.length > 0 && head[head.length - 1].appendChild(viewport); | |
} else { | |
head.length > 0 && head[head.length - 1].appendChild(viewport); | |
} | |
}; | |
return { | |
regulateScreen: regulateScreen, | |
adapt: adapt | |
}; | |
})(); | |
adaptUILayout.adapt(640); | |
$(window).bind('orientationchange', function (e) { | |
adaptUILayout.adapt(640); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment