javascript - 使用 davidjbradshaw 的 iframe-resizer 库

标签 javascript html iframe iframe-resizer

我是 JavaScript 新手,在使用 iframe-resizer 库时遇到问题。

我的 iFrame 有一个 Angular 组件 HTML 文件,其中包含以下代码。如您所见,我包含了 iframeResizer.min.js 文件,并且使用 srcdoc 动态构建 iFrame 的内容(我在此框架内容中包含了 iframeResizer.contentWindow.min.js 文件):

<script type='text/javascript' src='../iframeResizer.min.js'></script>

<iframe id="myIframe" srcdoc="<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?<p>On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.</p><script type='text/javascript' src='../iframeResizer.contentWindow.min.js'></script>" scrolling="no"></iframe>

<script>iFrameResize({log:true}, '#myIframe')</script>

我的组件还有一个 scss 文件,其中包含以下内容:

iframe {
    width: 1px;
    min-width: 100%;
}

我没有看到我的 iFrame 大小被调整,也没有在控制台中看到任何 iframe-resizer 日志。什么也没发生。知道为什么会发生这种情况吗?我究竟做错了什么?我是否需要一个 Angular 指令来公开必要的变量和行为——如果是这样,你有一个例子吗?很抱歉提出了菜鸟问题,并提前感谢您的帮助。

最佳答案

FWIW,当我必须在 Angular 中做同样的事情时,我使用此处的方法创建一个指令以应用于 iframe 以添加加载处理程序:

https://stackoverflow.com/a/23948165/2079345

然后在加载处理程序中我执行了

        let $frame=$('#the-iframe');
        setInterval(function () {
            let height = parseInt($frame.contents().find('body').height());
            if (height !== oldHeight) {
                $frame.height(height + 30);
                oldHeight = height;
            }
        })

+ 30 用于解决填充问题,您的用例可能会有所不同

关于javascript - 使用 davidjbradshaw 的 iframe-resizer 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559606/

相关文章:

javascript - 从 Javascript 在 Textarea 中搜索链接

iframe - 在 iframe 中嵌入 Office Online(Word、Excel、Powerpoint)

Javascript循环返回字符串x次

javascript - 可通过表单提交进行内容编辑

html - &lt;header&gt; 和 <main> 之间无法解释的差距

javascript - 从ngResource获取数据,然后绑定(bind)result到view

asp.net - 安装更新 KB3154​​070 后,IE 中未加载 iframe

html - 格式化 html 表格内容以用于 iframe

javascript - 为什么这个素数检查器有效?

javascript - 如何将 JSON 数组从 php 传递到 JS?