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

时间:2015-04-22 04:03:33

标签: javascript fancybox-2

我尝试了很多选项,但仍然无法适应大小。 在调试模式下,当运行到“afterLoad”那一行时,程序立即退出。

我的代码:

$('.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;
}

2 个答案:

答案 0 :(得分:0)

我找到了解决方案,但不是完美而是差不多。错误的工作在Firefox(太小),但在Chrome中:

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;
}

答案 1 :(得分:0)

如果您想使其与Firefox一起使用,请使用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

而不是public class MainClass { public static void main(String[] args) { sendFileToGcs(); } private static void sendFileToGcs() { GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder() .initialRetryDelayMillis(10) .retryMaxAttempts(10) .totalRetryPeriodMillis(15000) .build()); try{ GcsFileOptions instance = GcsFileOptions.getDefaultInstance(); GcsFilename fileName = new GcsFilename("my-bucket", "myFile"); GcsOutputChannel outputChannel; outputChannel = gcsService.createOrReplace(fileName, instance); File file = new File("audio.mp3"); FileInputStream fis = new FileInputStream(file); InputStream is = fis; copy(is, Channels.newOutputStream(outputChannel)); fis.close(); }catch(IOException e){} while(true){} } private static void copy(InputStream input, OutputStream output) throws IOException { final int BUFFER_SIZE = 2 * 1024 * 1024; try { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = input.read(buffer); while (bytesRead != -1) { output.write(buffer, 0, bytesRead); bytesRead = input.read(buffer); } } finally { input.close(); output.close(); } } } > Aug 02, 2016 3:41:01 PM > com.google.appengine.tools.cloudstorage.RetryHelper doRetry INFO: > RetryHelper(14.86 ms, 1 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #1 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 12 ms Aug 02, 2016 > 3:41:01 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(59.97 ms, 2 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #2 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 16 ms Aug 02, 2016 > 3:41:01 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(77.05 ms, 3 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #3 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 32 ms Aug 02, 2016 > 3:41:01 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(110.4 ms, 4 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #4 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 82 ms Aug 02, 2016 > 3:41:01 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(193.5 ms, 5 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #5 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 159 ms Aug 02, 2016 > 3:41:01 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(353.7 ms, 6 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #6 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 394 ms Aug 02, 2016 > 3:41:01 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(750.2 ms, 7 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #7 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 640 ms Aug 02, 2016 > 3:41:02 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(1.393 s, 8 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #8 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 1547 ms Aug 02, 2016 > 3:41:04 PM com.google.appengine.tools.cloudstorage.RetryHelper doRetry > INFO: RetryHelper(2.941 s, 9 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Attempt #9 failed [java.io.IOException: > java.lang.NullPointerException], sleeping for 2694 ms Exception in > thread "main" > com.google.appengine.tools.cloudstorage.RetriesExhaustedException: > RetryHelper(5.637 s, 10 attempts, > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1@1de0aca6): > Too many failures, giving up at > com.google.appengine.tools.cloudstorage.RetryHelper.doRetry(RetryHelper.java:127) > at > com.google.appengine.tools.cloudstorage.RetryHelper.runWithRetries(RetryHelper.java:166) > at > com.google.appengine.tools.cloudstorage.RetryHelper.runWithRetries(RetryHelper.java:156) > at > com.google.appengine.tools.cloudstorage.GcsServiceImpl.createOrReplace(GcsServiceImpl.java:70) > at MainClass.sendApkToFirebase(MainClass.java:33) at > MainClass.main(MainClass.java:19) Caused by: java.io.IOException: > java.lang.NullPointerException at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService$BlobStorageAdapter.getInstance(LocalRawGcsService.java:186) > at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService$BlobStorageAdapter.access$000(LocalRawGcsService.java:109) > at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.ensureInitialized(LocalRawGcsService.java:194) > at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.beginObjectCreation(LocalRawGcsService.java:249) > at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService.beginObjectCreation(LocalRawGcsService.java:92) > at > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1.call(GcsServiceImpl.java:74) > at > com.google.appengine.tools.cloudstorage.GcsServiceImpl$1.call(GcsServiceImpl.java:70) > at > com.google.appengine.tools.cloudstorage.RetryHelper.doRetry(RetryHelper.java:108) > ... 5 more Caused by: java.lang.NullPointerException at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService$BlobStorageAdapter.<init>(LocalRawGcsService.java:123) > at > com.google.appengine.tools.cloudstorage.dev.LocalRawGcsService$BlobStorageAdapter.getInstance(LocalRawGcsService.java:184) > ... 12 more