简体   繁体   English

Joomla iFrame动态高度

[英]Joomla iFrame dynamic height

I have developed a small angular application which needs to be used in a joomla website. 我开发了一个小的角度应用程序,需要在joomla网站中使用。 To do this, I am using an iFrame. 为此,我正在使用iFrame。 The height of the iFrame however needs to be defined in the parameters and needs to be dynamic as the application is responsive. 但是,iFrame的高度需要在参数中定义,并且随着应用程序的响应而需要动态变化。

I have been searching for two days now for possible solutions. 我一直在搜索两天,以寻求可能的解决方案。 A lot can be found using google, but none of the solutions seem to work. 使用google可以找到很多东西, 但是这些解决方案似乎都不起作用。 I am adding the iFrame using an article and adding following code: 我正在使用文章添加iFrame并添加以下代码:

<p><iframe id="glu" class="wrapper" src="calculator/#/plastic" name="iframe" width="100%" height="150">
    This option will not work correctly. Unfortunately, your browser does not support inline frames.</iframe></p>

For handling the dynamic height, one of the solutions available was to add following javascript code: 为了处理动态高度,可用的解决方案之一是添加以下javascript代码:

<script language="JavaScript">// <![CDATA[
function resize_iframe()
{

    var height=window.innerWidth;//Firefox
    if (document.body.clientHeight)
    {
        height=document.body.clientHeight;//IE
    }
    //resize the iframe according to the size of the
    //window (all these should be on the same line)
    document.getElementById("glu").style.height=parseInt(height-
    document.getElementById("glu").offsetTop-8)+"px";
}

// this will resize the iframe every
// time you change the size of the window.
window.onresize=resize_iframe; 
</script>

It seems like all of these parameters return the same, incorrect value: -Height -InnerHeight -clientHeight -> these all return the same value, which is the height that was initially defined in the iFrame. 似乎所有这些参数都返回相同的,错误的值:-Height -InnerHeight -clientHeight->这些都返回相同的值,这是iFrame中最初定义的高度。

I have tried a lot of other variants but none of them work. 我尝试了很多其他变体,但都没有用。

Does anybody have a working example of this? 有人有可行的例子吗? Note that the issue only seems to persist when using a framework like Joomla / wordpress.. 请注意,仅在使用Joomla / wordpress之类的框架时,问题似乎仍然存在。

Other options I am thinking of: -Create an iFrame element in the article javascript code and build it using the html code from the application -Create a service that holds the actual height of the iframe application. 我正在考虑的其他选项:-在文章javascript代码中创建一个iFrame元素,并使用应用程序中的html代码构建它-创建一个保存iframe应用程序实际高度的服务。 Setter can be defind in the application itself so a getter can be set in the iFrame element in the article. 可以在应用程序本身中定义Setter,因此可以在本文的iFrame元素中设置getter。

Any bright ideas / examples? 有什么好主意/示例吗?

I know there are already some stack overflow questions about this but none of them provide a correct answer to this question. 我知道已经有一些有关此问题的堆栈溢出问题,但是没有一个可以为这个问题提供正确的答案。

I believe you are on the right track. 我相信您的方向正确。 It would require a getter and setter of an iframe height variable. 这将需要iframe高度变量的获取器和设置器。

IMO I have yet to get a guaranteed percentage height solution using iframes within Joomla/WP. IMO我尚未在Joomla / WP中使用iframe获得有保证的百分比高度解决方案。 Only an absolute value seems to work across all browsers. 在所有浏览器中似乎只有绝对值起作用。 Using the following code it can assess the height value of the page being loaded. 使用以下代码,它可以评估正在加载的页面的高度值。

For the JS to calculate the height required for the iframe, the top margin of the web page & content needs to be zero. 为了让JS计算iframe所需的高度,网页和内容的上边距必须为零。

IFRAME PAGE 框架页面

  1. Remove page top margin: Set body tag margin-top:0; 删除页面顶部空白:设置正文标签margin-top:0; inline CSS. 内联CSS。
  2. Remove content top margin: style="margin-top:0;" 删除内容的上边距: style =“ margin-top:0;” inline CSS. 内联CSS。
  3. Put body content in named div: 将正文内容放入命名的div中:
  4. Place JS at bottom of the body content of the iframe page immediately below the closing </div> tag: 将JS放在iframe页面主体内容的底部,紧接在</div>标记下方:

    <script type="text/javascript"> parent.AdjustIframeHeight(document.getElementById("page-container").scrollHeight); </script>

PAGE WITH IFRAME CODE: 带有IFRAME代码的页面:

<iframe id="form-iframe" src="iframe1formexample.html" style="margin:0; width:100%; height:150px; border:none; overflow:hidden;" scrolling="no" onload="AdjustIframeHeightOnLoad()"></iframe>

Change the value of the src attribute to the URL of the iframe page. 将src属性的值更改为iframe页面的URL。 Both the id and onload attributes are required. id和onload属性都是必需的。

Insert JavaScript below the iframe: 在iframe下方插入JavaScript:

<script type="text/javascript">
function AdjustIframeHeightOnLoad() { 
document.getElementById("form-iframe").style.height = document.getElementById("form-iframe").contentWindow.document.body.scrollHeight + "px";
}
function AdjustIframeHeight(i) { 
document.getElementById("form-iframe").style.height = parseInt(i) + "px";
}
</script>

The id value of the iframe tag in the previous list item is specified in the JavaScript in three places. JavaScript中在三个地方指定了上一个列表项中iframe标记的ID值。 If the id value "form-iframe" of the iframe is changed, all three places in the JavaScript must be changed accordingly. 如果更改了iframe的id值“ form-iframe”,则必须相应更改JavaScript中的所有三个位置。

The function AdjustIframeHeightOnLoad() adjusts the iframe height when the iframe is first loaded. 第一次加载iframe时,函数AdjustIframeHeightOnLoad()调整iframe的高度。 The function AdjustIframeHeight() adjusts the iframe height when called from the JavaScript in the iframe page. 从iframe页面中的JavaScript调用时,函数AdjustIframeHeight()调整iframe高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM