html5屏幕旋转事件

         在项目开发中发现,屏幕旋转旋转后html界面出现了不同的显示效果,如果在屏幕旋转后重新加载一次界面的话就会显示的相对来说能接受一点,为此如果能监听到屏幕发生了旋转后进行相关的回调处理,则问题会得到解决,从网上搜索了大半天,并进行相关的测试后,有小的收获,仅以记之。

        可以参考这篇论坛: html5屏幕旋转事件  ,测试的时候按照@media 的css可以实现相关的body颜色改变效果,其他的方法在浏览器中没有测试成功,或许是没有在手机上进行测试缘故。

       下面的代码再手机上进行测试后,测试通过。

      html页面如下:

<!DOCTYPE html>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
    <head>
        <!--
        Customize this policy to fit your own app's needs. For more guidance, see:
            https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
        Some notes:
            * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
            * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
            * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
                * Enable inline JS: add 'unsafe-inline' to default-src
        -->
        <meta http-equiv="Content-Security-Policy">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
         <script>
         console.log(window.orientation );//打印屏幕的默认方向
        window.addEventListener("orientationchange",function(){
            console.log(window.orientation); 
        alert(window.orientation);
});
    </script>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>
     上面的代码是创建了一个cordova 项目后在主页面的index.html里面添加了测试代码,就是script标签里面的代码.

     手机浏览器支持orientationchange 事件(屏幕旋转事件) 和 window.orientation属性(屏幕旋转角度)

    orientation 可能值有 0 (默认竖屏) ,-90(向右旋转) ,90 (向左旋转)三个可选项。

关键代码:

  <script>
         console.log(window.orientation );
        window.addEventListener("orientationchange",function(){
            console.log(window.orientation);
        alert(window.orientation);
});
    </script>
      测试使用的是android设备,在ios没有进行测试。

     补充说明:关于手机横竖屏,屏幕选转的问题,以及屏幕适配的问题,在开发中都是要进行考虑的,为的是更好的用户体验,当然也可以在浏览器中固定屏幕的朝向,在游戏开发中很重要。不同的平台有不同的处理机制,比如使用以下js可以实现解除屏幕旋转锁定(屏幕可以进行旋转),和固定屏幕旋转。

   window.screen.unlockOrientation();//解除屏幕旋转锁定
 window.screen.lockOrientation('portrait-primary'); //锁定竖屏
 具体的相关其他细致用法,可以进行相关的搜索与研究。

     小结:有些特定的功能,只有在真机上进行相关的测试后才能证明(代码没有逻辑错误),在浏览器中则没有相关的效果,这需要我们多多摸索,多多尝试,这也是开发进行测试的意义所在。


  

如有不妥不正确之处,希望批评指出,相互学习,共同进步!












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值