我需要一个解决方案来自动调整iframe
的宽度
和高度
,以便刚好适合其内容。关键是,在iframe
加载后,宽度
和高度
可能会发生变化。我猜我需要一个事件操作来处理iframe
中包含的正文尺寸的变化。
我需要一个解决方案来自动调整iframe
的宽度
和高度
,以便刚好适合其内容。关键是,在iframe
加载后,宽度
和高度
可能会发生变化。我猜我需要一个事件操作来处理iframe
中包含的正文尺寸的变化。
<script type="application/javascript">
function resizeIFrameToFitContent( iFrame ) {
iFrame.width = iFrame.contentWindow.document.body.scrollWidth;
iFrame.height = iFrame.contentWindow.document.body.scrollHeight;
}
window.addEventListener('DOMContentLoaded', function(e) {
var iFrame = document.getElementById( 'iFrame1' );
resizeIFrameToFitContent( iFrame );
// or, to resize all iframes:
var iframes = document.querySelectorAll("iframe");
for( var i = 0; i < iframes.length; i++) {
resizeIFrameToFitContent( iframes[i] );
}
} );
</script>
<iframe src="usagelogs/default.aspx" id="iFrame1"></iframe>
DOMContentLoaded
,因为似乎没有 DOMContentReady
这个东西:https://developer.mozilla.org/en-US/search?q=domcontentready&topic=apps&topic=html&topic=css&topic=js&topic=api&topic=webdev - Max Heiber一种嵌入式的单行解决方案:从最小大小开始增加到内容大小。无需脚本标签。
<iframe src="http://URL_HERE.html" onload='javascript:(function(o){o.style.height=o.contentWindow.document.body.scrollHeight+"px";}(this));' style="height:200px;width:100%;border:none;overflow:hidden;"></iframe>
scrollHeight/scrollWidth
的答案都应该稍微调整一下,以考虑到 body 边距的影响。浏览器为文档的 body 元素(以及加载到框架中的内容)应用默认的非零边距。我找到的解决方案是添加以下代码:parseInt(window.getComputedStyle(this.contentDocument.body).margin
。 - Stanbody.parentElement.scrollHeight
替代 body.scrollHeight
(宽度同理),这样就可以包括文档边距等内容。如果使用 body
,则会排除顶级 html
元素的边距/填充。 - Jason C<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#IDofControlFiringResizeEvent").click(function () {
var frame = $('#IDofiframeInMainWindow', window.parent.document);
var height = jQuery("#IDofContainerInsideiFrame").height();
frame.height(height + 15);
});
});
</script>
jQuery("#IDofControlFiringResizeEvent").click(function ()
? - Ben$('#iframe_id').load(function () {
$(this).height($(this).contents().height());
$(this).width($(this).contents().width());
});
要使其动态调整大小,您可以这样做:
<script language="javaScript">
<!--
function autoResize(){
$('#themeframe').height($('#themeframe').contents().height());
}
//-->
</script>
<iframe id="themeframe" onLoad="autoResize();" marginheight="0" frameborder="0" src="URL"></iframe>
然后在iframe加载的页面上添加以下内容:
<script language="javaScript">
function resize()
{
window.parent.autoResize();
}
$(window).on('resize', resize);
</script>
如果您不想使用jQuery,这里有一个跨浏览器的解决方案:
/**
* Resizes the given iFrame width so it fits its content
* @param e The iframe to resize
*/
function resizeIframeWidth(e){
// Set width of iframe according to its content
if (e.Document && e.Document.body.scrollWidth) //ie5+ syntax
e.width = e.contentWindow.document.body.scrollWidth;
else if (e.contentDocument && e.contentDocument.body.scrollWidth) //ns6+ & opera syntax
e.width = e.contentDocument.body.scrollWidth + 35;
else (e.contentDocument && e.contentDocument.body.offsetWidth) //standards compliant syntax – ie8
e.width = e.contentDocument.body.offsetWidth + 35;
}
在我尝试了地球上的一切之后,这真的对我有用。
index.html
<style type="text/css">
html, body{
width:100%;
height:100%;
overflow:hidden;
margin:0px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function autoResize(iframe) {
$(iframe).height($(iframe).contents().find('html').height());
}
</script>
<iframe src="http://iframe.domain.com" width="100%" height="100%" marginheight="0" frameborder="0" border="0" scrolling="auto" onload="autoResize(this);"></iframe>
我在一个Web扩展程序中自己实现了这个功能。这个Web扩展会在每个页面中注入一些UI,这些UI位于一个iframe
内部。iframe
内的内容是动态的,因此我必须重新调整iframe
本身的宽度和高度。
我使用了React,但这个概念适用于所有库。
iframe
)在iframe
内部,我更改了body
样式,使其具有非常大的尺寸。这将使内部元素使用所有必要的空间进行布局。将width
和height
设置为100%对我来说不起作用(我猜是因为iframe
具有默认的width = 300px
和height = 150px
)
/* something like this */
body {
width: 99999px;
height: 99999px;
}
然后我将所有iframe UI注入到一个div中,并对其进行了一些样式设置。
#ui-root {
display: 'inline-block';
}
在我的应用程序中,我使用 React 在 #ui-root
内部进行渲染(我在 componentDidMount
中执行此操作)。然后,我计算此 div 的尺寸,并使用 window.postMessage
将其与父页面同步:
let elRect = el.getBoundingClientRect()
window.parent.postMessage({
type: 'resize-iframe',
payload: {
width: elRect.width,
height: elRect.height
}
}, '*')
window.addEventListener('message', (ev) => {
if(ev.data.type && ev.data.type === 'resize-iframe') {
iframe.style.width = ev.data.payload.width + 'px'
iframe.style.height = ev.data.payload.height + 'px'
}
}, false)
我正在使用这段代码来自动调整页面上所有带有“autoHeight”类的iframe的高度。经过测试,在IE、FF、Chrome、Safari和Opera中均可正常工作。
function doIframe() {
var $iframes = $("iframe.autoHeight");
$iframes.each(function() {
var iframe = this;
$(iframe).load(function() {
setHeight(iframe);
});
});
}
function setHeight(e) {
e.height = e.contentWindow.document.body.scrollHeight + 35;
}
$(window).load(function() {
doIframe();
});
function resizer(id)
{
var doc=document.getElementById(id).contentWindow.document;
var body_ = doc.body, html_ = doc.documentElement;
var height = Math.max( body_.scrollHeight, body_.offsetHeight, html_.clientHeight, html_.scrollHeight, html_.offsetHeight );
var width = Math.max( body_.scrollWidth, body_.offsetWidth, html_.clientWidth, html_.scrollWidth, html_.offsetWidth );
document.getElementById(id).style.height=height;
document.getElementById(id).style.width=width;
}
the html
<IFRAME SRC="blah.php" id="iframe1" onLoad="resizer('iframe1');"></iframe>