iframe自适应框架内容高度

//--根据子页面的内容高度设定iframe的高度。在初始化及子页面改变的时候都可以自适应。
$(function(){
  var ifr=$("#Selector");
  var ifr_loaded=false;
  ifr.load(function(){ 
    ifr_loaded=true;
    var childHeight=ifr[0].contentWindow.document.documentElement.scrollHeight; 
    console.log(childHeight);
    ifr[0].height =  childHeight;
    ifr.height(childHeight);
    //--当子页面高度改变时候,iframe也要跟着改变。
    $(ifr[0].contentWindow.document).resize(function(){
      var height2=ifr[0].contentWindow.document.documentElement.scrollHeight;
    ifr[0].height =  height2;
    ifr.height(height2);    

    });


    });
  //--父窗口重新resize尺寸时候,计算高度。
  var timer_ifr=null;
  $(window).resize(function(){
    if(ifr_loaded==false){
      //--系统没有加载完成子窗口就不要执行了,没意义。
      return;
    }
    if(timer_ifr!=null){
      clearTimeout(timer_ifr);
    }
    timer_ifr=setTimeout(function(){
      var height2=ifr[0].contentWindow.document.documentElement.scrollHeight;
    ifr[0].height =  height2;
    ifr.height(height2);         
      timer_ifr=null;
    },500);
  });
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值