- 浏览: 231762 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
李君寻:
内容挺好的,这是widget开发项目的大概框架吗?
Widget应用的基本组成 -
lliiqiang:
首先要有条件,算法只是提升系统而已。如果小范围变化基本可以尝试 ...
几个常用的检索排序算法的JavaScript实现 -
blessdyb:
aikki_gogh 写道最近正在研究canvas,求util ...
Canvas做游戏实践分享(十) -
aikki_gogh:
最近正在研究canvas,求utils.js和arrow.js ...
Canvas做游戏实践分享(十) -
Trinea:
请问你博客百度的广告怎么加的,不会是手动每天文章添加js吧
HTML5 WebGame开源工具之impactjs
Mobile Web开发基础之四————处理设备的横竖屏
为了应对移动设备屏幕的碎片化,我们在开发Mobile Web应用时,一个最佳实践就是采用流式布局,保证最大可能地利用有限的屏幕空间。由于屏幕存在着方向性,用户在切换了屏幕的方向后,有些设计上或实现上的问题就会凸显——我们至少需要处理一下当前显示元素的宽度的适配(当然,要做的可能不仅仅是这个)。很多时候,我们需要为不同的屏幕方向来设计对应的应用显示模式,这个时候,实时地获知设备的模竖屏状态就显得极为重要。
- window.orientation属性与onorientationchange事件
window.orientation :这个属性给出了当前设备的屏幕方向,0表示竖屏,正负90表示横屏(向左与向右)模式
onorientationchange : 在每次屏幕方向在横竖屏间切换后,就会触发这个window事件,用法与传统的事件类似
1:使用onorientationchange事件的回调函数,来动态地为body标签添加一个叫orient的属性,同时以body[orient=landspace]或body[orient=portrait]的方式在css中定义对应的样式,这样就可以实现在不同的屏幕模式下显示不同的样式。如下代码示例:
<!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"> body[orient=landscape]{ background-color: #ff0000; } body[orient=portrait]{ background-color: #00ffff; } </style> </head> <body orient="landspace"> <div> 内容 </div> <script type="text/javascript"> (function(){ if(window.orient==0){ document.body.setAttribute("orient","portrait"); }else{ document.body.setAttribute("orient","landscape"); } })(); window.onorientationchange=function(){ var body=document.body; var viewport=document.getElementById("viewport"); if(body.getAttribute("orient")=="landscape"){ body.setAttribute("orient","portrait"); }else{ body.setAttribute("orient","landscape"); } }; </script> </body> </html>
2: 类似的思路,不通过CSS的属性选择器来实现,如下代码的实现方案:
<!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> </head> <body orient="landspace"> <div> 内容 </div> <script type="text/javascript"> (function(){ var init=function(){ var updateOrientation=function(){ var orientation=window.orientation; switch(orientation){ case 90: case -90: orientation="landscape"; break; default: orientation="portrait"; break; } document.body.parentNode.setAttribute("class",orientation); }; window.addEventListener("orientationchange",updateOrientation,false); updateOrientation(); }; window.addEventListener("DOMContentLoaded",init,false); })(); </script> </body> </html>
- 使用media query方式
这是一种更为方便的方式,使用纯CSS就实现了对应的功能,如下代码演示:
<!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"> @media all and (orientation : landscape) { body { background-color: #ff0000; } } @media all and (orientation : portrait){ body { background-color: #00ff00; } } </style> </head> <body> <div> 内容 </div> </body> </html>
- 低版本浏览器的平稳降级
如果目标移动浏览器不支持media query,同时window.orientation也不存在,则我们需要采用另外一种方式来实现————使用定时器“伪实时”地对比当前窗口的高(window.innerHeight)与宽(window.innerWidth)之比,从而判定当前的横竖屏状态。如下代码所示:
<!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"> (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); })(); </script> </head> <body> <div> 内容 </div> </body> </html>
- 统一解决方案
将以上的两种解决方案整合在一起,就可以实现一个跨浏览器的解决方案,如下代码:
<!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"> (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); })(); </script> </head> <body> <div> 内容 </div> </body> </html>
【原文】http://davidbcalhoun.com/2010/dealing-with-device-orientation
发表评论
-
Mobile Web开发基础之三————由按键想到的
2012-05-17 22:33 1970按钮是天生为 ... -
Mobile Web开发基础之二————开发环境套件一览
2012-05-17 21:15 2061做为一个Mobile Web开发者,我们除了拥有真机外(这个对 ... -
Mobile Web开发基础之一————viewport标签
2012-05-17 20:04 4138再挖一个坑 ,canvas的游戏开发基础暂时 ... -
Fibonacci的非递归实现,Mobile Web横竖屏检测等
2012-05-17 18:29 0工作需要,碰到几个问题,依次罗列如下 1. Fibon ... -
几个常用的检索排序算法的JavaScript实现
2012-05-15 01:04 2335近期工作需要,开始复习相关的检索排序算法。对常用的几个算法,自 ... -
Canvas绘制的简单模拟时钟
2012-01-17 17:09 1216下午碰到这个问题,刚开始懵了,理一下思路,很简单的事情。 ... -
JavaScript 创建对象方法列举
2011-07-19 00:06 1174JavaScript 中没有类的概念,所以它的对象 ... -
JavaScript Random
2011-07-18 10:13 1097Math.random()方法返回介于0和1之间的一 ... -
no.de 使用测试
2011-04-18 18:34 1593最近折腾nodejs的人越来越多了,强大的joyent的兄弟们 ... -
使用widget进行简单移动应用开发(一)
2010-10-11 21:19 1228Widget主要作为Web 2.0服务或互联网内容的前端 ... -
JavaScript函数的几种特殊形式
2010-06-02 21:20 2165除了基本的脚本语言的函数使用,JavaScr ... -
Firebug七大扩展利器
2010-05-31 17:58 2378Firebug是我们Web开发中的不可缺少的工具,它的 ... -
JavaScript内置函数使用细节
2010-05-30 18:03 1418闲来无事,开始看Object-Oriented Ja ... -
基于OPhone的Mobile Widget联机应用开发基础
2009-12-30 10:39 1134在iPhone 引领的软件用户体验革命的潮流时,UI ... -
移动互联时代,Mobile widget将大行其道?
2009-12-02 21:53 1355目前,Widget在国内的发展如星星之火,镶嵌在博 ... -
使用jQuery来完成键盘事件的浏览器匹配
2009-11-03 20:54 2247最近写一个小的widget,需要来完成对于键盘事件 ... -
JavaScript Date 对象与函数
2009-06-05 10:28 1553JavaScript Date 对象与函数 目录 ... -
Widget开发中的一些专门的术语
2009-04-03 10:27 1329Control A control is a user int ... -
Widget应用的基本组成
2009-03-27 13:20 2912widget开发在最 ... -
什么是Widget
2009-03-26 20:01 2601移动互联网的兴起 ...
相关推荐
主要介绍了Mobile Web开发基础之-—处理手机设备的横竖屏,window.orientation属性与onorientationchange事件以及media query方式是开发过程中需要注意到的两种解决方式,需要的朋友可以参考下
为了应对移动设备屏幕的碎片化,我们在开发Mobile Web应用时,一个最佳实践就是采用流式布局,保证最大可能地利用有限的屏幕空间。由于屏幕存在着方向性,用户在切换了屏幕的方向后,有些设计上或实现上的问题就会...
【连载】Windows Mobile 贪吃蛇手机游戏开发 2——游戏设计准备知识
Windows Mobile 贪吃蛇手机游戏开发 1——开发环境的搭建与测试(Hello World源码)
详细讲解如何使用Visual Studio 进行Mobile页面开发
Head First Mobile Web shows how to use the web tech- nology you’re already familiar with to make sites and apps that work on any device of any size. Put your JavaScript, CSS media query, and ...
MSDN Webcast NET Compact Framework新动力——加速您的Windows Mobile应用开发 PDF
With this hands-on book, veteran mobile and web developer Maximiliano Firtman demonstrates which aspects of your site or app slow down the user’s experience, and what you can do to achieve lightning...
如何使用ASP.NET Mobile Controls开发Mobile Web应用
移动Web的使用在呈爆炸式增长。很快,人们会更愿意在手机和平板电脑而不是PC机上浏览网页。...使用*的开发技术,包括响应式Web设计,以及利用WURFL完成服务器端设备检测;通过图片、谜题、故事和问答轻松学习。
我们主要目的就是学会如何Mobile Control使我们的网页更适合我们的小屏幕设备,最大限度使用设备的表现能力。这次内容将包括如何在Mobile Control应用程序中如何使用Device Filters和模板定制,以及使用XHTML以及...
jquery mobile通过ajax访问web api2 接口,显示列表数据。
MSDN Webcast NET Compact Framework新动力——加速您的Windows Mobile应用开发 Video
如何使用ASP_NET Mobile Controls开发Mobile Web应用 pdf
With users increasingly accessing the web on mobile devices, it’s crucial to make sure your website is built to seamlessly fit this radical change in user behavior. Mobile Web Performance ...
High Performance Mobile Web 英文epub
SQL Server Mobile 开发基础讲解与实例
Master Mobile Web Apps with jQuery Mobile(3rd) 英文zip 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
mobile开发指南,mobile web相关技术经验汇总