告诉 iFrame 内容的方向
我目前有一个带有 iFrame 的网站,该 iFrame 是为平板电脑设备(例如 iPad)设计的。 我能够检测父级中的方向以更改 iFrame 本身的宽度和高度,但我似乎无法让 iFrame 页面在更改时检测设备的方向。
我知道如果我从 Safari Mobile 中的 iFrame 本身加载实际页面而不是在 iFrame 中,它会检测到更改并根据当时的方向进行旋转。 如果我在 iFrame 中以横向方式加载页面,则会以横向方式加载内容,反之亦然。 似乎它不想在 iFrame 中加载自身后不断检测方向,而仅在初始加载时检测。
有没有办法告诉 iFrame 方向已经以某种方式改变了?我已经使用了这段代码,但没有成功:
window.addEventListener("orientationchange", function(){
var iframe = document.getElementById("youriframe").contentWindow;
iframe.postMessage({
orientation: window.orientation
}, 'http://the.domain.of.the.iframe.com');
}, false)
更困难的是我无法编辑 iFrame 的内容,因为它不是我的!下面的代码就是您要注入的代码!
window.addEventListener("message", function(e){
var newOrientationValue = e.data.orientation;
alert(newOrientationValue); // <--- DO your own logic HERE
}, false)
请帮忙,我已经绞尽脑汁想了好几天了! 希望一切都有意义。
I currently have a web site with an iFrame that is designed for a tablet device (eg iPad).
I am able to detect orientation in the parent to change the width and height of the iFrame itself, but I can't seem to get the iFrame page to detect the orientation of the device upon change.
I know if I load the actual page from the iFrame itself in Safari Mobile without being in an iFrame, it detects changes and rotates according to the orientation at that time.
If I load the page in landscape when its in the iFrame, it load the content in landscape, and visa-versa with being in portrait.
It seems that it doesn't want to constantly detect orientation after it has loaded itself when in an iFrame, only upon initial load.
Is there a way to tell the iFrame that the orientation has changed in some way? I have used this code, but without success:
window.addEventListener("orientationchange", function(){
var iframe = document.getElementById("youriframe").contentWindow;
iframe.postMessage({
orientation: window.orientation
}, 'http://the.domain.of.the.iframe.com');
}, false)
What makes it harder is that I can't edit the content of the iFrame as it isn't mine! The code below is what you're meant to inject into it!
window.addEventListener("message", function(e){
var newOrientationValue = e.data.orientation;
alert(newOrientationValue); // <--- DO your own logic HERE
}, false)
Please help, I'be been racking my brain over this for days!
Hope that all makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论