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
    评论
使用iframe标签嵌入网页时,通常需要进行内容自适应缩放以适应不同屏幕尺寸或设备。下面是一种简单的方法来实现这个功能。 首先,在iframe标签中添加一个属性:sandbox="allow-scripts allow-same-origin"。这将确保框架内容可以执行脚本,并允许与嵌入网页来自相同原源的链接进行交互。 然后,在所嵌入的网页的头部,加入一个用于缩放的JavaScript脚本。这段脚本会根据窗口大小动态调整iframe的尺寸,以确保内容适应屏幕。 ```html <script> function resizeIframe() { var iframe = document.getElementById('myFrame'); // 这里的'myFrame'需要替换成你自己的iframe的id var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; // 获取窗口宽度 var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; // 获取窗口高度 var iframeWidth = windowWidth; // iframe宽度等于窗口宽度 var iframeHeight = windowHeight; // iframe高度等于窗口高度 iframe.style.width = iframeWidth + 'px'; // 设置iframe宽度 iframe.style.height = iframeHeight + 'px'; // 设置iframe高度 } window.onload = resizeIframe; // 页面加载时调用一次 window.onresize = resizeIframe; // 窗口大小改变时调用 </script> ``` 最后,需要确保iframe的宽度和高度设置为100%,以使其填充父容器。 ```html <iframe id="myFrame" src="your_url" style="width: 100%; height: 100%;"></iframe> ``` 通过以上的步骤,就可以实现iframe内容自适应缩放。无论窗口大小如何变化,内容都将以合适的比例进行缩放,以适应不同设备的屏幕。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值