iframe 高度设置、iframe 自适应高度方法总结
我要评论2014/04/07 13:50:35 来源:绿色资源网编辑:www.downcc.com 评论:0点击:310次
谓iframe自适应高度,就是,基于界面美观和交互的考虑,隐藏了iframe的border和scrollbar,让人看不出它是个iframe。如果iframe始终调用同一个固定高度的页面,我们直接写死iframe高度就可以了。而如果iframe要切换页面,或者被包含页面要做DOM动态操作,这时候,就需要程序去同步iframe高度和被包含页的实际高度了。
如果iframe的高度没有确定,那将是初始的高度。
iframe是网页中的一部分,其大小还要受到网页的限制,设置最高可以使用height="100%"。
基本上解决iframe超出的高度都是增加了滚动条来实现的,很简单,如果你iframe中的信息超出了一屏幕,你就必须使用滚动条了。
开始用的时候还不行,后来发现是因为js跨域问题,没有权限。后来设置了window.document.domain 就可以了,用的是jquery代码2方法。
跨域下的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;
})();
< /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的同源限制)
超级简单的方法,也不用写什么判断浏览器高度、宽度啥的。
下面的两种方法自选其一就行了。一个是放在和iframe同页面的,一个是放在test.html页面的。
注意别放错地方了哦。
下面是其他两种方法:
iframe代码,注意要写ID
< iframe src="test.html" id="main" width="700" height="300" frameborder="0" scrolling="auto"></iframe>
jquery代码1:
//注意:下面的代码是放在test.html调用
$(window.parent.document).find("#main").load(function(){
var main = $(window.parent.document).find("#main");
var thisheight = $(document).height()+30;
main.height(thisheight);
});
jquery代码2:
//注意:下面的代码是放在和iframe同一个页面调用
$("#main").load(function(){
var mainheight = $(this).contents().find("body").height()+30;
$(this).height(mainheight);
});
第二种有效,不过要注意一点是,增加的JS要写在iframe下面,放在头部是测试没有效果。
测试代码:
< iframe id="mainframe" name="mainframe" marginwidth="0" marginheight="0" src="/Home/Activitylist" frameborder="0" width="100%" scrolling="no" height="100%"></iframe>
< script type="text/javascript">
//注意:下面的代码是放在和iframe同一个页面调用,放在iframe下面
$("#mainframe").load(function () {
var mainheight = $(this).contents().find("body").height() + 30;
$(this).height(mainheight);
});
< /script>
关键词:iframe
相关阅读
- 09-28iframe自适应高度的多种方法方法小结
- 07-18MySql5.0 Table错误:is marked as crashed and last (automatic?) repair failed
- 06-25最简单的方法去掉iframe滚动条
- 06-25JS实现刷新iframe的方法
- 09-08用JS防止网站被挂IFRAME木马
- 08-21怎样设置WinRAR让碎文件不占C盘空间
- 06-13解析平板电脑通过Wifi上网的设置
- 05-27Oracle数据库如何查找删除重复的SQL语句
- 01-01用 iframe 解决下拉框与层之冲突
- 04-25Windows服务器下查IIS被挂iframe木马
阅读本文后您有什么感想? 已有 人给出评价!
用户评论
热门评论
最新评论
相关软件
热点图文
- 05-31CSS中如何让元素隐藏?
- 04-19网页设计配色应用教程之各种色彩对比分析
- 04-07IE与Firefox的CSS兼容大全
- 01-10div+css下js浮动对联广告不随屏幕滚动的解决方法
- 04-16marquee基本语法的全解释
- 04-07CSS的学习应该注意学习方法
- 04-16创建一个纯CSS的水平导航条
- 04-07网页框架和网页框架集如何工作的
- 05-25喜欢在DreamWeaver中编写CSS代码的朋友应该了解的几个习惯
- 02-12修改一般的ipad屏幕布局CSS代码