闭月羞花猫: 2012年1月于 河西新城科技园
平板开发中,经常需要用到设备判断横屏竖屏,以及屏幕发生横竖变化时候所触发的一些事件。
基本上使用下面的Js就可以了。
<script>// Detect whether device supports orIEntationchange event,otherwise fall back to// the resize event.var supportsOrIEntationChange = "onorIEntationchange" in window,orIEntationEvent = supportsOrIEntationChange ? "orIEntationchange" : "resize";window.addEventListener(orIEntationEvent,function() { alert('HolY ROTATING SCREENS BATMAN:' + window.orIEntation);},false);</script>
无论是ipad还是安卓:
可以在function里面实装切换后的事件,比如横竖屏不同,画面的布局设计,CSS使用不同等等。
※你可以使用window.orIEntation来判断切换之后到底是横屏还是竖屏。
但是: 关于上面的代码,有几项是需要注意的。
1, window.orIEntation
经过测试,在ipad,和andriod系统上面,window.orIEntation来判断横竖屏用得值正好相反。
window.orIEntation值 | 横竖屏结果 | |
---|---|---|
ipad | 90或者-90 | 横屏 |
ipad | 0 或者180 | 竖屏 |
Andriod | 0 或者180 | 横屏 |
Andriod | 90或者-90 | 竖屏 |
2,如何判断自己的设备是ipad还是安卓
一个土办法: 从 navigator.userAgent 里面截取字符串。
具体参照:
http://stackoverflow.com/questions/1649086/detect-rotation-of-androID-phone-in-the-browser-with-JavaScript
一个完整的例子:
<!DOCTYPE HTML><HTML><head><Meta charset="utf-8"><Meta content="text/HTML; charset=UTF-8" http-equiv="Content-Type"><Title> 横竖屏测试网页 </Title><script type="text/JavaScript">// Detect whether device supports orIEntationchange event,otherwise fall back to// the resize event.var supportsOrIEntationChange = "onorIEntationchange" in window,orIEntationEvent = supportsOrIEntationChange ? "orIEntationchange" : "resize";// 监听事件window.addEventListener(orIEntationEvent,function() { var ua = navigator.userAgent; var deviceType=""; //判断设备类型 if (ua.indexOf("iPad") > 0) { deviceType = "isIpad"; } else if (ua.indexOf("AndroID") > 0) { deviceType = "isAndroID"; } else { alert("既不是ipad,也不是安卓!"); return; } // 判断横竖屏 if ("isIpad" == deviceType) { if (Math.abs(window.orIEntation) == 90) { alert("我是ipad的横屏"); } else { alert("我是ipad的竖屏"); } } else if ("isAndroID" == deviceType ) { if (Math.abs(window.orIEntation) != 90) { alert("我是安卓的横屏"); } else { alert("我是安卓的竖屏"); } }},false);</script></head><body>横竖屏测试网页</body></HTML>总结
以上是内存溢出为你收集整理的iPad Android系统下,平板设备判断横竖屏,以及横竖屏变化之后的事件触发(html + javascript)全部内容,希望文章能够帮你解决iPad Android系统下,平板设备判断横竖屏,以及横竖屏变化之后的事件触发(html + javascript)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)