简体   繁体   English

将图像对象数组传递给Fancybox(灯箱)似乎不起作用?

[英]Passing an array of image objects to Fancybox (lightbox) doesn't seem to work?

I'm trying to get fancybox to display a series of images in a lightbox. 我正在尝试让fancybox在灯箱中显示一系列图像。 Unfortunately, only the first image is showing up. 不幸的是,只有第一张图像出现。 As far as I can tell from the docs , I should be able to pass an array of objects to fancybox and have it open them all. 据我从文档中得知,我应该能够将对象数组传递给fancybox并使其全部打开。

My code is below (note that I translated from coffeescript off the cuff so it might have some errors). 我的代码在下面(请注意,我从袖带上翻译了coffeescript,所以可能会有一些错误)。

When I log the images array it seems like a perfectly normal array of objects. 当我记录images数组时,它看起来像是一个完全正常的对象数组。 Navigating to the url for each image in the browser displays the image perfectly, proving that the URLs are valid. 导航到浏览器中每个图像的url,可以完美显示该图像,证明URL是有效的。

$(function(){
  $('.lightbox').click(function(event){
    event.preventDefault();
    // need to be able to get the link target
    $link = $(event.target).closest('a');

    $.getJSON($link.attr('href'), function(cake){
      // map the json to an array that fancybox can use
      images = $(cake.images).map(function(key, image)){
        return({ href: image.url });
      });

      console.log(images);
      // => [{href: 'image1.jpg'},{href: 'image2.jpg'},{href: 'image3.jpg'}]

      // show the lightbox
      $.fancybox.open(images);
    });
  });
});

HTML (with some ruby mixed in): HTML(混入一些红宝石):

<a href="<%= cake_path(cake) %>" class="lightbox">
  <%= cake.primary_thumbnail %>
  <h2>Chocolate Cake</h2>
</a>

One other hint is that passing the option index: 2 to fancybox breaks it (it should cause the script to open on the third image in the sequence). 另一个提示是,将选项index: 2传递给fancybox会使其破裂(这将导致脚本在序列中的第三个图像上打开)。 I get the following error: 我收到以下错误:

475Uncaught TypeError: Cannot read property 'nodeType' of null

Any ideas? 有任何想法吗?

The correct syntax should be: 正确的语法应为:

$.fancybox([ 
 {href : 'img1.jpg', title : 'Title'}, 
 {href : 'img2.jpg', title : 'Title'},
 {href : 'img3.jpg', title : 'Title'}  
 ],[
 {
 // API options here
 }
]);

so I guess your constructor should return every array element followed by a comma except the last one: 所以我想你的构造函数应该返回每个数组元素,后跟一个逗号,最后一个除外:

{ href: image.url },

then I guess you should call fancybox like: 那我猜你应该这样称呼fancybox:

$.fancybox.open([images]);

I opened an issue on Github and was told to update to the latest version (which is newer than the version currently available via the download link on the FancyBox website). 在Github上打开了一个问题 ,并被告知要更新到最新版本 (比通过FancyBox网站上的下载链接当前提供的版本新)。 The problem went away once I did that. 一旦这样做,问题就消失了。

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

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