简体   繁体   English

使用jQuery触发iframe加载

[英]Trigger on iframe load with jQuery

I've got an issue with Disqus on a Tumblr theme at present. 我目前在Tumblr主题上与Disqus有关的问题。

The theme itself uses Isotope (if you're not familiar it's essentially a more featureful, paid version of Masonry), and has Disqus integration. 主题本身使用同位素(如果您不熟悉,它本质上是功能更强大的付费石工版本),并且具有Disqus集成。

The issue is that the client wants us to place the Disqus comments inside the same div as the individual posts. 问题是客户希望我们将Disqus评论添加到与各个帖子相同的div中。 Unfortunately, Isotope and Disqus don't play well together - what happens is that Isotope lays out the page before the iFrame that Disqus loads in has finished loading, mucking up the layout in the process. 不幸的是,Isotope和Disqus不能很好地配合使用-发生的事情是Isotope在Disqus加载的iFrame完成加载之前对页面进行了布局,从而在此过程中造成了布局混乱。 I can quite easily re-layout the page by running $('#isotope').isotope('reLayout'); 我可以通过运行$('#isotope').isotope('reLayout');轻松地重新布局页面$('#isotope').isotope('reLayout'); , but I'm having trouble finding a good way of triggering it. ,但我很难找到触发它的好方法。

I need to trigger it once all the iframes on the page have finished loading. 页面上的所有iframe完成加载后,我需要触发它。 Disqus uses JavaScript to insert the iframe elements dynamically, so I thought using the following would do the trick: Disqus使用JavaScript来动态插入iframe元素,所以我认为使用以下方法可以解决问题:

$('body').on('load', 'iframe', function () {
    // Do stuff here
});

However, this is never triggered. 但是,这永远不会触发。

I've put together a jsFiddle demonstrating the kind of thing I want to be able to do. 我整理了一个jsFiddle,演示了我想做的事情。

Can anyone see where I went awry? 谁能看到我去哪里了?

If the iframe is from an external domain then you can't do that due to cross-domain rules (you won't be able to bind to certain events of the iframe) 如果iframe来自外部域,则由于跨域规则,您将无法执行此操作(您将无法绑定到iframe的某些事件)

However as far as your layout issue goes, if you designed your layout such that the iframe was always a fixed size with a defined width and height then the iframe won't affect the layout as the content loads (although I'm surprised it does in the first place, I didn't think iframes resized themselves) 但是,就布局问题而言,如果您设计布局时使iframe始终是具有定义的宽度和高度的固定大小,则iframe不会随着内容加载而影响布局(尽管我很惊讶首先,我不认为iframe会自行调整大小)

I was having same issue. 我有同样的问题。 So I figured that whenever iframe is loaded it increases the height of its parent element, ie its wrapper, and we can detect when an element is resized , Here is what I did. 因此,我发现无论何时加载iframe,它都会增加其父元素(即包装器)的高度,并且we can detect when an element is resized ,这就是我所做的。

$("your_ifram_wrap").on("resize",function() {
      var $this = $(this);
      if($this.find("iframe").length && $isotopeSelector.length){
           $isotopeSelector.isotope('layout');
      }
});

PS: I used http://benalman.com/projects/jquery-resize-plugin/ to detect resize. PS:我使用http://benalman.com/projects/jquery-resize-plugin/来检测调整大小。

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

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