zoukankan      html  css  js  c++  java
  • 检测手机横竖屏切换

    我们做移动端项目的时候,为了更好的完善用户体会,经常会需要处理好手机横竖屏时候的效果,下面看一下通过代码如何判断手机是否是横竖屏,两种方式:

    一、第一种方式:

    <style type="text/css">
    body,
    html {
    margin: 0;
    height: 100%;
    position: relative;
    overflow: hidden;
    }
    #box {
    100%;
    height: 100%;
    font-size: 20px;
    position: relative;
    }
    #div {
    100px;
    height: 100px;
    background: red;
    }
    #pop {
    display: none;
    position: absolute;
    left: 0;
    top: 0;
    100%;
    height: 100%;
    background: #000;
    color: #fff;
    line-height: 200px;
    font-size: 30px;
    text-align: center;
    }
    </style>
    </head>
    <body>
    <div id="box">
    请保持竖屏观看哟
    <div id="div"></div>
    </div>
    <div id="pop">请不要横屏浏览页面</div>
    <script type="text/javascript">
    setChange();
    window.addEventListener('orientationchange', function(e)
    {
    setChange()
    });
    function setChange(){
    var pop = document.querySelector('#pop');
    if(window.orientation == 90 || window.orientation == -90)
    {
    pop.style.display = "block";
    } else {
    pop.style.display = "none";
    }
    }
    </script>

    二、第二种方式
    <style type="text/css">
    body,
    html {
    margin: 0;
    height: 100%;
    position: relative;
    overflow: hidden;
    }
    #box {
    position: absolute;
    left: 50%;
    top: 50%;
    font-size: 20px;
    position: relative;
    }
    #div {
    100px;
    height: 100px;
    background: red;
    }
    </style>
    </head>
    <body>
    <div id="box">
    请保持竖屏观看哟
    <div id="div"></div>
    </div>
    <div id="pop">请不要横屏浏览页面</div>
    <script type="text/javascript" src="m.Tween.js"></script>
    <script type="text/javascript">
    setStyle()
    setChange();
    window.addEventListener('orientationchange', function(e)
    {
    setChange();
    });
    function setStyle(){
    var box = document.querySelector('#box');
    var w = window.screen.width;
    var h = window.screen.height;
    box.style.width = w + "px";
    box.style.height = h + "px";
    box.style.margin = -h/2 + "px 0 0 "+(-w/2)+"px";
    }
    function setChange(){
    var box = document.querySelector('#box');
    if(window.orientation == 90 || window.orientation == -90)
    {
    css(box,"rotate",90);
    } else {
    css(box,"rotate",0);
    }
    }
    </script>

    分享技术,分享快乐!
  • 相关阅读:
    解决阿里云服务器磁盘报警
    linux服务器启动报错UNEXPECTED INCONSISTENCY解决方法
    记一次gitlab添加用户收不到邮件的解决办法
    php7安装redis拓展
    centos6.5安装部署zabbix监控服务端和客户端
    centos-6.5安装部署LNMP环境
    centos6.5编译安装php7
    centos6.5新增加硬盘挂载并实现开机自动挂载
    简单快速部署samba服务器
    第177天:常用正则表达式(最全)
  • 原文地址:https://www.cnblogs.com/babywin/p/6420692.html
Copyright © 2011-2022 走看看