问题描述
请帮帮我,
iframe的高度第一次根据加载的高度进行更改,但是当iframe的内容更改为比当前高度更大的高度时,又再次降低了其未采用的新值.
The very first time the iframe height is changing as per the loaded height but when the iframe contents are changed to bigger height than the present one and again coming to lower its not taking new value.
例如加载的iframe高度= 1386px新内容更改iframe高度= 3440px并再次更改为新高度= 3440px(但实际高度小于1300px)
For exampleLoaded iframe height =1386pxnew content change iframe height = 3440pxand again change to new height = 3440px(but actual height is very lesser than 1300px)
这就是我尝试过的
$('iframe').load(function()
{
document.getElementById('loaderIFRAME').style.display = 'none';
document.getElementById('myiframe').style.display = 'block';
// Set inline style to equal the body height of the iframed content.
if($.browser.msie){
this.style.height = this.contentWindow.document.body.scrollHeight + 'px';
}else{
this.style.height = this.contentWindow.document.body.scrollHeight + 'px';
}
}
和另一个也一样
function autoIframe(frameId) {
try{
frame = document.getElementById(frameId);
innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
objToResize = (frame.style) ? frame.style : frame;
objToResize.height = innerDoc.body.scrollHeight + 10+ 'px';
alert( objToResize.height);
}
catch(err){
window.status = err.message;
}
}
我该如何解决?
推荐答案
问题是,当您将iframe高度设置为大值时,即使实际文档的高度较小,它的scrollHeight也始终等于大值..您可以尝试将iframe的高度设置为1px,然后阅读scrollHeight属性.
The problem is, when you have the iframe height set to a big value, then its scrollHeight always equals the big value, even if the real document height is smaller. You can try by setting the iframe height to 1px then read the scrollHeight property.
$('iframe').load(function(){
try {
var body = $(this).contents().find('body');
this.height = 1;
this.height = body.attr('scrollHeight');
} catch(e){}
});
有时最好使用offsetHeight + offsetTop属性.在这里,您可以找到我对该脚本的旧实现.
sometimes it is better to use offsetHeight+offsetTop properties.Here you can find my old implementation of this script.
这篇关于自动iframe高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!