我正在使用jQuery来控制iframe的高度。
jQuery("iframe",top.document).contents().height();
当 iframe 的高度增加时,这段代码可以工作。但当高度减少时,它就无法正常工作了。它只能返回旧值。为什么会这样呢?
我正在使用jQuery来控制iframe的高度。
jQuery("iframe",top.document).contents().height();
当 iframe 的高度增加时,这段代码可以工作。但当高度减少时,它就无法正常工作了。它只能返回旧值。为什么会这样呢?
我使用以下代码,它完美运行...
$("#frameId").height($("#frameId").contents().find("html").height());
当然,仅限于调用时(不使用“autoresize”)……但它可以随着增加和减少而工作。$("#frameId").height($("#frameId").contents().find("body").height());
- tocallaghan.contents()
的情况下设置并检索它,它对我起作用。请见下面的示例。function changeFrameHeight(newHeight){
jQuery("iframe",top.document).height(newHeight);
alert("iframe height=" + jQuery("iframe",top.document).height());
}
编辑:如果我理解正确的话,你正在尝试通过调用增量来去除滚动条,并通过调用减量返回原始高度。
在不同的浏览器上进行多次测试后,以下是适用于FF、IE、Chrome、Safari和Opera的代码。
//first declare a variable to store the original IFrame height.
var originalHeight = $("iframe",top.document).height();
将您的 heightIncrement 函数更改为使用以下内容:
heightIncrement:function(){
var heightDiv = jQuery("iframe",top.document).contents().find('body').attr('scrollHeight');
jQuery("iframe",top.document).css({height:heightDiv});
}
将您的heightDecrement函数更改为使用以下内容:
heightDecrement:function(){
jQuery("iframe",top.document).css({height:originalHeight});
}
var frame = top.document.getElementById('iFrameParentContainer').getElementsByTagName('iframe')[0];
var body = frame.contentWindow.document.body;
var height = body.scrollHeight; // possibly incorrect
body.height = "1px";
height = body.scrollHeight; // correct
这可能会导致轻微的闪烁,但通常不会。至少在我的机器上(tm)是有效的。
heightIncrement:function(){
if($.browser.mozilla)
{
var heightDiv = jQuery("iframe",top.document).contents().attr("height")+"px";
jQuery("iframe",top.document).css({height:heightDiv});
}
else if($.browser.opera || $.browser.safari || $.browser.msie)
{
var heightDiv = jQuery("iframe",top.document).height();
jQuery("iframe",top.document).css({height:heightDiv});
}
}
如果我不使用contents(),它会返回零。
这是一个对我有效的简单jQuery。在iExplorer、Firefox和Crome中进行了测试:
$('#my_frame').load(function () {
$(this).height($(this).contents().find("html").height()+20);
});
我增加了20像素,只是为了避免出现滚动条,但你可以尝试更低的数值。
我不确定人们是否满意他们的方法,但我已经写了一个非常简单的方法,对于我来说,在最新的Safari、Chrome、IE9、Opera和Firefox中似乎运行得很好。
我有一个问题,需要调整谷歌日历提供的iFrame大小,所以我只是在iFrame的样式中设置了默认大小:width="600"和高度设置基本上是我想要的最大值980,然后将其包装在一个容器Div中,高度和宽度都为100%,最小高度和最小宽度分别为400px和300px,并添加了一些jQuery代码在onload和onresize时运行...
<script>
var resizeHandle = function(){
var caseHeight = $('#calendarCase').height();
var caseWidth = $('#calendarCase').width();
var iframeWidth = $('#googleCalendar').width(caseWidth - 10);
var iframeHeight = $('#googleCalendar').height(caseWidth - 10);;
};
</script>
<body id ="home" onLoad="resizeHandle()" onResize="resizeHandle()">
为了澄清,调整大小的函数在onLoad时运行,因为我针对移动电话和平板电脑进行响应式设计,它获取calendarCase div的高度和宽度,并相应地设置iframe(#googleCalendar)的高度和宽度,减去10像素作为一些填充。
编辑我忘记提到我已经使用caseWidth来设置iFrame的高度,以保持比例约束和某处正方形。
到目前为止,这已经很好地工作了,如果有人有任何想法,为什么它可能不稳定,请建议,
谢谢
我已成功调整 iframe 加载时的高度和宽度。基本上你可以按照下面的方法来做,另外我也在我的博客上发布了一篇小文章: http://www.the-di-lab.com/?p=280
$(".colorbox").colorbox( { iframe:true, innerWidth:0, innerHeight:0, scrolling:false, onComplete:function(){ $.colorbox.resize( { innerHeight:($('iframe').offset().top + $('iframe').height()), innerWidth:($('iframe').offset().left + $('iframe').width()) } ); } } );