简体   繁体   English

调整iframe的大小以加载内容

[英]Resize iframe to loaded content

I want to load content in the iframe and make it resize its height according to the content. 我想在iframe中加载内容,并根据内容调整其高度。 All the pages are in the same domain. 所有页面都在同一个域中。 I tried some scripts I found, but nothing worked. 我尝试了一些发现的脚本,但没有任何效果。 Most of the times, it just opens the loaded content in a new tab. 大多数情况下,它只是在新选项卡中打开加载的内容。

HTML: HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container">
  <div id="header"></div>
  <div id="nav"><a href="home.html">Home</a> <a href="the_band.html">The Band</a>
  <a href="News.html">News</a> <a href="gallery.html">Gallery</a> 
  <a href="contact.html">Contact</a></div> <div id="content"></div>
  <iframe id="frame"></iframe>
  <div id="footer"></div>
</div>
</body>
</html>

CSS: CSS:

div#container {
    height: 100%;
    width: 100%;
}
div#header {
    top: 0px;
    width: 100%;
    height: 80px;
    position: fixed;
    background-color: transparent;
    text-align: center;
}
div#nav {
    font-family: MgSouvenirLight;
    font-size: 18pt;
    color: #FF0;
    top: 120px;
    width: 100%;
    height: 40px;
    position: fixed;
    text-decoration: none;
    font-weight: bolder;
    font-variant: normal;
}
div#footer {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 9px;
    bottom: 0px;
    position: fixed;
    height: 30px;
    width: 100%;
    text-align: center;
    color: #fff;
 }
#frame {
    position: absolute;
    top: 200px;
    bottom: 40px;
    width: 800px;
    border: none;
    left: 120px;
    height: auto;
}
a:link {
    font-family: MgSouvenirLight;
    font-size: 18pt;
    color: #FF0;
}
a:visited {
    font-family: MgSouvenirLight;
    font-size: 18pt;
    color: #FF0;
}
a:hover {
    font-family: MgSouvenirLight;
    font-size: 18pt;
    font-style: italic;
    color: #FC0;
}

you could try to get the height after iframe is loaded , and change it with jquery 您可以尝试在加载iframe后获取高度,并使用jquery进行更改

     $('iframe').load(function() {
        setTimeout(iResize, 50);
        // Safari and Opera need a kick-start.
        var iSource = document.getElementById('your-iframe-id').src;
        document.getElementById('your-iframe-id').src = '';
        document.getElementById('your-iframe-id').src = iSource;
     });
     function iResize() {
        document.getElementById('your-iframe-id').style.height = 
        document.getElementById('your-iframe-id').contentWindow.document.body.offsetHeight + 'px';
     }

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

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