iframe实现高度自适应小程序web-view方案

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

iframe实现高度自适应小程序web-view方案

念念不忘   2023-02-03 我要评论

前言

最近在项目开发中,遇到一种场景:H5的页面展示的有服务端返回的页面展示,所以考虑的iframe 引入对应的页面展示,在浏览器是可以正常设置 iframe 的高度,可以正常展示,但是在小程序的的 web-view 展示是不生效的。

关于 contentWindow, contentDocument

定义和用法

用iframe嵌套页面时,如果父页面要获取子页面里面的内容,可以使用contentWindow或者contentDocument,其区别如下:

contentWindow:是用来获取iframe的window对象的,只读属性,兼容各大浏览器,用法如下:

document.getElementById("iframeId").contentWindow 

这样简单的一句就得到了iframe包含页面的window对象;

contentDocument:是用来获取子窗口的document对象的,主流浏览器都支持和ie8+支持,用法如下

document.getElementById("iframeId").contentDocument

这样简单的一句就得到了iframe包含页面的document对象;

以上两种方法是在父窗口中获取到子窗口,既然我们都能拿到子窗口window对象和document对象了,那么子窗口内其他的操作就easy了 !

  contentDocument 属性能够以 HTML 对象来返回 iframe 中的文档, 可以通过所有标准的 DOM 方法来处理被返回的对象。

  语法:frameObject.contentWindow,或者 iframeObject.contentWindow(不是jquery对象)

  兼容获取document对象:

var getIFrameDoc = function(){ 
    var iobj = document.createElement("iframe"); 
    document.getElementsByTagName("body")[0].appendChild(iobj); 
    return iobj.contentDocument || iobj.contentWindow.document; 
}

基本使用:

1、document.getElementById("myiframe").contentWindow,得到iframe对象后,就可以通过contentWindow得到iframe包含页面的window对象,然后就可以正常访问页面元素了;

2、$("#myiframe")[0].contentWindow,jquery选择器获得iframe,先把jquery对象转换为DOM对象,或者使用get()方法转换;

3、("#myiframe")[0].contentWindow.("#dd").val(),可以在得到iframe的window对象后接着使用jquery选择器进行页面操作;

4、 $("#myiframe")[0].contentWindow.username="zhangsan" ; 可以通过这种方式向iframe页面传递参数,在iframe页面window.username就可以获取到值,username是自定义的全局变量;

5、在iframe页面通过parent可以获得主页面的window,接着就可以正常访问父亲页面的元素了;

6、parent.$("#frame_A")[0].contentWindow.document.getElmentById("#frame_B"); 同级iframe页面之间调用,需要先得到父亲的window,然后调用同级的iframe得到window进行操作;

    //在子级iframe设置 父级 iframe ,或 孙级 iframe 高度。
 function showIframeH(){ 
    var parentWin = parent.document.getElementById("test"); 
    if(!parentWin) return false; 
    var sub = parentWin.contentWindow.document.getElementById("test2"); 
    if(!sub) return false; 
    var thirdHeight = sub.contentWindow.document.body.offsetHeight; //第三层 body 对象
    sub.height = thirdHeight; //设置第二层 iframe 的高度
    var secondHeight = parentWin .contentWindow.document.body.offsetHeight; //第二层 body 对象
     parentWin .height = secondHeight; //设置第一层 iframe 的高度
}
  • 一、在使用iframe的页面时,要操作这个iframe里面的DOM元素可以用:contentWindow、contentDocument

1、先获取iframe里面的window对象,再通过这个对象,获取到里面的DOM元素

例子:

var ifr = document.getElementById("iframe"); ifr.contentWindow.document.getElementById("XXXXX") <iframe src="a.html" id=""></iframe>

  ifr.contentWindow 这里,返回的是iframe的window对象,所以后面可以接着调用document方法,再接着调用getElementByTagName。那么就可以对iframe里面的元素进行操作了。

  • 二、在iframe本页面,要操作这个iframe的父页面的DOM元素(即嵌套这个iframe的页面)可以用:

  window.parent、window.top(这里的TOP是获取的顶层,即有多层嵌套iframe的时候使用)

var ifr = document.getElementByTagName("iframe"); ifr.parent.document.getElementById("XXXXX") <iframe src="a.html" id=""></iframe>

解决方案

使用 Vue 写的项目,原生的可以直接使用 window.onload = function(){}

    <iframe
    ref="iframe"
    frameborder="no"
    scrolling="auto"
    class="iframe-wrap"
    id="iframeWrap"
    :src="context_url"
    :height="loadHeight"
    @load.once="setIframeHeight">
    </iframe>
    setIframeHeight(){
      const ifm= document.getElementById('iframeWrap');   
      try {
        const bodyDom = ifm.contentWindow || ifm.contentDocument.parentWindow;
        const height = bodyDom.document.body.scrollHeight || bodyDom.document.documentElement.scrollHeight
        this.loadHeight = height
      } catch (error) {
        console.log(error)
      } 
    },

注意:本地开发时,若存在src跨域问题,高度自适应不起作用,打包部署之后高度会自适应。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们