<div id="box"></div>
<div id="box2"></div>
<script type="text/javascript">
function isMobile() {
return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? true : false;
}
function orient() {
if (window.orientation == undefined) {
return 'Not mobile';
}
// 竖屏
if (window.orientation == 0 || window.orientation == 180) {
return true;
}
// 横屏
else if (window.orientation == 90 || window.orientation == -90) {
return false;
}
}
document.getElementById('box').innerHTML = 'isMobile:' + isMobile()
isMobile()&&(document.getElementById('box2').innerHTML = 'isMobileVertical:' + orient())
//另一种判断屏幕方向的写法
function orient() {
// landscape(横屏)和portrait(竖屏)
return window.matchMedia("(orientation: landscape)");
}
</script>
网友评论