Javascript 将Fancybox iframe大小设置为适合内容图像

Javascript 将Fancybox iframe大小设置为适合内容图像,javascript,fancybox-2,Javascript,Fancybox 2,我尝试了很多选择,但仍然不适合尺寸。 在调试模式下,当运行到“后加载”该行时,程序将立即退出 我的代码: $('.fancybox').fancybox({ fitToView: false, autoSize: false, type: 'iframe', afterLoad: function() { $('.fancybox-iframe').load(function() { $('.fancybox-inner').height( $

我尝试了很多选择,但仍然不适合尺寸。 在调试模式下,当运行到“后加载”该行时,程序将立即退出

我的代码:

$('.fancybox').fancybox({
  fitToView: false,
  autoSize: false,
  type: 'iframe',
  afterLoad: function() {
    $('.fancybox-iframe').load(function() {
      $('.fancybox-inner').height(
        $(this).contents().find('body').height()
      );
      $('.fancybox-inner').width(
        $(this).contents().find('body').width()
      );
    });
  }
});
更新: 我找到了解决方案,虽然不完美,但几乎完美。在Firefox中工作错误(太小),但在Chrome中工作正确:

afterLoad: function() {
  this.width = this.content[0].contentDocument.images[0].width;
  this.height = this.content[0].contentDocument.images[0].height;
}
afterLoad: function() {
  this.content[0].contentDocument.images[0].style = ('width: auto; height: auto;');
  this.width = this.content[0].contentDocument.images[0].width;
  this.height = this.content[0].contentDocument.images[0].height;
}

我找到了解决方案,虽然不完美,但几乎完美。在Firefox中工作错误(太小),但在Chrome中工作正确:

afterLoad: function() {
  this.width = this.content[0].contentDocument.images[0].width;
  this.height = this.content[0].contentDocument.images[0].height;
}
afterLoad: function() {
  this.content[0].contentDocument.images[0].style = ('width: auto; height: auto;');
  this.width = this.content[0].contentDocument.images[0].width;
  this.height = this.content[0].contentDocument.images[0].height;
}

如果你想让它与firefox一起工作,那么就使用
.naturalWidth
.naturalHeight

afterLoad: function() {
    this.content[0].contentDocument.images[0].style = ('width: auto; height: auto;');
    this.width = this.content[0].contentDocument.images[0].naturalWidth;
    this.height = this.content[0].contentDocument.images[0].naturalHeight;
}
而不是
.width
.height

afterLoad: function() {
    this.content[0].contentDocument.images[0].style = ('width: auto; height: auto;');
    this.width = this.content[0].contentDocument.images[0].naturalWidth;
    this.height = this.content[0].contentDocument.images[0].naturalHeight;
}