iframe自适应高度处理
一中方法:
在子页面加载完毕的时候执行
parent.document.getElementById("iframe").height=0;
parent.document.getElementById("iframe").height=document.body.scrollHeight;
另一种:
在主页面 iframe onLoad 时间里面写
function iframeLoad()
{
document.getElementById("iframe").height=0;
document.getElementById("iframe").height=document.getElementById("iframe").contentWindow.document.body.scrollHeight;
}
内容宽度变化的iframe高度自适应
1.(兼容不好)
<iframe src="backtop.html" frameborder="0" scrolling="no" id="test" onload="this.height=100"></iframe>
<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById("test");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
console.log(height);
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>
2.(兼容不好)
<iframe src="{{ url('/admins/welcome') }}" frameborder="0" width="100%" id="mainIframe" name="mainIframe" scrolling="no" ></iframe>
<script type="text/javascript">
            function SetCwinHeight(){
                var iframeid=document.getElementById("mainIframe"); //iframe id
                if (document.getElementById){
                    if (iframeid && !window.opera){
                        if (iframeid.contentDocument && iframeid.contentDocument.body.offsetHeight){
                            iframeid.height = iframeid.contentDocument.body.offsetHeight;
                        }else if(iframeid.Document && iframeid.Document.body.scrollHeight){
                            iframeid.height = iframeid.Document.body.scrollHeight;
                        }
                    }
                }
            }
        window.setInterval("SetCwinHeight()", 1000);
</script>
3.(兼容好些因为是用jquery写的) 终极
<iframe src="{{ url('/admins/welcome') }}" frameborder="0" width="100%" id="mainIframe" name="mainIframe" scrolling="no" >
            </iframe>
    <script src="/static/js/jquery/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function(){
            });
            function myfunSetHeight()
            {
                var iframeHeight = $("#mainIframe").contents().find("body").height();
                $("#mainIframe").height(iframeHeight);
            }
        window.setInterval("myfunSetHeight()", 1000);//选下面
window.onload = function () {
    myfunSetHeight();
};
</script>
跨域下的iframe自适应高度
跨域的时候,由于js的同源策略,父页面内的js不能获取到iframe页面的高度。需要一个页面来做代理。
 方法如下:假设www.a.com下的一个页面a.html要包含www.b.com下的一个页面c.html。
 我们使用www.a.com下的另一个页面agent.html来做代理,通过它获取iframe页面的高度,并设定iframe元素的高度。·
a.html中包含iframe:
<iframe src="http://www.b.com/c.html" id="Iframe" frameborder="0" scrolling="no" style="border:0px;"></iframe>
在c.html中加入如下代码:
<iframe id="c_iframe" height="0" width="0" src="http://www.a.com/agent.html" style="display:none" ></iframe>
<script type="text/javascript">
(function autoHeight(){
var b_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
var b_height = Math.max(document.body.scrollHeight,document.body.clientHeight);
var c_iframe = document.getElementById("c_iframe");
c_iframe.src = c_iframe.src + "#" + b_width + "|" + b_height; // 这里通过hash传递b.htm的宽高
})();
</script>
最后,agent.html中放入一段js:
<script type="text/javascript">
var b_iframe = window.parent.parent.document.getElementById("Iframe");
var hash_url = window.location.hash;
if(hash_url.indexOf("#")>=0){
var hash_width = hash_url.split("#")[1].split("|")[0]+"px";
var hash_height = hash_url.split("#")[1].split("|")[1]+"px";
b_iframe.style.width = hash_width;
b_iframe.style.height = hash_height;
}
</script>
agent.html从URL中获得宽度值和高度值,并设置iframe的高度和宽度(因为agent.html在www.a.com下,所以操作a.html时不受JavaScript的同源限制)
终极自己写的一个
<script src="/static/js/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ });
function myfunSetHeight()
{
var iframeHeight = $("#mainIframe").contents().find("body").height();
$("#mainIframe").height(iframeHeight+120);//多加120个像素
} window.setInterval("myfunSetHeight()", 1000);//选下面
window.onload = function () {
    myfunSetHeight();
};
</script>
转与参考:http://caibaojian.com/iframe-adjust-content-height.html
iframe自适应高度处理的更多相关文章
- jquery 清空 iframe 的内容,,iframe自适应高度
		
$(iframe).contents().find("body").html(""); iframe自适应高度 $("#AllDescription& ...
 - 真正的让iframe自适应高度 兼容多种浏览器随着窗口大小改变
		
今天有朋友问到我关于"iframe自适应高度"的问题,原本以为是很简单的问题,没想到折腾了20分钟才搞定.期间遇到几个问题,要么是高度自适应了,但是当窗口改变时会出现滚动条.也就是 ...
 - jquery iframe自适应高度[转]
		
经典代码 iFrame 自适应高度,在IE6/IE7/IE8/Firefox/Opera/Chrome/Safari通过测试. 很古老的方法: <iframe src="../In ...
 - js实现iframe自适应高度
		
转自:http://www.jb51.net/article/15780.htm 对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的! 不带边框的iframe因为能 ...
 - 网页制作技巧:iframe自适应高度
		
转自:http://www.enet.com.cn/article/2012/0620/A20120620126237.shtml 通过Google搜索iframe 自适应高度,结果5W多条,搜索if ...
 - Iframe 自适应高度的方法!
		
第一种方法:代码简单,兼容性还可以,大家可以先测试下. function SetWinHeight(obj) { var win=obj; if (document.getElementById) { ...
 - iframe自适应高度的多种方法小结
		
转自:http://www.jb51.net/article/15780.htm 不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的大小 ...
 - iframe自适应高度的多种方法方法小结
		
对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的 不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 ifram ...
 - [转载]再谈iframe自适应高度
		
Demo页面:主页面 iframe_a.html ,被包含页面 iframe_b.htm 和 iframe_c.html 下面开始讲: 通过Google搜索iframe 自适应高度,结果5W多条,搜索 ...
 - iframe自适应高度的多种方法方法小结(转)
		
对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是 iframe的 ...
 
随机推荐
- VS2010程序打包操作(超详细的)转
			
1. 在vs2010 选择“新建项目”----“其他项目类型”----“Visual Studio Installerà“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, ...
 - OpenERP实施记录(14):收款处理
			
本文是<OpenERP实施记录>系列文章的一部分. 1. 在前面的文章中,销售订单确认时自动生成了客户发票,可以在 会计 > 客户 > 客户发票 查询,状态为"草稿& ...
 - Singleton 单例模式(懒汉方式和饿汉方式)
			
单例模式的概念: 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 关键点: 1)一个类只有一个实例 这是最基本 ...
 - 实现SQL Server中的切割字符串SplitString函数
			
有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.没什么好说的,需要的朋友直接拿去用吧 SET ANSI_NULLS ON GO S ...
 - iOS:转载sqlite3
			
SQLITE3 使用总结 2012-08-21 13:48:28 分类: SQLite/嵌入式数据库 SQLITE3 使用总结 2009-09-16 07:36 2624人阅读 评论(10) 收藏 ...
 - 数学图形(1.40)T_parameter
			
不记得在哪搞了个数学公式生成的图形. vertices = t = to (*PI) r = 2.0 x = r*(*cos(t) - cos(*t)) y = r*(*sin(t) - sin(*t ...
 - 查看和调试Qt源码(动态编译的QT也可进入源码)good
			
简述 在调试程序的时候,有时需要调试进入 Qt 源码,这不仅有利于我们了解内部实现机制,而且对于解决一些隐蔽性问题很有帮助. 都知道 F11 是“单步进入”,可是在调试的过程中,按下 F11 却无法进 ...
 - 当Windows Server 2012的主DC出了问题, 如何迁移其上的FSMO角色?
			
步骤如下: 1. 遵循https://support.microsoft.com/kb/255504中的transfer FSMO的步骤. 2. 这样做之后还没完. 因为Windows Server ...
 - phpstorm failed to create jvm:error code -6 解决办法 解决方法
			
phpStorm 软件打开运行提示 failed to create JVM的解决办法. 修改文件 D:\Program Files (x86)\JetBrains\PhpStorm 7.1.3\bi ...
 - 【Python】Django auth 修改密码如何实现?
			
使用示例1.创建用户>>> from django.contrib.auth.models import User>>> user = User.objects.c ...