gpt4 book ai didi

javascript - 如何处理 uiwebview 内的 Javascript onorientationchange 事件

转载 作者:行者123 更新时间:2023-12-03 19:10:50 24 4
gpt4 key购买 nike

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

我正在 try catch Javascript 的 onorientationchange 事件。我尝试了以下代码:

<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>

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

最佳答案

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

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

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

关于javascript - 如何处理 uiwebview 内的 Javascript onorientationchange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5220999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com