js利用orientation属性判断移动设备是竖屏还是横屏的方法

目录
文章目录隐藏
  1. window.orientation 属性
  2. onorientationchange 事件
  3. 结束语

在开发 web 页面时,我们不仅要考虑 pc 端的自适应问题,还要应对移动设备屏幕的横屏和竖屏显示问题,最佳的实践方法就是流式布局,保证最大可能的利用有限的屏幕空间空间。但是用户在用手机端浏览页面时会存在着方向性,很多时候,我们需要为不同的屏幕方向来设计对应的应用显示模式,这个时候,实时地获知设备的模竖屏状态就显得极为重要,即横屏竖屏浏览效果。

js 利用 orientation 属性判断移动设备是竖屏还是横屏的方法

window.orientation 属性

window.orientation 属性目前移动端的浏览器一般都支持 window.orientation 这个参数,通过这个参数可以判断出手机设备当前的屏幕方向。
屏幕方向对应的 window.orientation 值:
ipad: 90 或 -90 横屏
ipad: 0 或 180 竖屏
Andriod:0 或 180 横屏
Andriod: 90 或 -90 竖屏

onorientationchange 事件

在每次屏幕方向在横竖屏间切换后,就会触发这个 window 事件。通过添加监听事件 onorientationchange,进行执行就可以了。

判断手机横屏竖屏方法

//判断手机横竖屏状态:
function hengshuping(){
if(window.orientation==180||window.orientation==0){
alert("竖屏状态!")
}
if(window.orientation==90||window.orientation==-90){
alert("横屏状态!")
}
}
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", hengshuping, false);

然而在 ipad、iphone 网页开发中,我们很可能需要判断是横屏或者竖屏。
一起来看看如何用 jQuery 判断 iPad、iPhone、Android 是横屏还是竖屏的方法。

function orient() {
	if(window.orientation == 90 || window.orientation == -90) {
		//ipad、iphone 竖屏;Andriod 横屏
		$("body").attr("class", "landscape");
		orientation = 'landscape';
		return false;
	} else if(window.orientation == 0 || window.orientation == 180) {
		//ipad、iphone 横屏;Andriod 竖屏
		$("body").attr("class", "portrait");
		orientation = 'portrait';
		return false;
	}
}
//页面加载时调用
$(function() {
	orient();
});
//用户变化屏幕方向时调用
$(window).bind('orientationchange', function(e) {
	orient();
});

以下是网络搜集的 2 种方法:

一、使用 mediaquery 方式

这是一种更为方便的方式,使用纯 CSS 就实现了对应的功能,如下代码演示:

@media all and (orientation : landscape) {
    body {
        background-color: #ff0000;
    }
}
  
@media all and (orientation : portrait){
    body {
        background-color: #00ff00;
    }
}

二、低版本浏览器的平稳降级

如果目标移动浏览器不支持 media query,同时 window.orientation 也不存在,则我们需要采用另外一种方式来实现————使用定时器“伪实时”地对比当前窗口的高(window.innerHeight)与宽(window.innerWidth)之比,从而判定当前的横竖屏状态。如下代码所示:

(function() {
	var updateOrientation = function() {
		var orientation = (window.innerWidth > window.innerHeight) ? "landscape" : "portrait";
		document.body.parentNode.setAttribute("class", orientation);
	};

	var init = function() {
		updateOrientation();
		window.setInterval(updateOrientation, 5000);
	};
	window.addEventListener("DOMContentLoaded", init, false);
})();

三、终极解决方案

将以上的两种解决方案整合在一起,就可以实现一个跨浏览器的解决方案,如下代码:

(function() {
	var supportOrientation = (typeof window.orientation == "number" && typeof window.onorientationchange == "object");
	var updateOrientation = function() {
		if(supportOrientation) {
			updateOrientation = function() {
				var orientation = window.orientation;
				switch(orientation) {
					case 90:
					case -90:
						orientation = "landscape";
						break;
					default:
						orientation = "portrait";
				}
				document.body.parentNode.setAttribute("class", orientation);
			};
		} else {
			updateOrientation = function() {
				var orientation = (window.innerWidth > window.innerHeight) ? "landscape" : "portrait";
				document.body.parentNode.setAttribute("class", orientation);
			};
		}
		updateOrientation();
	};

	var init = function() {
		updateOrientation();
		if(supportOrientation) {
			window.addEventListener("orientationchange", updateOrientation, false);
		} else {
			window.setInterval(updateOrientation, 5000);
		}
	};
	window.addEventListener("DOMContentLoaded", init, false);
})();

结束语

以上就是通过 js 的 orientation 属性判断手机设备横屏还是竖屏的全部方法,内容仅供参考如有所不当之处,欢迎留言指正。

「点点赞赏,手留余香」

10

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » js利用orientation属性判断移动设备是竖屏还是横屏的方法

2 评论

  1. 可以参考一下

回复 婆滴蜡 取消回复