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

本文介绍了如何在Vue3中通过<iframe>标签引用本地文件,并利用JavaScript监听load事件,实时根据iframe内容高度调整其大小,确保用户体验的流畅性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值