使用javascript实现Iframe自适应高度
作者:bea
方法一: 代码如下: $(window.parent.document).find("#ContentIframe").load(function() { var main = $(window.parent.document).find("#ContentIframe"); var thisheight = $(document).height(); if (thisheight < 800) thisheight = 800; main.h
方法一:
代码如下:
$(window.parent.document).find("#ContentIframe").load(function() {
var main = $(window.parent.document).find("#ContentIframe");
var thisheight = $(document).height();
if (thisheight < 800)
thisheight = 800;
main.height(thisheight);
});
这种写法,只能对于加载的时候对固有元素的自适应高度,当元素变化的时候(如添加了很多元素,高度变化后)不能及时改变父窗体的iframe高度。
方法二:
代码如下:
function setMainHeight() {
var main = $(window.parent.document).find("#ContentIframe"); var thisheight = $("body").height(); if (thisheight < 800) { thisheight = 800; } main.height(thisheight+50); setTimeout(setMainHeight, 2000); }
添加一个计时器,轮询判断子页面的高度变化。
以上2种都可以是iframe实现高度自适应,小伙伴们根据自己的项目需求,自由选择吧
有用 | 无用
代码如下:
$(window.parent.document).find("#ContentIframe").load(function() {
var main = $(window.parent.document).find("#ContentIframe");
var thisheight = $(document).height();
if (thisheight < 800)
thisheight = 800;
main.height(thisheight);
});
这种写法,只能对于加载的时候对固有元素的自适应高度,当元素变化的时候(如添加了很多元素,高度变化后)不能及时改变父窗体的iframe高度。
方法二:
代码如下:
function setMainHeight() {
var main = $(window.parent.document).find("#ContentIframe"); var thisheight = $("body").height(); if (thisheight < 800) { thisheight = 800; } main.height(thisheight+50); setTimeout(setMainHeight, 2000); }
添加一个计时器,轮询判断子页面的高度变化。
以上2种都可以是iframe实现高度自适应,小伙伴们根据自己的项目需求,自由选择吧
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- jQuery中after()方法用法实例
- jQuery中prepend()方法用法实例
- jQuery中append()方法用法实例
- 使用mini-define实现前端代码的模块化管理
- jQuery中html()方法用法实例
- jQuery中val()方法用法实例
- 24款热门实用的jQuery插件推荐
- js获取当前时间显示在页面上并每秒刷新
- Javascript 多物体运动的实现
- Javascript 运动中Offset的bug解决方案
- jQuery中height()方法用法实例
- jQuery中width()方法用法实例
- jQuery中animate()方法用法实例
- jQuery中fadeOut()方法用法实例
- 创建js对象和js类的方法汇总
- javascript使用prototype完成单继承
- jQuery中slideUp()方法用法分析
- node.js开机自启动脚本文件
- 使用jquery动态加载js文件的方法