2016-08-15 59 views
0

我试图根据iFrame内的大小更改iFrame的大小,挑战来自用户在iFrame内开始单击时检查我使用的间隔iFrame来检查内容的大小,像这样jQuery检查iFrame高度并调整

var checkHeight = setInterval(function(){ 
checkHeight(); 
},1000); 

function checkHeight() { 
    var height = $('.foo').contents().height(); 
    console.log(height) 
    $('.foo').css({ 
    'height': height + 'px' 
    }); 
} 

这是好的,当用户进入一个页面,内容为600像素,所述iFrame设置为600像素,但是当用户然后进入到一个页面的高度是200px,iFrame注销并设置600px,我无法弄清楚为什么jQuery保存最高的高度数字?在每次函数调用时将该值设置为零会使页面跳动。

回答

0

我使用的是类似的东西:

$(function(){ 
    $('#foo').load(function(){ 
     $('#foo').contents().find("body").on('click', function(event) { 
      runAfterTimeout('foo'); 
     }); 
    }); 
}); 

function runAfterTimeout(id) { 
    setTimeout(function(){ 
     autoResize(id); 
    },1000); 
}; 

function autoResize(id){ 
    var newheight; 
    var newwidth; 

    if(document.getElementById){ 
     newheight=document.getElementById(id).contentWindow.document.body.scrollHeight; 
     newwidth=document.getElementById(id).contentWindow.document.body.scrollWidth; 
    } 

    document.getElementById(id).height=(newheight) + "px"; 
    document.getElementById(id).width=(newwidth) + "px"; 
} 

而且在IFRAME补充一点:的onload = “自动调整( '富')”