<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0;">
<title>横竖屏切换检测</title>
<style type="text/css">
.landscape body {
background-color: #ff0000;
} .portrait body {
background-color: #00ffff;
}
</style>
<script type="text/javascript">
// window.orientation :这个属性给出了当前设备的屏幕方向,0表示竖屏,正负90表示横屏(向左与向右)模式
// onorientationchange : 在每次屏幕方向在横竖屏间切换后,就会触发这个window事件,用法与传统的事件类似
(function(){
var supportOrientation=(typeof window.orientation == "number" && typeof window.onorientationchange == "object"); //判断浏览器是否支持orientation 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(){ //如果当前浏览器不支持orientation,则使用最简单的方法(判断窗口的高宽)
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);
})();
</script>
</head>
<body>
<div>
移动端Web开发如何处理横竖屏
</div>
</body>
</html>

  

最新文章

  1. HTML学习笔记
  2. Java浮点数float,bigdecimal和double精确计算的精度误差问题总结
  3. css3 transform动画效果与公司框架简易动画的差异
  4. c语言将2进制数转化为10进制数(栈的初始化,进栈,出栈)
  5. Android-L-Samples
  6. C#基础|初探反射
  7. 在easyui dialog的子页面内如何关闭弹窗
  8. MongoDB学习笔记02
  9. QT 自动获取可用串口
  10. package-cleanup
  11. mariadb集群与nginx负载均衡配置--centos7版本
  12. Transaction 事务简单详解
  13. (汇总)os模块以及shutil模块对文件的操作
  14. 关于parseInt()里的一些小坑
  15. pymsql简单的使用
  16. JSON初步认识
  17. spring程序打包使用该插件,不然容易报错xsd找不到
  18. Python基础知识:模块
  19. python多版本共存问题(以2.7和3.5系列版本为例)
  20. 格式化输出的方法:%、.format()、f

热门文章

  1. SOA技术的进化史
  2. JDk 内部分工具 简述
  3. Linux下源码安装Nginx服务
  4. ngnix apache tomcat集群负载均衡配置
  5. List&lt;T&gt;实体去重分组:
  6. oracle15 pl/sql 分页
  7. oracle6
  8. innodb_support_xa
  9. SQL Server存储内幕系列
  10. 使用tuple返回多个值