[自学做网页] iframe大小自动调整
<div class="d-b-right" id="d-b-right"> <iframe src="FindUser.aspx" frameborder="1" framespaceing= "0" scrolling="auto" marginheight="0" marginwidth="0" name="mmc" id="mmc" class="mmc" onload="iframeOnload()"></iframe> </div>
想让iframe大小自适应窗口,网上有很多操作方法,如:
/*动态调整iframe的大小*/ function changeIframeSize(iframeContentHeight){ document.getElementById('rightContent').height = iframeContentHeight+20; } /*iframe页面载入处理函数*/ function iframeOnload() { var frms=document.getElementById('rightContent'); /*判断浏览器类型*/ var height=($.browser.msie||$.browser.opera)? frms.document.body.scrollHeight:frms.contentDocument.body.offsetHeight; changeIframeSize(height); /*this.height=rightContent.document.body.scrollHeight*/ }
都可以让iframe的大小在网页载入时自动设置合适的高度。但是当调整窗口大小时,iframe的大小不会改变。我就想到让窗口打开就自动最大化
if(document.all){ top.window.resizeTo(screen.availWidth,screen.availHeight); } else if(document.layers||document.getElementById){ if(top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){ top.window.outerHeight = screen.availHeight; top.window.outerWidth = screen.availWidth; }}
但是如果打开时,如果不是第一个标签,也不会自动最大化。最后,使用了。window.onresize()事件。
function iframeOnload() { //var win = obj; mmc = document.getElementById("mmc"); var div = document.getElementById("d-b-right") if (div.offsetHeight) { winWidth = div.offsetWidth - 30; winHeight = div.offsetHeight - 30; } else { winWidth = div.offsetWidth - 30; winHeight = div.scrollHeight - 30; } mmc.style.height = winHeight + "px"; mmc.style.width = winWidth + "px"; }
onresize要放在</body>之前,放在<head></head>里只会运行一次。
window.onresize = function () { iframeOnload(); };
我肯定是土办法,期待大虾指点。