Web开发中管理ipad屏幕的方向变化
Web开发中,我们会遇到在手机垂直或水平视角时展示不同状态的情况。下面我来总结一下检测移动设备方向变化的一些方法。
1 使用javascript
直接看代码:
<script type="text/javascript">
window.onorientationchange = function () { if ( orientation == 0 ) {
alert ('Portrait模式, Home键在下边');
}
else if ( orientation == 90 ) {
alert ('Landscape模式, Home键在右边');
}
else if ( orientation == -90 ) {
alert ('Landscap模式, Home键在左边');
}
else if ( orientation == 180 ) {
alert ('Portrait模式, Home键在上边');
}
}
</script>
原理很简单,采用window的onorientationchange的处理。每次屏幕视角方向改变时,都会触发onorientationchange事件,我们通过读取orientation属性来检测屏幕的方向(如果在firefox下,则为screen.orientation,如果是window phone IE11,则属性前都要加上ms前缀,如msOrientation,MSOrientationChange),不过这里需要注意的是,文档加载时并不会触发onorientationchange事件。因此,如果需要在文档加载时确定文档的方向,可将orientationChangeHandler()函数赋给onload事件。
$(document).ready(function() {
$(window).on('orientationchange', function(event) {
//handle orientation change
});
});
2 CSS 检测
css media 查询中可以检测设备的视角方向,示例代码如下:
@media screen and (orientation: portrait) {
//your style
}
@media screen and (orientation: landscape) {
// your style
}
你同样可以通过条件注释添加对不同视角的css文件引用:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
3 阻止屏幕方向变化 (仅在firefox和ie11中支持)
如果我们要阻止屏幕方向的变化,可以使用Screen.lockOrientation()(ie11中为msLockOrientation)方法。
Screen.lockOrientation() 方法接受屏幕的方向字符串或字符串数组为参数,可选参数为:
- portrait-primary
Portrait模式, Home键在下边
- portrait-secondary
Portrait模式, Home键在上边
- landscape-primary
Landscape模式, Home键在右边
- landscape-secondary
Landscap模式, Home键在左边
portrait:所有portrait模式
- landscape:所有landscape模式
- default:浏览器默认模式,根据屏幕分辨率决定,如1280*800为landscape模式,800*1280为portrait模式
示例代码:
var lockOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;
if (lockOrientation("landscape-primary")) {
// orientation was locked
} else {
// orientation lock failed
}
//参数可同样为字符串数组
if (lockOrientation(["landscape-primary", "landscape-secondary"])) {
// orientation was locked
} else {
// orientation lock failed
}
如果要解除锁定的话,可以使用Screen.unlockOrientation
能否使用javascript动态设定屏幕方向? 很遗憾,不能。设置orientation的值? 呵呵,确实不能。
Web开发中管理ipad屏幕的方向变化的更多相关文章
- [译]如何在Web开发中使用Python
[译]如何在Web开发中使用Python 原文:HOWTO Use Python in the Web 摘要 这篇文档展示了Python如何融入到web中.它介绍了几种Python结合web服务器的方 ...
- 第123天:移动web开发中的常见问题
一.函数库 underscoreJS _.template: <ol class="carousel-indicators"> <!--渲染的HTML字符串--& ...
- 【初码干货】使用阿里云对Web开发中的资源文件进行CDN加速的深入研究和实践
提示:阅读本文需提前了解的相关知识 1.阿里云(https://www.aliyun.com) 2.阿里云CDN(https://www.aliyun.com/product/cdn) 3.阿里云OS ...
- Redis在WEB开发中的应用与实践
Redis在WEB开发中的应用与实践 一.Redis概述: Redis是一个功能强大.性能高效的开源数据结构服务器,Redis最典型的应用是NoSQL.但事实上Redis除了作为NoSQL数据库使用之 ...
- Web开发中的主要概念
一.Web开发中的主要概念1.静态资源:一成不变的.html.js.css2.动态资源:JavaWeb.输出或产生静态资源.(用户用浏览器看到的页面永远都是静态资源) 3.JavaEE:十三种技术的集 ...
- 移动Web 开发中的一些前端知识收集汇总
在开发DeveMobile 与EaseMobile 主题 的时候积累了一些移动Web 开发的前端知识,本着记录总结的目的,特写这篇文章备忘一下. 要说移动Web 开发与传统的PC 端开发,感觉也没什么 ...
- SpringBoot学习(七)-->SpringBoot在web开发中的配置
SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...
- Web开发中的18个关键性错误
前几年,我有机会能参与一些有趣的项目,并且独立完成开发.升级.重构以及新功能的开发等工作. 本文总结了一些PHP程序员在Web开发中经常 忽略的关键错误,尤其是在处理中大型的项目上问题更为突出.典型的 ...
- MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。
MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得. 之前的项目比较简单,多是用JSP .Servlet + JDBC 直接搞定,在项目中尝试用 Strut ...
随机推荐
- java连接mysql步骤
转自:http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html JDBC连接mysql数据库 •创建一个以JDBC连接数据库的程 ...
- STM32的USART中断死循环,形成死机。
作者:观海 QQ:531622 直接说重点:我用的是 STM32F103 芯片 USART2_IRQHandler 总是中断,程序死循环. 1.出现问题: 原程序的中断处理程序是: void USA ...
- 7. LAMP环境搭建
一.准备工作 1.安装编译工具gcc.gcc-c++ 注意解决依赖关系,推荐使用yum安装,若不能联网可使用安装光盘做为yum源-- 1)编辑yum配置文件: # mount /dev/cdrom / ...
- ajax 跨域请求
1. $.ajax({ type: "get", async: false, url: "http://61.160.194.208:8383/Api/login?acc ...
- NSDate和NSDateFormatter 相关应用代码示例
此方法用来计算当前时间与目标时间的先后顺序: -(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{ //将当前时间转为本地时区 ...
- windows10 下访问 virtualbox 虚拟机的linux15.10/16.04 系统 及 用 putty 访问虚拟机的配置
参考: http://www.doc88.com/p-915707596190.html --- 安装samba http://my.oschina.net/u/2260265/blog/405598 ...
- webdriver杀死浏览器和Chromedriver进程
/** * 执行dos命令 * @param command */ public static void command(String command) { ...
- TryUpdateModel
MSDN参考 https://msdn.microsoft.com/zh-cn/library/system.web.mvc.controller.tryupdatemodel.aspx 相关博文 h ...
- 远程执行shellcode
#include "Windows.h" #include <WinSock2.h> #include <stdio.h> #pragma comment( ...
- Repeater多列分别合并单元格
GridView.Repeater合并单元格可以参考http://www.cnblogs.com/zhmore/archive/2009/04/22/1440979.html,但是原文例子是合并一列的 ...