美文网首页
解决iframe高度自适应的问题

解决iframe高度自适应的问题

作者: 睡在下铺的丑哔 | 来源:发表于2017-05-25 09:56 被阅读0次

解决iframe高度自适应的问题

最近做某项目,要用到iframe实现类似tab切换的功能,遇到高度不能自适应的问题,最后还是靠张鑫旭老师的文章解决问题,想看具体的直接点链接,下面上代码:

父页面代码

 var iframe = document.getElementById("iframe");
 var iframeHeight = function() {
   var hash = window.location.hash.slice(1),h;
   if (hash && /height=/.test(hash)) {
      h = hash.replace("height=", "");
      iframe.height = h;
   }
  setTimeout(iframeHeight, 100);
 };
 iframeHeight();

子页面代码

var height = $(document).height();
var hostUrl = window.top.location.toString().split("#")[0];
if (hostUrl) {
  hostUrl += "#height=" + height;
  window.top.location = hostUrl;
}

OK!完美解决

相关文章

网友评论

      本文标题:解决iframe高度自适应的问题

      本文链接:https://www.haomeiwen.com/subject/exmaxxtx.html