iframe自适应高度 iframe
2018-01-08 17:52
309 查看
本以为在前端道路上不会用到iframe,毕竟html5已经废弃了。但是项目中还是遇到了,于是各种百度开始啦
下面是我百度到的代码:
然后在我写的时候这还没有完,因为iframe的高度和之前一样,并没有自适应,后来我找到了src中引用的页面 ,显式的获取页面文档高度 给body元素
document.body.offsetHeight
这样就可以啦..
但是 由于rc中引用的页面存在异步, 上面的方法需要放到ajax 成功的回调之后
由于本人的项目 那个页面有多个ajax封装的方法, 所以索性 更改代码:
iframe嵌进去的页面是不可点击的,设置一下样式就可以
嗯 就这样,之后再优化
今天没事了 改了一下 把setTimeout改成了setInterval,代码如下:
下面是我百度到的代码:
<iframe id="iframe" scrolling="no" frameborder="0" style="width:540px;text-align: center;" src="aaa.html?shop_id=26"></iframe> var ifm = document.getElementById("iframe"); ifm.onload = function() { var subWeb = document.frames ? document.frames["iframe"].document : ifm.contentDocument; if (ifm != null && subWeb != null) { ifm.height = subWeb.body.scrollHeight + 44; alert(ifm.height) } }
然后在我写的时候这还没有完,因为iframe的高度和之前一样,并没有自适应,后来我找到了src中引用的页面 ,显式的获取页面文档高度 给body元素
document.body.offsetHeight
这样就可以啦..
但是 由于rc中引用的页面存在异步, 上面的方法需要放到ajax 成功的回调之后
由于本人的项目 那个页面有多个ajax封装的方法, 所以索性 更改代码:
var ifm = document.getElementById("iframe"); ifm.onload = function() { var subWeb = document.frames ? document.frames["iframe"].document : ifm.contentDocument; setTimeout(function() { if (ifm != null && subWeb != null) { ifm.height = subWeb.body.scrollHeight + 44; alert(ifm.height) } }, 4000) }
iframe嵌进去的页面是不可点击的,设置一下样式就可以
<style> iframe{ pointer-events: none; } </style>
嗯 就这样,之后再优化
今天没事了 改了一下 把setTimeout改成了setInterval,代码如下:
var ifm = document.getElementById("iframe"); ifm.onload = function() { var subWeb = document.frames ? document.frames["iframe"].document : ifm.contentDocument; var heightArray = []; var interval = setInterval(function() { if (ifm != null && subWeb != null) { ifm.height = subWeb.body.scrollHeight + 44; } heightArray.push(ifm.height); if(heightArray.length > 1) { if(heightArray[heightArray.length-2] === heightArray[heightArray.length-1]) { clearInterval(interval) } } }, 1200) }
相关文章推荐
- (转)iFrame高度自适应
- javascript原生和jquery库实现iframe自适应高度和宽度
- iframe 子页面自适应高度问题
- html iframe高度自适应
- iframe高度自适应、载入完成事件
- iframe 高度宽度自适应
- js获取iframe和父级之间元素,方法、属,获取iframe的高度自适应iframe高度
- 跨域实现IFRAME自适应高度
- 跨域iframe高度自适应(兼容IE/FF/OP/Chrome)
- iframe自适应加载的页面高度
- JavaScript设置IFrame高度自适应(兼容多浏览器)
- iframe 自适应高度的问题(jquery控制)
- 跨域情况下Iframe高度自适应解决方案
- IFRAME自适应高度
- iframe高度自适应问题
- iframe自适应高度
- iframe 自适应高度,更改内容时iframe高度怎么根据新的内容高度自动改变
- 去iframe的边框,自适应高度等iframe知识
- iframe标签用法详解(属性、透明、自适应高度)
- iframe 去除边框和自适应高度