您的位置:首页 > 其它

让iframe简单有效自适应高度

2011-08-09 15:36 190 查看

一种简单有效自适应高度的iframe

<iframe src="show.htm" id="show" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" width="100%" onload="this.height=0;var fdh=(this.Document?this.Document.body.scrollHeight:this.contentDocument.body.offsetHeight);this.height=(fdh>7?fdh:7)"></iframe>


注:onload字段中的数值“7”为Iframe最低高度值,可自定义!

另外这个同时支持IE&Firefox:)

PS:如果内页的CSS设置了BODY的MARGIN属性,IFRAME自适应的高度不会把margin-top和margin-bottom的值加到里面,所以最后这地方this.height=(fdh>450?fdh:400)得改成

this.height=(fdh>450?fdh+margin-top的高度+margin-bottom的高度:400)

比如说

this.height=(fdh>450?fdh+20+15:400) //尚未测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: