将图像对象数组传递给 Fancybox(灯箱)似乎不起作用?
我试图让 fancybox 在灯箱中显示一系列图像。不幸的是,只显示第一张图片。据我所知,我应该能够将对象数组传递给 fancybox并让它将它们全部打开。
我的代码如下(请注意,我是从 CoffeeScript 即时翻译的,因此可能有一些错误)。
当我记录 images
数组时,它看起来像是一个完全正常的对象数组。在浏览器中导航到每个图像的 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(混合了一些 ruby):
<a href="<%= cake_path(cake) %>" class="lightbox">
<%= cake.primary_thumbnail %>
<h2>Chocolate Cake</h2>
</a>
另一个提示是,将选项 index: 2
传递给 fancybox 会破坏它(它应该导致脚本在序列中的第三个图像上打开)。我收到以下错误:
475Uncaught TypeError: Cannot read property 'nodeType' of null
有什么想法吗?
I'm trying to get fancybox to display a series of images in a lightbox. 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.
My code is below (note that I translated from coffeescript off the cuff so it might have some errors).
When I log the images
array it seems like a perfectly normal array of objects. Navigating to the url for each image in the browser displays the image perfectly, proving that the URLs are valid.
$(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):
<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). I get the following error:
475Uncaught TypeError: Cannot read property 'nodeType' of null
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的语法应该是:
所以我想你的构造函数应该返回每个数组元素,后跟一个逗号,除了最后一个:
然后我想你应该像这样调用fancybox:
The correct syntax should be:
so I guess your constructor should return every array element followed by a comma except the last one:
then I guess you should call fancybox like:
我在 Github 上打开了 一个问题,并被告知更新到 最新版本(比当前通过 FancyBox 网站上的下载链接提供的版本更新)。一旦我这样做了,问题就消失了。
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). The problem went away once I did that.