最近在做一个网页效果的时候,碰到了iframe自适应高度的问题,几经周转总算差不多了。
<div class="r_content">
<iframe src="main.html" name="content" id="content" οnlοad="reinitIframe()" frameborder="0"></iframe>
<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById('content');
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
if(height<570) height=570;
iframe.style.height = height + 'px';
}catch (ex){}
}
//window.setInterval("reinitIframe()", 900); //如果iframe里面要操作dom 增加或减少,可以开启这个。避免增加了或减少了借点而iframe高度不变。 谷歌浏览器要在服务器上运行才会有效果。
//window.setTimeout("reinitIframe()", 2000);
</script>
</div>
贴出来,记录一下。
http://500px.com/photo/45519120
http://500px.com/photo/45519118
http://500px.com/photo/45519116
http://500px.com/photo/45519110
http://500px.com/photo/45519108
http://500px.com/photo/45519106
http://500px.com/photo/45519104
http://500px.com/photo/45519102
http://500px.com/photo/45519100
http://500px.com/photo/45518470
http://500px.com/photo/45518468
http://500px.com/photo/45518466
http://500px.com/photo/45518464
http://500px.com/photo/45518462
http://500px.com/photo/45517650
http://500px.com/photo/45517648
http://500px.com/photo/45517646
http://500px.com/photo/45517644
http://500px.com/photo/45517642
http://500px.com/photo/45516818
http://500px.com/photo/45516814
http://500px.com/photo/45516812
http://500px.com/photo/45516810
http://500px.com/photo/45516806
http://www.zhihu.com/collection/26321153
http://www.zhihu.com/collection/26322856
http://www.zhihu.com/collection/26323045