質問編集履歴
5
javascriptコピーし直し
test
CHANGED
File without changes
|
test
CHANGED
@@ -140,408 +140,376 @@
|
|
140
140
|
|
141
141
|
},
|
142
142
|
|
143
|
-
var initPhotoSwipeFromDOM = function
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
// parse slide data (url, title, size ...) from DOM elements
|
148
|
-
|
149
|
-
// (children of gallerySelector)
|
150
|
-
|
151
|
-
var parseThumbnailElements = function
|
152
|
-
|
153
|
-
var thumbElements = el.childNodes,
|
154
|
-
|
155
|
-
numNodes = thumbElements.length,
|
156
|
-
|
157
|
-
items = [],
|
158
|
-
|
159
|
-
figureEl,
|
160
|
-
|
161
|
-
linkEl,
|
162
|
-
|
163
|
-
size,
|
164
|
-
|
165
|
-
item;
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
for
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
figureEl = thumbElements[i]; // <figure> element
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
// include only element nodes
|
178
|
-
|
179
|
-
if
|
180
|
-
|
181
|
-
continue;
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
linkEl = figureEl.children[0]; // <a> element
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
size = linkEl.getAttribute('data-size').split('x');
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
// create slide object
|
196
|
-
|
197
|
-
item = {
|
198
|
-
|
199
|
-
src: linkEl.getAttribute('href'),
|
200
|
-
|
201
|
-
w: parseInt(size[0], 10),
|
202
|
-
|
203
|
-
h: parseInt(size[1], 10)
|
204
|
-
|
205
|
-
};
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
if
|
214
|
-
|
215
|
-
// <figcaption> content
|
216
|
-
|
217
|
-
item.title = figureEl.children[1].innerHTML;
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
if
|
224
|
-
|
225
|
-
// <img> thumbnail element, retrieving thumbnail url
|
226
|
-
|
227
|
-
item.msrc = linkEl.children[0].getAttribute('src');
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
item.el = figureEl; // save link to element for getThumbBoundsFn
|
234
|
-
|
235
|
-
items.push(item);
|
143
|
+
var initPhotoSwipeFromDOM = function( gallerySelector ) {
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// parse slide data (url, title, size ...) from DOM elements
|
148
|
+
|
149
|
+
// (children of gallerySelector)
|
150
|
+
|
151
|
+
var parseThumbnailElements = function(el) {
|
152
|
+
|
153
|
+
var thumbElements = el.childNodes,
|
154
|
+
|
155
|
+
numNodes = thumbElements.length,
|
156
|
+
|
157
|
+
items = [],
|
158
|
+
|
159
|
+
figureEl,
|
160
|
+
|
161
|
+
linkEl,
|
162
|
+
|
163
|
+
size,
|
164
|
+
|
165
|
+
item;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
for(var i = 0; i < numNodes; i++) {
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
figureEl = thumbElements[i]; // <figure> element
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// include only element nodes
|
178
|
+
|
179
|
+
if(figureEl.nodeType !== 1) {
|
180
|
+
|
181
|
+
continue;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
linkEl = figureEl.children[0]; // <a> element
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
size = linkEl.getAttribute('data-size').split('x');
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
// create slide object
|
196
|
+
|
197
|
+
item = {
|
198
|
+
|
199
|
+
src: linkEl.getAttribute('href'),
|
200
|
+
|
201
|
+
w: parseInt(size[0], 10),
|
202
|
+
|
203
|
+
h: parseInt(size[1], 10)
|
204
|
+
|
205
|
+
};
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
if(figureEl.children.length > 1) {
|
214
|
+
|
215
|
+
// <figcaption> content
|
216
|
+
|
217
|
+
item.title = figureEl.children[1].innerHTML;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if(linkEl.children.length > 0) {
|
224
|
+
|
225
|
+
// <img> thumbnail element, retrieving thumbnail url
|
226
|
+
|
227
|
+
item.msrc = linkEl.children[0].getAttribute('src');
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
item.el = figureEl; // save link to element for getThumbBoundsFn
|
234
|
+
|
235
|
+
items.push(item);
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
return items;
|
242
|
+
|
243
|
+
};
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// find nearest parent element
|
248
|
+
|
249
|
+
var closest = function closest(el, fn) {
|
250
|
+
|
251
|
+
return el && ( fn(el) ? el : closest(el.parentNode, fn) );
|
252
|
+
|
253
|
+
};
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// triggers when user clicks on thumbnail
|
258
|
+
|
259
|
+
var onThumbnailsClick = function(e) {
|
260
|
+
|
261
|
+
e = e || window.event;
|
262
|
+
|
263
|
+
e.preventDefault ? e.preventDefault() : e.returnValue = false;
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
var eTarget = e.target || e.srcElement;
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
// find root element of slide
|
272
|
+
|
273
|
+
var clickedListItem = closest(eTarget, function(el) {
|
274
|
+
|
275
|
+
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
|
276
|
+
|
277
|
+
});
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
if(!clickedListItem) {
|
282
|
+
|
283
|
+
return;
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
// find index of clicked item by looping through all child nodes
|
290
|
+
|
291
|
+
// alternatively, you may define index via data- attribute
|
292
|
+
|
293
|
+
var clickedGallery = clickedListItem.parentNode,
|
294
|
+
|
295
|
+
childNodes = clickedListItem.parentNode.childNodes,
|
296
|
+
|
297
|
+
numChildNodes = childNodes.length,
|
298
|
+
|
299
|
+
nodeIndex = 0,
|
300
|
+
|
301
|
+
index;
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
for (var i = 0; i < numChildNodes; i++) {
|
306
|
+
|
307
|
+
if(childNodes[i].nodeType !== 1) {
|
308
|
+
|
309
|
+
continue;
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
if(childNodes[i] === clickedListItem) {
|
316
|
+
|
317
|
+
index = nodeIndex;
|
318
|
+
|
319
|
+
break;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
nodeIndex++;
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
if(index >= 0) {
|
334
|
+
|
335
|
+
// open PhotoSwipe if valid index found
|
336
|
+
|
337
|
+
openPhotoSwipe( index, clickedGallery );
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
return false;
|
342
|
+
|
343
|
+
};
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
// parse picture index and gallery index from URL (#&pid=1&gid=2)
|
348
|
+
|
349
|
+
var photoswipeParseHash = function() {
|
350
|
+
|
351
|
+
var hash = window.location.hash.substring(1),
|
352
|
+
|
353
|
+
params = {};
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
if(hash.length < 5) {
|
358
|
+
|
359
|
+
return params;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
var vars = hash.split('&');
|
366
|
+
|
367
|
+
for (var i = 0; i < vars.length; i++) {
|
368
|
+
|
369
|
+
if(!vars[i]) {
|
370
|
+
|
371
|
+
continue;
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
var pair = vars[i].split('=');
|
376
|
+
|
377
|
+
if(pair.length < 2) {
|
378
|
+
|
379
|
+
continue;
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
params[pair[0]] = pair[1];
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
if(params.gid) {
|
390
|
+
|
391
|
+
params.gid = parseInt(params.gid, 10);
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
if(!params.hasOwnProperty('pid')) {
|
398
|
+
|
399
|
+
return params;
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
params.pid = parseInt(params.pid, 10);
|
404
|
+
|
405
|
+
return params;
|
406
|
+
|
407
|
+
};
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
var openPhotoSwipe = function(index, galleryElement, disableAnimation) {
|
412
|
+
|
413
|
+
var pswpElement = document.querySelectorAll('.pswp')[0],
|
414
|
+
|
415
|
+
gallery,
|
416
|
+
|
417
|
+
options,
|
418
|
+
|
419
|
+
items;
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
items = parseThumbnailElements(galleryElement);
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
// define options (if needed)
|
428
|
+
|
429
|
+
options = {
|
430
|
+
|
431
|
+
index: index,
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
// define gallery index (for URL)
|
436
|
+
|
437
|
+
galleryUID: galleryElement.getAttribute('data-pswp-uid'),
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
getThumbBoundsFn: function(index) {
|
442
|
+
|
443
|
+
// See Options -> getThumbBoundsFn section of documentation for more info
|
444
|
+
|
445
|
+
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
|
446
|
+
|
447
|
+
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
|
448
|
+
|
449
|
+
rect = thumbnail.getBoundingClientRect();
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
};
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
if(disableAnimation) {
|
464
|
+
|
465
|
+
options.showAnimationDuration = 0;
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
// Pass data to PhotoSwipe and initialize it
|
472
|
+
|
473
|
+
gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
|
474
|
+
|
475
|
+
gallery.init();
|
476
|
+
|
477
|
+
};
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
// loop through all gallery elements and bind events
|
482
|
+
|
483
|
+
var galleryElements = document.querySelectorAll( gallerySelector );
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
for(var i = 0, l = galleryElements.length; i < l; i++) {
|
488
|
+
|
489
|
+
galleryElements[i].setAttribute('data-pswp-uid', i+1);
|
490
|
+
|
491
|
+
galleryElements[i].onclick = onThumbnailsClick;
|
236
492
|
|
237
493
|
}
|
238
494
|
|
239
495
|
|
240
496
|
|
241
|
-
return items;
|
242
|
-
|
243
|
-
};
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
//
|
497
|
+
// Parse URL and open gallery if it contains #&pid=3&gid=1
|
248
|
-
|
498
|
+
|
249
|
-
var
|
499
|
+
var hashData = photoswipeParseHash();
|
500
|
+
|
250
|
-
|
501
|
+
if(hashData.pid > 0 && hashData.gid > 0) {
|
502
|
+
|
251
|
-
|
503
|
+
openPhotoSwipe( hashData.pid - 1 , galleryElements[ hashData.gid - 1 ], true );
|
252
|
-
|
253
|
-
};
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
// triggers when user clicks on thumbnail
|
258
|
-
|
259
|
-
var onThumbnailsClick = function (e) {
|
260
|
-
|
261
|
-
e = e || window.event;
|
262
|
-
|
263
|
-
e.preventDefault ? e.preventDefault() : e.returnValue = false;
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
var eTarget = e.target || e.srcElement;
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
// find root element of slide
|
272
|
-
|
273
|
-
var clickedListItem = closest(eTarget, function (el) {
|
274
|
-
|
275
|
-
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
|
276
|
-
|
277
|
-
});
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
if (!clickedListItem) {
|
282
|
-
|
283
|
-
return;
|
284
504
|
|
285
505
|
}
|
286
506
|
|
287
|
-
|
288
|
-
|
289
|
-
// find index of clicked item by looping through all child nodes
|
290
|
-
|
291
|
-
// alternatively, you may define index via data- attribute
|
292
|
-
|
293
|
-
var clickedGallery = clickedListItem.parentNode,
|
294
|
-
|
295
|
-
childNodes = clickedListItem.parentNode.childNodes,
|
296
|
-
|
297
|
-
numChildNodes = childNodes.length,
|
298
|
-
|
299
|
-
nodeIndex = 0,
|
300
|
-
|
301
|
-
index;
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
for (var i = 0; i < numChildNodes; i++) {
|
306
|
-
|
307
|
-
if (childNodes[i].nodeType !== 1) {
|
308
|
-
|
309
|
-
continue;
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
if (childNodes[i] === clickedListItem) {
|
316
|
-
|
317
|
-
index = nodeIndex;
|
318
|
-
|
319
|
-
break;
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
nodeIndex++;
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
if (index >= 0) {
|
334
|
-
|
335
|
-
// open PhotoSwipe if valid index found
|
336
|
-
|
337
|
-
openPhotoSwipe(index, clickedGallery);
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
return false;
|
342
|
-
|
343
|
-
};
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
// parse picture index and gallery index from URL (#&pid=1&gid=2)
|
348
|
-
|
349
|
-
var photoswipeParseHash = function () {
|
350
|
-
|
351
|
-
var hash = window.location.hash.substring(1),
|
352
|
-
|
353
|
-
params = {};
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
if (hash.length < 5) {
|
358
|
-
|
359
|
-
return params;
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
var vars = hash.split('&');
|
366
|
-
|
367
|
-
for (var i = 0; i < vars.length; i++) {
|
368
|
-
|
369
|
-
if (!vars[i]) {
|
370
|
-
|
371
|
-
continue;
|
372
|
-
|
373
|
-
}
|
374
|
-
|
375
|
-
var pair = vars[i].split('=');
|
376
|
-
|
377
|
-
if (pair.length < 2) {
|
378
|
-
|
379
|
-
continue;
|
380
|
-
|
381
|
-
}
|
382
|
-
|
383
|
-
params[pair[0]] = pair[1];
|
384
|
-
|
385
|
-
}
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
if (params.gid) {
|
390
|
-
|
391
|
-
params.gid = parseInt(params.gid, 10);
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
return params;
|
398
|
-
|
399
|
-
};
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
var openPhotoSwipe = function (index, galleryElement, disableAnimation, fromURL) {
|
404
|
-
|
405
|
-
var pswpElement = document.querySelectorAll('.pswp')[0],
|
406
|
-
|
407
|
-
gallery,
|
408
|
-
|
409
|
-
options,
|
410
|
-
|
411
|
-
items;
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
items = parseThumbnailElements(galleryElement);
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
// define options (if needed)
|
420
|
-
|
421
|
-
options = {
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
// define gallery index (for URL)
|
426
|
-
|
427
|
-
galleryUID: galleryElement.getAttribute('data-pswp-uid'),
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
getThumbBoundsFn: function (index) {
|
432
|
-
|
433
|
-
// See Options -> getThumbBoundsFn section of documentation for more info
|
434
|
-
|
435
|
-
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
|
436
|
-
|
437
|
-
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
|
438
|
-
|
439
|
-
rect = thumbnail.getBoundingClientRect();
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
return { x: rect.left, y: rect.top + pageYScroll, w: rect.width };
|
444
|
-
|
445
|
-
}
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
};
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
// PhotoSwipe opened from URL
|
454
|
-
|
455
|
-
if (fromURL) {
|
456
|
-
|
457
|
-
if (options.galleryPIDs) {
|
458
|
-
|
459
|
-
// parse real index when custom PIDs are used
|
460
|
-
|
461
|
-
// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
|
462
|
-
|
463
|
-
for (var j = 0; j < items.length; j++) {
|
464
|
-
|
465
|
-
if (items[j].pid == index) {
|
466
|
-
|
467
|
-
options.index = j;
|
468
|
-
|
469
|
-
break;
|
470
|
-
|
471
|
-
}
|
472
|
-
|
473
|
-
}
|
474
|
-
|
475
|
-
} else {
|
476
|
-
|
477
|
-
// in URL indexes start from 1
|
478
|
-
|
479
|
-
options.index = parseInt(index, 10) - 1;
|
480
|
-
|
481
|
-
}
|
482
|
-
|
483
|
-
} else {
|
484
|
-
|
485
|
-
options.index = parseInt(index, 10);
|
486
|
-
|
487
|
-
}
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
// exit if index not found
|
492
|
-
|
493
|
-
if (isNaN(options.index)) {
|
494
|
-
|
495
|
-
return;
|
496
|
-
|
497
|
-
}
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
if (disableAnimation) {
|
502
|
-
|
503
|
-
options.showAnimationDuration = 0;
|
504
|
-
|
505
|
-
}
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
// Pass data to PhotoSwipe and initialize it
|
510
|
-
|
511
|
-
gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
|
512
|
-
|
513
|
-
gallery.init();
|
514
|
-
|
515
|
-
};
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
// loop through all gallery elements and bind events
|
520
|
-
|
521
|
-
var galleryElements = document.querySelectorAll(gallerySelector);
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
for (var i = 0, l = galleryElements.length; i < l; i++) {
|
526
|
-
|
527
|
-
galleryElements[i].setAttribute('data-pswp-uid', i + 1);
|
528
|
-
|
529
|
-
galleryElements[i].onclick = onThumbnailsClick;
|
530
|
-
|
531
|
-
}
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
// Parse URL and open gallery if it contains #&pid=3&gid=1
|
536
|
-
|
537
|
-
var hashData = photoswipeParseHash();
|
538
|
-
|
539
|
-
if (hashData.pid && hashData.gid) {
|
540
|
-
|
541
|
-
openPhotoSwipe(hashData.pid, galleryElements[hashData.gid - 1], true, true);
|
542
|
-
|
543
|
-
}
|
544
|
-
|
545
507
|
};
|
546
508
|
|
509
|
+
|
510
|
+
|
511
|
+
// PhotoSwipeを起動する
|
512
|
+
|
513
|
+
initPhotoSwipeFromDOM( '.my-gallery' ) ;
|
514
|
+
|
547
515
|
```
|
4
javascript追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
どこをいじって消えてしまったのかわからず、。見た目だけならcssでdisplay:noneをけしたら出てきましたがスワイプしません。どのあたりを確認したら良いのか教えていただきたいです。
|
2
2
|
|
3
|
+
```ここに言語を入力
|
4
|
+
|
3
|
-
|
5
|
+
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
|
4
6
|
|
5
7
|
<div class="pswp__bg"></div>
|
6
8
|
|
@@ -138,9 +140,365 @@
|
|
138
140
|
|
139
141
|
},
|
140
142
|
|
141
|
-
|
143
|
+
var initPhotoSwipeFromDOM = function (gallerySelector) {
|
144
|
+
|
145
|
+
|
146
|
+
|
142
|
-
|
147
|
+
// parse slide data (url, title, size ...) from DOM elements
|
148
|
+
|
149
|
+
// (children of gallerySelector)
|
150
|
+
|
151
|
+
var parseThumbnailElements = function (el) {
|
152
|
+
|
153
|
+
var thumbElements = el.childNodes,
|
154
|
+
|
155
|
+
numNodes = thumbElements.length,
|
156
|
+
|
157
|
+
items = [],
|
158
|
+
|
159
|
+
figureEl,
|
160
|
+
|
161
|
+
linkEl,
|
162
|
+
|
163
|
+
size,
|
164
|
+
|
165
|
+
item;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
for (var i = 0; i < numNodes; i++) {
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
figureEl = thumbElements[i]; // <figure> element
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// include only element nodes
|
178
|
+
|
179
|
+
if (figureEl.nodeType !== 1) {
|
180
|
+
|
181
|
+
continue;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
linkEl = figureEl.children[0]; // <a> element
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
size = linkEl.getAttribute('data-size').split('x');
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
// create slide object
|
196
|
+
|
197
|
+
item = {
|
198
|
+
|
199
|
+
src: linkEl.getAttribute('href'),
|
200
|
+
|
201
|
+
w: parseInt(size[0], 10),
|
202
|
+
|
203
|
+
h: parseInt(size[1], 10)
|
204
|
+
|
205
|
+
};
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
if (figureEl.children.length > 1) {
|
214
|
+
|
215
|
+
// <figcaption> content
|
216
|
+
|
217
|
+
item.title = figureEl.children[1].innerHTML;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if (linkEl.children.length > 0) {
|
224
|
+
|
225
|
+
// <img> thumbnail element, retrieving thumbnail url
|
226
|
+
|
227
|
+
item.msrc = linkEl.children[0].getAttribute('src');
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
item.el = figureEl; // save link to element for getThumbBoundsFn
|
234
|
+
|
235
|
+
items.push(item);
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
return items;
|
242
|
+
|
243
|
+
};
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
// find nearest parent element
|
248
|
+
|
249
|
+
var closest = function closest(el, fn) {
|
250
|
+
|
251
|
+
return el && (fn(el) ? el : closest(el.parentNode, fn));
|
252
|
+
|
253
|
+
};
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// triggers when user clicks on thumbnail
|
258
|
+
|
259
|
+
var onThumbnailsClick = function (e) {
|
260
|
+
|
261
|
+
e = e || window.event;
|
262
|
+
|
263
|
+
e.preventDefault ? e.preventDefault() : e.returnValue = false;
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
var eTarget = e.target || e.srcElement;
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
// find root element of slide
|
272
|
+
|
273
|
+
var clickedListItem = closest(eTarget, function (el) {
|
274
|
+
|
275
|
+
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
|
276
|
+
|
277
|
+
});
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
if (!clickedListItem) {
|
282
|
+
|
283
|
+
return;
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
// find index of clicked item by looping through all child nodes
|
290
|
+
|
291
|
+
// alternatively, you may define index via data- attribute
|
292
|
+
|
293
|
+
var clickedGallery = clickedListItem.parentNode,
|
294
|
+
|
295
|
+
childNodes = clickedListItem.parentNode.childNodes,
|
296
|
+
|
297
|
+
numChildNodes = childNodes.length,
|
298
|
+
|
299
|
+
nodeIndex = 0,
|
300
|
+
|
301
|
+
index;
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
for (var i = 0; i < numChildNodes; i++) {
|
306
|
+
|
307
|
+
if (childNodes[i].nodeType !== 1) {
|
308
|
+
|
309
|
+
continue;
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
if (childNodes[i] === clickedListItem) {
|
316
|
+
|
317
|
+
index = nodeIndex;
|
318
|
+
|
319
|
+
break;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
nodeIndex++;
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
if (index >= 0) {
|
334
|
+
|
335
|
+
// open PhotoSwipe if valid index found
|
336
|
+
|
337
|
+
openPhotoSwipe(index, clickedGallery);
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
return false;
|
342
|
+
|
343
|
+
};
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
// parse picture index and gallery index from URL (#&pid=1&gid=2)
|
348
|
+
|
349
|
+
var photoswipeParseHash = function () {
|
350
|
+
|
351
|
+
var hash = window.location.hash.substring(1),
|
352
|
+
|
353
|
+
params = {};
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
if (hash.length < 5) {
|
358
|
+
|
359
|
+
return params;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
var vars = hash.split('&');
|
366
|
+
|
367
|
+
for (var i = 0; i < vars.length; i++) {
|
368
|
+
|
369
|
+
if (!vars[i]) {
|
370
|
+
|
371
|
+
continue;
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
var pair = vars[i].split('=');
|
376
|
+
|
377
|
+
if (pair.length < 2) {
|
378
|
+
|
379
|
+
continue;
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
params[pair[0]] = pair[1];
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
if (params.gid) {
|
390
|
+
|
391
|
+
params.gid = parseInt(params.gid, 10);
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
return params;
|
398
|
+
|
399
|
+
};
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
var openPhotoSwipe = function (index, galleryElement, disableAnimation, fromURL) {
|
404
|
+
|
405
|
+
var pswpElement = document.querySelectorAll('.pswp')[0],
|
406
|
+
|
407
|
+
gallery,
|
408
|
+
|
409
|
+
options,
|
410
|
+
|
411
|
+
items;
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
items = parseThumbnailElements(galleryElement);
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
// define options (if needed)
|
420
|
+
|
421
|
+
options = {
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
// define gallery index (for URL)
|
426
|
+
|
427
|
+
galleryUID: galleryElement.getAttribute('data-pswp-uid'),
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
getThumbBoundsFn: function (index) {
|
432
|
+
|
433
|
+
// See Options -> getThumbBoundsFn section of documentation for more info
|
434
|
+
|
435
|
+
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
|
436
|
+
|
437
|
+
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
|
438
|
+
|
439
|
+
rect = thumbnail.getBoundingClientRect();
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
return { x: rect.left, y: rect.top + pageYScroll, w: rect.width };
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
};
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
// PhotoSwipe opened from URL
|
454
|
+
|
455
|
+
if (fromURL) {
|
456
|
+
|
457
|
+
if (options.galleryPIDs) {
|
458
|
+
|
459
|
+
// parse real index when custom PIDs are used
|
460
|
+
|
461
|
+
// http://photoswipe.com/documentation/faq.html#custom-pid-in-url
|
462
|
+
|
463
|
+
for (var j = 0; j < items.length; j++) {
|
464
|
+
|
465
|
+
if (items[j].pid == index) {
|
466
|
+
|
467
|
+
options.index = j;
|
468
|
+
|
469
|
+
break;
|
470
|
+
|
471
|
+
}
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
} else {
|
476
|
+
|
477
|
+
// in URL indexes start from 1
|
478
|
+
|
479
|
+
options.index = parseInt(index, 10) - 1;
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
} else {
|
484
|
+
|
485
|
+
options.index = parseInt(index, 10);
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
// exit if index not found
|
492
|
+
|
493
|
+
if (isNaN(options.index)) {
|
494
|
+
|
495
|
+
return;
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
|
500
|
+
|
143
|
-
|
501
|
+
if (disableAnimation) {
|
144
502
|
|
145
503
|
options.showAnimationDuration = 0;
|
146
504
|
|
@@ -184,6 +542,6 @@
|
|
184
542
|
|
185
543
|
}
|
186
544
|
|
187
|
-
};
|
545
|
+
};
|
188
546
|
|
189
547
|
```
|
3
javascript追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -138,9 +138,9 @@
|
|
138
138
|
|
139
139
|
},
|
140
140
|
|
141
|
-
``````ここに言語を入力
|
142
141
|
|
142
|
+
|
143
|
-
if (disableAnimation) {
|
143
|
+
``if (disableAnimation) {
|
144
144
|
|
145
145
|
options.showAnimationDuration = 0;
|
146
146
|
|
2
javascript追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -138,4 +138,52 @@
|
|
138
138
|
|
139
139
|
},
|
140
140
|
|
141
|
+
``````ここに言語を入力
|
142
|
+
|
143
|
+
if (disableAnimation) {
|
144
|
+
|
145
|
+
options.showAnimationDuration = 0;
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
// Pass data to PhotoSwipe and initialize it
|
152
|
+
|
153
|
+
gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
|
154
|
+
|
155
|
+
gallery.init();
|
156
|
+
|
157
|
+
};
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
// loop through all gallery elements and bind events
|
162
|
+
|
163
|
+
var galleryElements = document.querySelectorAll(gallerySelector);
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
for (var i = 0, l = galleryElements.length; i < l; i++) {
|
168
|
+
|
169
|
+
galleryElements[i].setAttribute('data-pswp-uid', i + 1);
|
170
|
+
|
171
|
+
galleryElements[i].onclick = onThumbnailsClick;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
// Parse URL and open gallery if it contains #&pid=3&gid=1
|
178
|
+
|
179
|
+
var hashData = photoswipeParseHash();
|
180
|
+
|
181
|
+
if (hashData.pid && hashData.gid) {
|
182
|
+
|
183
|
+
openPhotoSwipe(hashData.pid, galleryElements[hashData.gid - 1], true, true);
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
};```
|
188
|
+
|
141
189
|
```
|
1
すみません、直しました!
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
どこをいじって消えてしまったのかわからず、。見た目だけならcssでdisplay:noneをけしたら出てきましたがスワイプしません。どのあたりを確認したら良いのか教えていただきたいです。
|
2
2
|
|
3
|
-
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
|
3
|
+
```<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
|
4
4
|
|
5
5
|
<div class="pswp__bg"></div>
|
6
6
|
|
@@ -136,8 +136,6 @@
|
|
136
136
|
|
137
137
|
onTap: pswp.next
|
138
138
|
|
139
|
-
},ここに言語を入力
|
140
|
-
|
141
|
-
|
139
|
+
},
|
142
140
|
|
143
141
|
```
|