[英]Dynamic iframe height
我有个问题。 我正在处理一个由中央 div 和其中的 iframe 组成的模板。 div 应根据 iframe 的内容动态调整大小。我尝试了此处找到的其他解决方案,但没有结果。 我正在使用以下 HTML 代码:
<div id="site_content" style="position:relative; width:445px; height:500px;">
<iframe id="mainframe" name="mainframe" onload="parent.adjustIFrameHeight();" FRAMEBORDER="0" scrolling="no" style="overflow:hidden; border:none;" src="http://www.letsapp.it/alecianetti/" width="445" height="600"></iframe>
</div>
我希望 div (#site_content) 在 iframe 内容更改时动态调整大小。 我试过这个 function 但我不是很擅长使用 js 和类似的东西(我现在正在研究它们;))所以我不知道它是否有用或正确
onload="parent.adjustIFrameHeight();"
为了让您了解结果,我给您链接了ThemeForest Wave CV的演示站点,这是我正在复制的模板。
谁能帮我?
刚刚试了一下!
<script type="text/javascript">
function setIframeHeight(iframeName) {
//var iframeWin = window.frames[iframeName];
var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
if (iframeEl) {
iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
//var docHt = getDocHeight(iframeWin.document);
// need to add to height to be sure it will all show
var h = alertSize();
var new_h = (h-148);
iframeEl.style.height = new_h + "px";
//alertSize();
}
}
function alertSize() {
var myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myHeight = document.body.clientHeight;
}
//window.alert( 'Height = ' + myHeight );
return myHeight;
}
</script>
</head>
<body onload="setIframeHeight('ifr');">
<center>
<iframe id="ifr" src="yourdomain!" onload="parent.adjustIFrameHeight();" width="850px" scrolling="no" ></iframe>
</center>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.