Vue3 iframe引用本地文件以及根据内容调整iframe高度

Vue3 iframe引用本地文件以及根据内容调整iframe高度

  1. 首先在public文件夹下新建文件夹static,将需要引入的html页面放入进去
  2. template写引入iframe
  <iframe src="static/word/index.html"  id="testFrame" name="myiframe" frameborder="0" width="100%" scrolling="no" style="min-height: 500px;" ></iframe>
  1. setup里计算iframe高度
 onMounted(()=>{
    
    
//使用JavaScript监听iframe元素的load事件,然后根据iframe中document的高度来动态调整iframe窗口的高度。
   var testFrame = document.getElementById('testFrame');
    testFrame.addEventListener('load', function() {
    
    
        testFrame.height = getHeight(testFrame.contentDocument);
    });
    function getHeight(doc) {
    
    
        var body = doc.body,
            html = doc.documentElement;

        var height = Math.max( body.scrollHeight, body.offsetHeight,
            html.clientHeight, html.scrollHeight, html.offsetHeight );
        return height;
    }
})

猜你喜欢

转载自blog.csdn.net/qq_37656005/article/details/123480986
今日推荐