//--根据子页面的内容高度设定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);
});
});
iframe自适应框架内容高度
最新推荐文章于 2019-06-11 17:25:41 发布