I need to have every two of images in gallery embed with extra <div>
.
I managed Photoswipe to use all images in that gallery. But I can’t fix problem: when I click on any thumb (not first or second one), Photoswipe every time opens only first or second image from firs <div>
.
JavaScript
x
214
214
1
var initPhotoSwipeFromDOM = function(gallerySelector) {
2
3
// parse slide data (url, title, size ...) from DOM elements
4
// (children of gallerySelector)
5
var parseThumbnailElements = function(el) { var thumbElements = Array.prototype.slice.call(document.querySelectorAll('.img-item')),
6
numNodes = thumbElements.length,
7
items = [],
8
figureEl,
9
linkEl,
10
size,
11
item;
12
13
for(var i = 0; i < numNodes; i++) {
14
15
figureEl = thumbElements[i]; // <figure> element
16
17
// include only element nodes
18
if(figureEl.nodeType !== 1) {
19
continue;
20
}
21
22
linkEl = figureEl.children[0]; // <a> element
23
24
size = linkEl.getAttribute('data-size').split('x');
25
26
// create slide object
27
item = {
28
src: linkEl.getAttribute('href'),
29
w: parseInt(size[0], 10),
30
h: parseInt(size[1], 10)
31
};
32
33
34
35
if(figureEl.children.length > 1) {
36
// <figcaption> content
37
item.title = figureEl.children[1].innerHTML;
38
}
39
40
if(linkEl.children.length > 0) {
41
// <img> thumbnail element, retrieving thumbnail url
42
item.msrc = linkEl.children[0].getAttribute('src');
43
}
44
45
item.el = figureEl; // save link to element for getThumbBoundsFn
46
items.push(item);
47
}
48
49
return items;
50
};
51
52
// find nearest parent element
53
var closest = function closest(el, fn) {
54
return el && ( fn(el) ? el : closest(el.parentNode, fn) );
55
};
56
57
// triggers when user clicks on thumbnail
58
var onThumbnailsClick = function(e) {
59
e = e || window.event;
60
e.preventDefault ? e.preventDefault() : e.returnValue = false;
61
62
var eTarget = e.target || e.srcElement;
63
64
// find root element of slide
65
var clickedListItem = closest(eTarget, function(el) {
66
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
67
});
68
69
if(!clickedListItem) {
70
return;
71
}
72
73
// find index of clicked item by looping through all child nodes
74
// alternatively, you may define index via data- attribute
75
var clickedGallery = document.querySelectorAll('.my-gallery')[0], childNodes = Array.prototype.slice.call(document.querySelectorAll('.img-item')),
76
childNodes = clickedListItem.parentNode.childNodes,
77
numChildNodes = childNodes.length,
78
nodeIndex = 0,
79
index;
80
81
for (var i = 0; i < numChildNodes; i++) {
82
if(childNodes[i].nodeType !== 1) {
83
continue;
84
}
85
86
if(childNodes[i] === clickedListItem) {
87
index = nodeIndex;
88
break;
89
}
90
nodeIndex++;
91
}
92
93
94
95
if(index >= 0) {
96
// open PhotoSwipe if valid index found
97
openPhotoSwipe( index, clickedGallery );
98
}
99
return false;
100
};
101
102
// parse picture index and gallery index from URL (#&pid=1&gid=2)
103
var photoswipeParseHash = function() {
104
var hash = window.location.hash.substring(1),
105
params = {};
106
107
if(hash.length < 5) {
108
return params;
109
}
110
111
var vars = hash.split('&');
112
for (var i = 0; i < vars.length; i++) {
113
if(!vars[i]) {
114
continue;
115
}
116
var pair = vars[i].split('=');
117
if(pair.length < 2) {
118
continue;
119
}
120
params[pair[0]] = pair[1];
121
}
122
123
if(params.gid) {
124
params.gid = parseInt(params.gid, 10);
125
}
126
127
return params;
128
};
129
130
var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
131
var pswpElement = document.querySelectorAll('.pswp')[0],
132
gallery,
133
options,
134
items;
135
136
items = parseThumbnailElements(galleryElement);
137
138
// define options (if needed)
139
options = {
140
fullscreenEl: false,
141
zoomEl: false,
142
//counterEl: false,
143
closeElClasses: ['item', 'caption', 'zoom-wrap', 'ui', 'top-bar'],
144
shareButtons: [
145
{id:'facebook', label:'Share on Facebook', url:'https://www.facebook.com/sharer/sharer.php?u={{url}}'},
146
{id:'twitter', label:'Tweet', url:'https://twitter.com/intent/tweet?text={{text}}&url={{url}}'},
147
{id:'pinterest', label:'Pin it', url:'http://www.pinterest.com/pin/create/button/?url={{url}}&media={{image_url}}&description={{text}}'}
148
],
149
150
// define gallery index (for URL)
151
galleryUID: galleryElement.getAttribute('data-pswp-uid'),
152
153
getThumbBoundsFn: function(index) {
154
// See Options -> getThumbBoundsFn section of documentation for more info
155
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
156
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
157
rect = thumbnail.getBoundingClientRect();
158
159
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
160
}
161
162
};
163
164
// PhotoSwipe opened from URL
165
if(fromURL) {
166
if(options.galleryPIDs) {
167
// parse real index when custom PIDs are used
168
// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
169
for(var j = 0; j < items.length; j++) {
170
if(items[j].pid == index) {
171
options.index = j;
172
break;
173
}
174
}
175
} else {
176
// in URL indexes start from 1
177
options.index = parseInt(index, 10) - 1;
178
}
179
} else {
180
options.index = parseInt(index, 10);
181
}
182
183
// exit if index not found
184
if( isNaN(options.index) ) {
185
return;
186
}
187
188
if(disableAnimation) {
189
options.showAnimationDuration = 0;
190
}
191
192
// Pass data to PhotoSwipe and initialize it
193
gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
194
gallery.init();
195
};
196
197
// loop through all gallery elements and bind events
198
var galleryElements = document.querySelectorAll( gallerySelector );
199
200
for(var i = 0, l = galleryElements.length; i < l; i++) {
201
galleryElements[i].setAttribute('data-pswp-uid', i+1);
202
galleryElements[i].onclick = onThumbnailsClick;
203
}
204
205
// Parse URL and open gallery if it contains #&pid=3&gid=1
206
var hashData = photoswipeParseHash();
207
if(hashData.pid && hashData.gid) {
208
openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );
209
}
210
};
211
212
// execute above function
213
initPhotoSwipeFromDOM('.my-gallery');
214
Thank you for any advice on solving this.
Advertisement
Answer
I modified the default Photoswipe Codepen Example to work with open Images at a custom Index. Here is the Codepen Example how to open a Custom Picture on Event Click
JavaScript
1
107
107
1
<button id="btn">Open PhotoSwipe</button>
2
3
<!-- Root element of PhotoSwipe. Must have class pswp. -->
4
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
5
6
<!-- Background of PhotoSwipe.
7
It's a separate element, as animating opacity is faster than rgba(). -->
8
<div class="pswp__bg"></div>
9
10
<!-- Slides wrapper with overflow:hidden. -->
11
<div class="pswp__scroll-wrap">
12
13
<!-- Container that holds slides. PhotoSwipe keeps only 3 slides in DOM to save memory. -->
14
<div class="pswp__container">
15
<!-- don't modify these 3 pswp__item elements, data is added later on -->
16
<div class="pswp__item"></div>
17
<div class="pswp__item"></div>
18
<div class="pswp__item"></div>
19
</div>
20
21
<!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
22
<div class="pswp__ui pswp__ui--hidden">
23
24
<div class="pswp__top-bar">
25
26
<!-- Controls are self-explanatory. Order can be changed. -->
27
28
<div class="pswp__counter"></div>
29
30
<button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
31
32
<button class="pswp__button pswp__button--share" title="Share"></button>
33
34
<button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
35
36
<button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
37
38
<!-- Preloader demo https://codepen.io/dimsemenov/pen/yyBWoR -->
39
<!-- element will get class pswp__preloader--active when preloader is running -->
40
<div class="pswp__preloader">
41
<div class="pswp__preloader__icn">
42
<div class="pswp__preloader__cut">
43
<div class="pswp__preloader__donut"></div>
44
</div>
45
</div>
46
</div>
47
</div>
48
49
<div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
50
<div class="pswp__share-tooltip"></div>
51
</div>
52
53
<button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
54
</button>
55
56
<button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
57
</button>
58
59
<div class="pswp__caption">
60
<div class="pswp__caption__center"></div>
61
</div>
62
63
</div>
64
65
</div>
66
67
</div>
68
69
var openPhotoSwipe = function(p_index) {
70
var pswpElement = document.querySelectorAll('.pswp')[0];
71
72
// build items array
73
var items = [
74
{
75
src: 'https://farm2.staticflickr.com/1043/5186867718_06b2e9e551_b.jpg',
76
w: 964,
77
h: 1024
78
},
79
{
80
src: 'https://farm7.staticflickr.com/6175/6176698785_7dee72237e_b.jpg',
81
w: 1024,
82
h: 683
83
}
84
];
85
86
// define options (if needed)
87
var options = {
88
// history & focus options are disabled on CodePen
89
history: false,
90
focus: false,
91
92
showAnimationDuration: 0,
93
hideAnimationDuration: 0,
94
index: p_index
95
96
};
97
98
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
99
gallery.init();
100
};
101
102
openPhotoSwipe(0);
103
104
document.getElementById('btn').onclick = function() {
105
openPhotoSwipe(1);
106
}
107
With the Method
JavaScript
1
2
1
openPhotoSwipe(1);
2
you can open the Picture with the Index you want.