如何在 uiwebview 中处理 Javascript onorientationchange 事件

Waq*_*aja 5 javascript uiwebview orientation dom-events ios

任何人都可以帮助我uiwebview在 iPhone 中访问时如何处理 Javascript 中的方向更改事件?

我正在尝试捕获onorientationchangeJavascript 事件。我尝试了以下代码:

<style type="text/css" media="only screen and (max-device-width: 480px)">
    .portrait
    {
        width: 300px;
        padding: 0 15px 0 5px;
    }
    .landscape
    {
        width: 460px;
        padding: 0 15px 0 5px;
    }
</style>

<script type="text/javascript">
    window.onload = orientationchange;
    window.onorientationchange = orientationchange;
    function orientationchange() {
        if (window.orientation == 90
            || window.orientation == -90) {
            document.getElementById('contents').className = 'landscape';
        }
        else {
            document.getElementById('contents').className = 'portrait';
        }
    }
</script>

// and I have a div inside html

<div id="contents">
    my contents and description... e.t.c, and e.t.c...
</div>
Run Code Online (Sandbox Code Playgroud)

它在 safari 中运行良好,但是当我使用uiwebview方向更改事件访问页面时,没有被捕获,并且无法实现我想要的功能。

Nic*_*ver 4

我想向您指出这篇文章:“iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang”,接受答案正下方的答案确实对您的问题表示敬意:处理应用程序中的方向更改代码并使用 javascript 注入更改。我引用希望作者不会介意:

“关于 UIWebView 中未调用 onorientationchange 处理程序的第二个问题的答案:

我注意到 window.orientation 属性无法正常工作,并且 window.onorientationchange 处理程序不会被调用。大多数应用程序都会遇到这个问题。这可以通过设置 window.orientation 属性并在包含 UIWebView 的视图控制器的 willRotateToInterfaceOrientation 方法中调用 window.onorientationchange 来修复:”