html5 判断手机横竖屏,移动端判断横竖屏的5种解决方案
说明:
如果横竖屏切换只是替换样式, 请用第4种,
如果横竖屏切换, 需要执行某些方法,最简单粗暴的就使用 第5种方式。【小米4c,微信浏览器,小米浏览器,360浏览器亲测没有问题】
温馨提示:
1、如果移动端所有浏览器都失效,请检查手机屏幕旋转是否开启;
2、如果只有微信旋转失效,而在浏览器中打开正常,请打开微信的【开启横屏模式】;
3、如果以上两条都无法解决,请检查人品。
移动端判断横竖屏切换,主要的五种方式
1. 浏览器自带事件 orientationchange
var updateOrientation = function() {
var orientation = window.orientation;
switch(orientation) {
case 90: case -90:
orientation = 'landscape';
break;
default:
orientation = 'portrait';
}
// set the class on the HTML element (i.e. )
document.body.parentNode.setAttribute('class', orientation);
};
// event triggered every 90 degrees of rotation
window.addEventListener('orientationchange', updateOrientation, false);
2. CSS样式的
@media all and (orientation: portrait) {
body div { width: 10%; }
}
@media all and (orientation: landscape) {
body div { width: 15%; }
}
3. 定时器判断页面宽高
var updateOrientation = function() {
// landscape when width is biggest, otherwise portrait
var orientation = (window.innerWidth > window.innerHeight) ? 'landscape': 'portrait';
// set the class on the HTML element (i.e. )
document.body.parentNode.setAttribute('class', orientation);
};
// initialize the orientation
updateOrientation();
// update every 5 seconds
setInterval(updateOrientation, 5000);
4. 方法1 和 2 两种的结合, 因为1 存在兼容性问题,比如微信浏览器不触发, 那么就可以用定时器,判断CSS。
(function(){
var supportsOrientation = (typeof window.orientation == 'number' && typeof window.onorientationchange == 'object');
var HTMLNode = document.body.parentNode;
var updateOrientation = function() {
// rewrite the function depending on what's supported
if(supportsOrientation) {
updateOrientation = function() {
var orientation = window.orientation;
switch(orientation) {
case 90: case -90:
orientation = 'landscape';
break;
default:
orientation = 'portrait';
}
// set the class on the HTML element (i.e. )
HTMLNode.setAttribute('class', orientation);
}
} else {
updateOrientation = function() {
// landscape when width is biggest, otherwise portrait
var orientation = (window.innerWidth > window.innerHeight) ? 'landscape': 'portrait';
// set the class on the HTML element (i.e. )
HTMLNode.setAttribute('class', orientation);
}
}
updateOrientation();
}
var init = function() {
// initialize the orientation
updateOrientation();
if(supportsOrientation) {
window.addEventListener('orientationchange', updateOrientation, false);
} else {
// fallback: update every 5 seconds
setInterval(updateOrientation, 5000);
}
}
window.addEventListener('DOMContentLoaded', init, false);
})();
5. 最简单粗暴的 onresize 事件【注意,不能加载 document 或者 body 上】,**必须加在 window对象上** ,因为 body 和 document 的大小可能因为HTML内容而变化, 这样会造成不必要的触发。
$(window).on('resize', function () {
fn_onResize();
})
参考文章
html5 判断手机横竖屏,移动端判断横竖屏的5种解决方案相关推荐
- php判断手机 跳转代码,php判断客户端是手机设备然后跳转到手机站
// 如果是手机设置,跳转到手机页面 if (isMobile ()) { Header ( "Location: mobile.php" ); exit (); } /** * ...
- java 判断手机访问_java后台如何判断是移动端还是pc端的访问请求
java后台如何判断是移动端还是pc端的访问请求 主要是根据 HttpServletRequest request 中的请求头所带的参数 user-agent来获取: String userAge ...
- html5 判断手机电脑,H5_0006:JS判断PC,平板,手机平台的方法
//平台.设备和操作系统 var system = { win: false, mac: false, xll: false, ipad: false }; //检测平台 var p = naviga ...
- php判断手机浏览器,PHP环境下判断客户端是否为手机浏览器
//判断是否为移动浏览器 function check_wap() { if (isset($_SERVER['HTTP_VIA'])) return true; if (isset($_SERVER ...
- java 判断手机运营商_JS正则表达式判断手机号所属运营商
根据用户的输入手机号码判断该号的运营商是移动.联通.电信或其他,再根据不同的运营商做出相应的处理,下面介绍js中如何判断手机号的运营商的代码电话号码是电话管理部门为电话机设定的号码.一般7--8位数组 ...
- html5 video 自动横屏,html 5web端 video 竖屏肿么变横屏
为一个文档定义屏幕方向 通过screen.orientation属性的lock()方法可以调整屏幕方向,其默认值是any,这允许设备根据其物理方向来应用任何方向.值"natural" ...
- android陀螺仪判断手机旋转方向,unity 陀螺仪判断手机方向
首先说一下为什么用陀螺仪,很多时候当ios设备系统级别锁定方向后,我们使用ios系统的设备方向方法将不能其作用. ios系统代码中为一下代码: //下面代码只有在设备系统不锁定屏幕旋转是Natific ...
- 基于手机微信或PC端的西门子PLC远程监控和数据采集解决方案
一.远程监控西门子PLC的意义: 随着工业物联网和丰富多样技术的融合发展,通过手机微信.小程序或者PC电脑终端对西门子PLC进行远程预警监控的技术已经十分成熟,在智能工厂.设备制造商等领域应用广泛. ...
- android播放器全屏,Android端实现全屏播放的解决办法
之前碰到过视频播放全屏的问题,但是并没有很好解决,后面做网站时用到过一款视屏播放插件jwplayer,后面把它嵌入mui中实现全屏播放了,仅Android端测试,ios未测试. Loading the ...
- 判断当前是否是移动端H5打开
最近遇到一个第三方支付的问题在app上需要拉去应用去授权,所以要判断是否是H5移动端 判断是否是移动端 返回true的时候就是移动端 false是pc端 function mobile (){retu ...
最新文章
- 使用Python中的OpenCV降噪功能增强图像的3个步骤
- vm 软件现在地址加速版
- Android用户界面设计学习之旅-第二站
- 字节JAVA研发面试
- 自学编程的人,都是怎么找到自己的第一份工作的?
- Android 系统(55)---Android App开发之ANR异常的原因分析及处理总结
- android控件字体,android 设置控件的颜色字体的方法
- 鸿蒙太空是什么意思,[评论]林黛玉:“眼泪还债”暗洒闲抛知为谁?
- matlab 去高光,Specular-Highlight-Mitigation-Removal-master
- 在CAD中加载大影像的一种方法
- sheetJS+input——实现vue导入excel文件,并判断文件内容是否正确——基础积累
- Python入门03——函数相关
- 视觉在机器人领域的应用
- Adam,AdamW,LAMB优化器原理与代码
- Spring Boot安装及使用(2021.10.28)
- linux分辨率自动恢复,Linux系统Manjaro分辨率调整恢复
- 看得见的数据结构Android版之表的数组实现(视图篇)
- mysql负载突然飙升_hugepages使用出现kswapd导致系统负载突然上升
- 论文导读 | 基于多臂赌博机(MAB)建模的SimRank计算
- 多视图几何中的三维重建
热门文章
- mac注销快捷键_Mac快捷键大全
- 标准差和标准误差、平均值
- GAMIT/GLOBK处理流程
- 不属于计算机网络性能指标的是,数据传输速率是计算机网络的一项重要性能指标,下面不属于计算机网络数据传输常用单位的是_______。...
- html闹钟设置,设置闹钟标签.html
- 2018年6月2日 星期六 天气晴
- Linux日文教程,【图片】#教程#修正配置 Noto Sans CJK 避免中文显示为异体(日文)字形【linux吧】_百度贴吧...
- 微信公众号消息模板——Java
- 怎么把PDF转换成图片?推荐6个终极解决方法!
- 浅谈android应用之编程语言