简体   繁体   English

使用davidjbradshaw的iframe-resizer库

[英]using davidjbradshaw's iframe-resizer library

I'm new to JavaScript and am having issues using the iframe-resizer library. 我是JavaScript新手,在使用iframe-resizer库时遇到问题。

I have an Angular component HTML file for my iFrame which contains the code below. 我的iFrame有一个Angular组件HTML文件,其中包含以下代码。 As you can see, I'm including the iframeResizer.min.js file, and I'm building the content of my iFrame on the fly using srcdoc (I include the iframeResizer.contentWindow.min.js file inside this framed content): 如您所见,我包含了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> 

I also have an scss file for my component which contains the following: 我的组件也有一个scss文件,其中包含以下内容:

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

I'm not seeing my iFrame being resized, nor am I seeing any iframe-resizer logs in the console. 我没有看到调整iFrame的大小,也没有看到控制台中的任何iframe调整程序日志。 Nothing is happening at all. 什么都没有发生。 Any idea why this might be happening? 知道为什么会这样吗? What am I doing wrong? 我究竟做错了什么? Do I need an Angular directive to expose necessary variables and behavior -- if so, do you have an example of that? 我是否需要一个Angular指令来公开必要的变量和行为-如果是这样,您是否有一个示例? Sorry for the noob questions and thanks in advance for your help. 很抱歉出现菜鸟问题,在此先感谢您的帮助。

FWIW, when I had to do this same thing in angular, I used the method here to create a directive to apply to iframes to add a load handler: FWIW,当我不得不做角度相同的事情时,我使用这里的方法创建了一个指令,以应用于iframe以添加负载处理程序:

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

and then in the load handler I did 然后在加载处理程序中

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

The plus 30 was for padding issues, your use case may differ 加号30用于填充问题,您的用例可能会有所不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM