js利用orientation属性判断移动设备是竖屏还是横屏的方法
在开发 web 页面时,我们不仅要考虑 pc 端的自适应问题,还要应对移动设备屏幕的横屏和竖屏显示问题,最佳的实践方法就是流式布局,保证最大可能的利用有限的屏幕空间空间。但是用户在用手机端浏览页面时会存在着方向性,很多时候,我们需要为不同的屏幕方向来设计对应的应用显示模式,这个时候,实时地获知设备的模竖屏状态就显得极为重要,即横屏竖屏浏览效果。
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 属性判断手机设备横屏还是竖屏的全部方法,内容仅供参考如有所不当之处,欢迎留言指正。
码云笔记 » js利用orientation属性判断移动设备是竖屏还是横屏的方法
可以参考一下
正需要