iframe高度自适应

南笙酒味 提交于 2019-11-26 21:08:37

前提:父页面是功能选项,固定在网页上。在网页留出的空白部分,用于嵌套子页面

为了美观和用户体验,子父页面共用子页面的纵向滚动条,且子页面的内容是变化的,所以要设置iframe高度自适应

 

js代码

function setIframeHeight(iframe) {
  if (iframe) {    /*      contentDocument是获得iframe子窗口的document对象,兼容Firefox和ie8+      contentWindow是获得子窗口的window对象,兼容大部分浏览器    */
    var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
    if (iframeWin.document.body) {
      iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
    }
  }
};
//获取子页面的高度
window.onload = function () {
  setIframeHeight(document.getElementById('external-frame'));
};

 

html

<iframe id="iframe-center" scrolling="yes" rameborder="0" src="center.html" name="centerPage"class="pull-left" onload="setIframeHeight(this)"></iframe>

 

详情参考:http://caibaojian.com/iframe-adjust-content-height.html?tdsourcetag=s_pctim_aiomsg

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!