适应各个浏览器的iframe高度自动调整

来源:http://www.mzone.cc/article/457.html

 

[转载声明] 转载时必须标注:本文来源于铁木箱子的博客http://www.mzone.cc
[本文地址] 本文永久地址是: http://www.mzone.cc/article/457.html

      iframe一般在我们用来最大和最麻烦的就是自动高度调整了,这里结合铁木箱子的实际使用情况,给出一个javascript代码片段和测试例子。

1、mzone.cc.js文件,用来创建基于命名空间的对象


     
     
  1. /**
  2.  * Title : 基于命名空间的js对象创建
  3.  * Author : 铁木箱子
  4.  * Site : http://www.mzone.cc
  5.  * Date : 2010-10-14
  6.  * Version : 0.1
  7.  */
  8.  
  9. function mzone ( ) { } ;
  10.  
  11. /**
  12.  * Desc : 使用给定的对象路径进行对象的创建
  13.  * Para : objPath - 对象路径,字符串类型,格式为:xx.yy.zz
  14.  * Return : 没有返回值,创建成功后直接使用对象路径即可
  15.  */
  16. mzone. createObject = function ( /*string*/objPath ) {
  17. if ( !objPath || typeof (objPath ) != 'string' ) return ;
  18. var root = window ;
  19. var parts = objPath. split ( '.' ) ;
  20. for ( var i = 0 ; i < parts. length ; i ++ ) {
  21. if ( typeof (root [parts [i ] ] ) == 'undefined' ) root [parts [i ] ] = { } ;
  22. root = root [parts [i ] ] ;
  23. }
  24. } ;

2、mzone.cc.iframe.js文件,iframe的自动高度调整


     
     
  1. /**
  2.  * Title : iframe高度自动调整,适应各个浏览器(IE、Firefox、Chrome等).需要注意的是在chrome下测试时必须在web容器中测试
  3.  * Author : 铁木箱子
  4.  * Site : http://www.mzone.cc
  5.  * Date : 2010-10-14
  6.  * Version : 0.1
  7.  */
  8.  
  9. /**
  10.  * Desc : 创建mzone.cc.iframe对象
  11.  */
  12. mzone. createObject ( "mzone.cc.iframe" ) ;
  13.  
  14. /**
  15.  * Desc : 定义对象mzone.cc.iframe的静态方法实现iframe动态调整
  16.  * Para : iframe - 要调整的iframe对象,DOM对象
  17.  * extHeight - 自动计算iframe的高度后额外增加的高度值,数字型
  18.  * minHeight - iframe的最小高度,数字型
  19.  * Return : 没有返回值
  20.  */
  21. mzone. cc. iframe. autoHeight = function ( /*object*/iframe , /*int*/extHeight , /*int*/minHeight ) {
  22. iframe. style. display = "block" ;
  23. if (iframe. contentDocument && iframe. contentDocument. body. offsetHeight ) { // ff,chrome等
  24. var h = parseInt (iframe. contentDocument. body. offsetHeight ) + 50 ;
  25. h += extHeight ;
  26. if (h < minHeight ) h = minHeight ;
  27. iframe. height = h + "px" ;
  28. } else if (iframe. Document && iframe. Document. body. scrollHeight ) { // IE
  29. var h = parseInt (iframe. Document. body. scrollHeight ) + 35 ;
  30. h += extHeight ;
  31. if (h < minHeight ) h = minHeight ;
  32. iframe. height = h + "px" ;
  33. } else {
  34. var h = parseInt (iframe. contentWindow. document. documentElement. scrollHeight ) + 35 ;
  35. h += extHeight ;
  36. if (h < minHeight ) h = minHeight ;
  37. iframe. height = h + "px" ;
  38. }
  39. }

      将以上两个js保存并引入到页面中就可以按照如下方法调用实现iframe的高度自动调整了:


     
     
  1. var iframe = document. getElementById ( "iframe_body" ) ; // iframe对象
  2. var extHeight = 0 ; // 要额外增加的iframe的高度值
  3. var minHeight = 300 ; // iframe的最小高度设置
  4. mzone. cc. iframe. autoHeight (iframe , extHeight , minHeight ) ;

      以上脚本在IE、Firefox和Chrome下都测试通过,需要注意的是在google的Chrome浏览器中离线测试时是有错误的,需要在web容器(如apache或IIS)中进行测试才能够看到效果。

附:适应各个浏览器的iframe高度自动调整示例下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值