dzo*_*rdz 3 html javascript css iframe
我正在尝试根据内容的高度和宽度(网页)调整iframe的大小.我已经使用了我在堆栈的其他答案中找到的代码.对于新宽度的设置,似乎有效,但我无法达到工作的高度,我不知道为什么.
您可以在此处查看和编辑我的示例:http://jsfiddle.net/dzorz/pvtr3/
这是我的HTML:
<iframe id="finance-iframe" class="finance-iframe" src="http://wordpress.org" width="100%" height="300px" marginheight="0" frameborder="0" onLoad="autoResize('finance-iframe');"></iframe>
Run Code Online (Sandbox Code Playgroud)
和javascript:
function autoResize("finance-iframe"){
var newheight;
var newwidth;
if(document.getElementById){
newheight=document.getElementById("finance-iframe").contentWindow.document.body.scrollHeight;
newwidth=document.getElementById("finance-iframe").contentWindow.document.body.scrollWidth;
}
document.getElementById("finance-iframe").height= (newheight) + "px";
document.getElementById("finance-iframe").width= (newwidth) + "px";
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
小智 8
要查看iFrame中内容的高度,需要查看4个不同的属性.
document.documentElement.scrollHeight
document.documentElement.offsetHeight
document.body.scrollHeight
document.body.offsetHeight
Run Code Online (Sandbox Code Playgroud)
可悲的是,他们都可以给出不同的答案,这些在浏览器之间是不一致的.如果将体边距设置为0,则document.body.offsetHeight给出最佳答案.要获得正确的值,请尝试此功能; 这是从iframe-resizer库中获取的,该库也会在内容更改或调整浏览器大小时保持iFrame的正确大小.
function getIFrameHeight(){
function getComputedBodyStyle(prop) {
function getPixelValue(value) {
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) {
return parseInt(value,base);
}
var
style = el.style.left,
runtimeStyle = el.runtimeStyle.left;
el.runtimeStyle.left = el.currentStyle.left;
el.style.left = value || 0;
value = el.style.pixelLeft;
el.style.left = style;
el.runtimeStyle.left = runtimeStyle;
return value;
}
var
el = document.body,
retVal = 0;
if (document.defaultView && document.defaultView.getComputedStyle) {
retVal = document.defaultView.getComputedStyle(el, null)[prop];
} else {//IE8 & below
retVal = getPixelValue(el.currentStyle[prop]);
}
return parseInt(retVal,10);
}
return document.body.offsetHeight +
getComputedBodyStyle('marginTop') +
getComputedBodyStyle('marginBottom');
}
Run Code Online (Sandbox Code Playgroud)
<section id="about" data-type="background" data-speed="10" class="pages">
<iframe src="index.html" id="demo_frame" align="center" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe>
</section>
<script>
$('iframe').load(function() {
this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
});
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33694 次 |
| 最近记录: |