【发布时间】:2018-05-09 02:58:00
【问题描述】:
我有一个由单张图片和照片集组成的图片库。对于后者,我添加了额外的 div 以将它们并排放置。下面是我的 HTML:
<div class="container">
<article>
<figure>
<div data-layout="1212" id="justified">
<div class="photoset-row photoset-row-1" style="overflow: hidden;">
<div class="photoset-cell" style="display:inline-block;overflow:hidden;vertical-align:top;width:100%;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;"><img src="https://unsplash.it/1200/900/?image=702" data-width="1200" data-height="900" style="width: 100%; height: auto; display: block; opacity: 1;"></div>
</div>
<div class="photoset-row photoset-row-2" style="margin-top: 3px; overflow: hidden;">
<div class="photoset-cell" style="display: inline-block; overflow: hidden; vertical-align: top; width: 50%; box-sizing: border-box; padding-right: 1.5px;"><img src="https://unsplash.it/1200/900/?image=695" data-width="1200" data-height="900" style="width: 100%; height: auto; display: block; opacity: 1;"></div>
<div class="photoset-cell" style="display: inline-block; overflow: hidden; vertical-align: top; width: 50%; box-sizing: border-box; padding-left: 1.5px; float:right;"><img src="https://unsplash.it/1200/900/?image=675" data-width="1200" data-height="900" style="width: 100%; height: auto; display: block; opacity: 1;"></div>
</div>
</div>
</article>
</figure>
<article>
<figure class="post post--photo" role="img">
<img class="post__img" src="https://unsplash.it/1200/900/?image=511" data-width="1200" data-height="900">
</figure>
</article>
<article>
<figure class="post post--photo" role="img">
<img class="post__img" src="https://unsplash.it/1200/900/?image=514" data-width="1200" data-height="900">
</figure>
</article>
然后我使用 jQuery 为 Photoswipe 创建一个数组,如下所示:
'use strict';
/* global jQuery, PhotoSwipe, PhotoSwipeUI_Default, console */
(function($) {
// Init empty gallery array
var container = [];
// Loop over gallery items and push it to the array
$('article').find('figure, .photoset-cell').each(function() {
var $link = $(this).find('img, video'),
item = {
src: $link.attr('src'),
w: $link.data('width'),
h: $link.data('height'),
};
container.push(item);
}),
// Define click event on gallery item
$('img, video').click(function(event) {
// Prevent location change
event.preventDefault();
// Define object and gallery options
var $pswp = $('.pswp')[0],
options = {
index: $(this).closest('figure, .photoset-cell').index('figure, .photoset-cell'),
bgOpacity: 0.9,
showHideOpacity: true,
};
// Initialize PhotoSwipe
var gallery = new PhotoSwipe($pswp, PhotoSwipeUI_Default, container, options);
gallery.init();
});
}(jQuery));
一切都很好,除了一件事:photoset 数组中的第一个法师被添加到数组中两次。我怎样才能防止这种情况发生?
您可以在此处查看演示:https://codepen.io/anon/pen/MOprem
【问题讨论】:
-
我想
new PhotoSwipe正在为你添加第一张图片。 -
@Nick 谢谢。这就是初始化Photoswipe的原因,所以我不确定是否可以修改。想法?
标签: javascript jquery photoswipe