简体   繁体   English

防止将同一图像两次添加到阵列

[英]Prevent same image from being added twice to array

I have an image gallery composed of single images as well as photosets. 我有一个包含单个图像和照片集的图像库。 For the latter, I add additional divs to position them side by side. 对于后者,我添加了其他div以使其并排放置。 Below is my HTML: 以下是我的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>

I then use jQuery to create an array for Photoswipe as follows: 然后,我使用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));

Everything works fine except for one thing: the first mage in the photoset array gets added twice to the array. 除了一件事情外,其他所有东西都工作正常:将photoset数组中的第一个法师添加到数组两次。 How can I prevent this from happening? 如何防止这种情况发生?

You can see a demo here: https://codepen.io/anon/pen/MOprem 您可以在此处查看演示: https : //codepen.io/anon/pen/MOprem

Your problem is that your photoset-cell divs are enclosed in a <figure> element. 您的问题是您的photoset-cell div包含在<figure>元素中。 So this code: 所以这段代码:

$('article').find('figure, .photoset-cell')

finds that <figure> element and pushes the first image inside it ie 找到<figure>元素并将其内的第一个图像压入

<img src="https://unsplash.it/1200/900/?image=702"

then that first photoset-cell (the one with image 702) is found and the image inside it is pushed again. 然后找到第一个photoset-cell (带有图像702),并再次推送其中的图像。 Hence you get two copies of the first image in your gallery. 因此,您将获得画廊中第一张图片的两份副本。

The easiest fix is to remove the <figure> element around the photoset-cell divs. 最简单的解决方法是删除photoset-cell div周围的<figure>元素。 If that's not possible for layout reasons, then based on the code you have supplied, the easiest fix is probably to change this line: 如果由于布局原因而无法实现,则根据您提供的代码,最简单的解决方法可能是更改此行:

$('article').find('figure, .photoset-cell').each(function() {

to: 至:

$('article').find('figure.post, .photoset-cell').each(function() {

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

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