手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >移动端横屏的JS代码(beta)
移动端横屏的JS代码(beta)
摘要:废话不多说了,直接给大家贴代码了具体代码如下所示:Documentvarorientation=0;functionscreenOrient...

废话不多说了,直接给大家贴代码了

具体代码如下所示:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script> var orientation=0; function screenOrientationEvent(){ if(orientation == 0) { document.getElementById("change").value="竖"; $("body").css({ 'transform':'rotate(90deg)', '-webkit-transform':'rotate(90deg)', '-moz-transform':'rotate(90deg)' }); } else { document.getElementById("change").value="横"; $("body").css({ 'transform':'rotate(0deg)', '-webkit-transform':'rotate(0deg)', '-moz-transform':'rotate(0deg)' }); } } var innerWidthTmp = window.innerWidth; //横竖屏事件监听方法 function screenOrientationListener(){ try{ var iw = window.innerWidth; //屏幕方向改变处理 if(iw != innerWidthTmp){ if(iw>window.innerHeight) { orientation = 90; } else { orientation = 0; } //调用转屏事件 screenOrientationEvent(); innerWidthTmp = iw; } } catch(e){alert(e);}; //间隔固定事件检查是否转屏,默认500毫秒 setTimeout("screenOrientationListener()",500); } //启动横竖屏事件监听 screenOrientationListener(); </script> <body onload="screenOrientationEvent()"> <input id="change" type="text" value=""/> </body> </html>

以上js代码是实现移动端横屏的核心代码,代码简单易懂,所有没给大家附太多的注释,如果大家哪里有不明白的地方欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对查字典教程网的支持!

【移动端横屏的JS代码(beta)】相关文章:

Javascript随机显示图片的源代码

简单的防盗链功能代码(iframe)

自己编写的支持Ajax验证的JS表单验证插件

加入收藏夹代码(兼容 gecko)

Ctrl + Enter提交前检测的代码

鼠标图片振动代码

网页里控制图片大小的相关代码

浏览器检测JS代码(兼容目前各大主流浏览器)

打印/预览/设置的客户端代码

实现DIV圆角的JavaScript代码

精品推荐
分类导航