質問編集履歴

4

リンクをコードで囲みました。

2017/01/04 16:26

投稿

araE
araE

スコア7

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- 実際にPhotoSwipeを設置したページです→http://web-box.jp/arai/index.html
13
+ 実際にPhotoSwipeを設置したページです→[http://web-box.jp/arai/index.html](http://web-box.jp/arai/index.html)
14
14
 
15
15
 
16
16
 
@@ -18,11 +18,11 @@
18
18
 
19
19
 
20
20
 
21
- http://web-box.jp/arai/photoswipe.html(photoswipe.jsのコード)
21
+ [http://web-box.jp/arai/photoswipe.html](http://web-box.jp/arai/photoswipe.html)(photoswipe.jsのコード)
22
22
 
23
- http://web-box.jp/arai/photoswipe-ui-default.html(photoswipe-ui-defaultのコード)
23
+ [http://web-box.jp/arai/photoswipe-ui-default.html](http://web-box.jp/arai/photoswipe-ui-default.html)(photoswipe-ui-defaultのコード)
24
24
 
25
- http://web-box.jp/arai/ps.html(ps.jsのコード/公式サイトhttp://photoswipe.com/documentation/getting-started.htmlからコピペしたもの)
25
+ [http://web-box.jp/arai/ps.html](http://web-box.jp/arai/ps.html)(ps.jsのコード/公式サイト[http://photoswipe.com/documentation/getting-started.html](http://photoswipe.com/documentation/getting-started.html)からコピペしたもの)
26
26
 
27
27
 
28
28
 
@@ -38,9 +38,9 @@
38
38
 
39
39
  他にCSSファイルがあります。
40
40
 
41
- http://web-box.jp/arai/dist/photoswipe.css
41
+ [http://web-box.jp/arai/dist/photoswipe.css](http://web-box.jp/arai/dist/photoswipe.css)
42
42
 
43
- http://web-box.jp/arai/dist/skin/skin.css
43
+ [http://web-box.jp/arai/dist/skin/skin.css](http://web-box.jp/arai/dist/skin/skin.css)
44
44
 
45
45
 
46
46
 

3

URLを貼りました。

2017/01/04 16:26

投稿

araE
araE

スコア7

test CHANGED
File without changes
test CHANGED
@@ -10,423 +10,19 @@
10
10
 
11
11
 
12
12
 
13
- ###該当のソースコード
14
-
15
- 公式サイトからコピペしたものをjsファイルにしました。
16
-
17
- ```ここに言語を入力
18
-
19
- var initPhotoSwipeFromDOM = function(gallerySelector) {
13
+ 実際にPhotoSwipeを設置したページです→http://web-box.jp/arai/index.html
20
14
 
21
15
 
22
16
 
23
- // parse slide data (url, title, size ...) from DOM elements
24
-
25
- // (children of gallerySelector)
26
-
27
- var parseThumbnailElements = function(el) {
28
-
29
- var thumbElements = el.childNodes,
30
-
31
- numNodes = thumbElements.length,
32
-
33
- items = [],
17
+ ###該当のソースコード
34
-
35
- figureEl,
36
-
37
- linkEl,
38
-
39
- size,
40
-
41
- item;
42
18
 
43
19
 
44
20
 
45
- for(var i = 0; i < numNodes; i++) {
21
+ http://web-box.jp/arai/photoswipe.html(photoswipe.jsのコード)
46
22
 
23
+ http://web-box.jp/arai/photoswipe-ui-default.html(photoswipe-ui-defaultのコード)
47
24
 
48
-
49
- figureEl = thumbElements[i]; // <figure> element
50
-
51
-
52
-
53
- // include only element nodes
54
-
55
- if(figureEl.nodeType !== 1) {
56
-
57
- continue;
58
-
59
- }
60
-
61
-
62
-
63
- linkEl = figureEl.children[0]; // <a> element
64
-
65
-
66
-
67
- size = linkEl.getAttribute('data-size').split('x');
68
-
69
-
70
-
71
- // create slide object
72
-
73
- item = {
74
-
75
- src: linkEl.getAttribute('href'),
76
-
77
- w: parseInt(size[0], 10),
78
-
79
- h: parseInt(size[1], 10)
80
-
81
- };
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
- if(figureEl.children.length > 1) {
90
-
91
- // <figcaption> content
92
-
93
- item.title = figureEl.children[1].innerHTML;
94
-
95
- }
96
-
97
-
98
-
99
- if(linkEl.children.length > 0) {
100
-
101
- // <img> thumbnail element, retrieving thumbnail url
102
-
103
- item.msrc = linkEl.children[0].getAttribute('src');
104
-
105
- }
106
-
107
-
108
-
109
- item.el = figureEl; // save link to element for getThumbBoundsFn
110
-
111
- items.push(item);
112
-
113
- }
114
-
115
-
116
-
117
- return items;
118
-
119
- };
120
-
121
-
122
-
123
- // find nearest parent element
124
-
125
- var closest = function closest(el, fn) {
126
-
127
- return el && ( fn(el) ? el : closest(el.parentNode, fn) );
128
-
129
- };
130
-
131
-
132
-
133
- // triggers when user clicks on thumbnail
134
-
135
- var onThumbnailsClick = function(e) {
136
-
137
- e = e || window.event;
138
-
139
- e.preventDefault ? e.preventDefault() : e.returnValue = false;
140
-
141
-
142
-
143
- var eTarget = e.target || e.srcElement;
144
-
145
-
146
-
147
- // find root element of slide
148
-
149
- var clickedListItem = closest(eTarget, function(el) {
150
-
151
- return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
152
-
153
- });
154
-
155
-
156
-
157
- if(!clickedListItem) {
158
-
159
- return;
160
-
161
- }
162
-
163
-
164
-
165
- // find index of clicked item by looping through all child nodes
166
-
167
- // alternatively, you may define index via data- attribute
168
-
169
- var clickedGallery = clickedListItem.parentNode,
170
-
171
- childNodes = clickedListItem.parentNode.childNodes,
172
-
173
- numChildNodes = childNodes.length,
174
-
175
- nodeIndex = 0,
176
-
177
- index;
178
-
179
-
180
-
181
- for (var i = 0; i < numChildNodes; i++) {
182
-
183
- if(childNodes[i].nodeType !== 1) {
184
-
185
- continue;
186
-
187
- }
188
-
189
-
190
-
191
- if(childNodes[i] === clickedListItem) {
192
-
193
- index = nodeIndex;
194
-
195
- break;
196
-
197
- }
198
-
199
- nodeIndex++;
200
-
201
- }
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
- if(index >= 0) {
210
-
211
- // open PhotoSwipe if valid index found
212
-
213
- openPhotoSwipe( index, clickedGallery );
214
-
215
- }
216
-
217
- return false;
218
-
219
- };
220
-
221
-
222
-
223
- // parse picture index and gallery index from URL (#&pid=1&gid=2)
224
-
225
- var photoswipeParseHash = function() {
226
-
227
- var hash = window.location.hash.substring(1),
228
-
229
- params = {};
230
-
231
-
232
-
233
- if(hash.length < 5) {
234
-
235
- return params;
236
-
237
- }
238
-
239
-
240
-
241
- var vars = hash.split('&');
242
-
243
- for (var i = 0; i < vars.length; i++) {
244
-
245
- if(!vars[i]) {
246
-
247
- continue;
248
-
249
- }
250
-
251
- var pair = vars[i].split('=');
252
-
253
- if(pair.length < 2) {
254
-
255
- continue;
256
-
257
- }
258
-
259
- params[pair[0]] = pair[1];
260
-
261
- }
262
-
263
-
264
-
265
- if(params.gid) {
266
-
267
- params.gid = parseInt(params.gid, 10);
268
-
269
- }
270
-
271
-
272
-
273
- return params;
274
-
275
- };
276
-
277
-
278
-
279
- var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
280
-
281
- var pswpElement = document.querySelectorAll('.pswp')[0],
282
-
283
- gallery,
284
-
285
- options,
286
-
287
- items;
288
-
289
-
290
-
291
- items = parseThumbnailElements(galleryElement);
292
-
293
-
294
-
295
- // define options (if needed)
296
-
297
- options = {
298
-
299
-
300
-
301
- // define gallery index (for URL)
302
-
303
- galleryUID: galleryElement.getAttribute('data-pswp-uid'),
304
-
305
-
306
-
307
- getThumbBoundsFn: function(index) {
308
-
309
- // See Options -> getThumbBoundsFn section of documentation for more info
310
-
311
- var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
312
-
313
- pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
314
-
315
- rect = thumbnail.getBoundingClientRect();
316
-
317
-
318
-
319
- return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
320
-
321
- }
322
-
323
-
324
-
325
- };
326
-
327
-
328
-
329
- // PhotoSwipe opened from URL
330
-
331
- if(fromURL) {
332
-
333
- if(options.galleryPIDs) {
334
-
335
- // parse real index when custom PIDs are used
336
-
337
- // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
25
+ http://web-box.jp/arai/ps.html(ps.jsのコード/公式サイトhttp://photoswipe.com/documentation/getting-started.htmlからコピペしたもの)
338
-
339
- for(var j = 0; j < items.length; j++) {
340
-
341
- if(items[j].pid == index) {
342
-
343
- options.index = j;
344
-
345
- break;
346
-
347
- }
348
-
349
- }
350
-
351
- } else {
352
-
353
- // in URL indexes start from 1
354
-
355
- options.index = parseInt(index, 10) - 1;
356
-
357
- }
358
-
359
- } else {
360
-
361
- options.index = parseInt(index, 10);
362
-
363
- }
364
-
365
-
366
-
367
- // exit if index not found
368
-
369
- if( isNaN(options.index) ) {
370
-
371
- return;
372
-
373
- }
374
-
375
-
376
-
377
- if(disableAnimation) {
378
-
379
- options.showAnimationDuration = 0;
380
-
381
- }
382
-
383
-
384
-
385
- // Pass data to PhotoSwipe and initialize it
386
-
387
- gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
388
-
389
- gallery.init();
390
-
391
- };
392
-
393
-
394
-
395
- // loop through all gallery elements and bind events
396
-
397
- var galleryElements = document.querySelectorAll( gallerySelector );
398
-
399
-
400
-
401
- for(var i = 0, l = galleryElements.length; i < l; i++) {
402
-
403
- galleryElements[i].setAttribute('data-pswp-uid', i+1);
404
-
405
- galleryElements[i].onclick = onThumbnailsClick;
406
-
407
- }
408
-
409
-
410
-
411
- // Parse URL and open gallery if it contains #&pid=3&gid=1
412
-
413
- var hashData = photoswipeParseHash();
414
-
415
- if(hashData.pid && hashData.gid) {
416
-
417
- openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );
418
-
419
- }
420
-
421
- };
422
-
423
-
424
-
425
- // execute above function
426
-
427
- initPhotoSwipeFromDOM('.my-gallery');
428
-
429
- ```
430
26
 
431
27
 
432
28
 
@@ -440,17 +36,11 @@
440
36
 
441
37
  ###補足情報(言語/FW/ツール等のバージョンなど)
442
38
 
443
- dist
39
+ 他にCSSァイがあります。
444
40
 
445
- ・上記コピペファイル.js
41
+ http://web-box.jp/arai/dist/photoswipe.css
446
42
 
447
- ・photoswipe.js
448
-
449
- ・photoswipe-ui-default.js
43
+ http://web-box.jp/arai/dist/skin/skin.css
450
-
451
- ・他CSSファイル
452
-
453
- があります。
454
44
 
455
45
 
456
46
 

2

見づらいのでminファイルを削りました。

2017/01/04 16:22

投稿

araE
araE

スコア7

test CHANGED
File without changes
test CHANGED
@@ -446,12 +446,8 @@
446
446
 
447
447
  ・photoswipe.js
448
448
 
449
- ・photoswipe.min.js
450
-
451
449
  ・photoswipe-ui-default.js
452
450
 
453
- ・photoswipe-ui-default.min.js
454
-
455
451
  ・他CSSファイル
456
452
 
457
453
  があります。

1

質問文のコードをコードブロックで囲みました。

2017/01/04 13:50

投稿

araE
araE

スコア7

test CHANGED
File without changes
test CHANGED
@@ -14,9 +14,119 @@
14
14
 
15
15
  公式サイトからコピペしたものをjsファイルにしました。
16
16
 
17
+ ```ここに言語を入力
18
+
19
+ var initPhotoSwipeFromDOM = function(gallerySelector) {
20
+
21
+
22
+
23
+ // parse slide data (url, title, size ...) from DOM elements
24
+
25
+ // (children of gallerySelector)
26
+
27
+ var parseThumbnailElements = function(el) {
28
+
29
+ var thumbElements = el.childNodes,
30
+
31
+ numNodes = thumbElements.length,
32
+
33
+ items = [],
34
+
35
+ figureEl,
36
+
37
+ linkEl,
38
+
39
+ size,
40
+
41
+ item;
42
+
43
+
44
+
45
+ for(var i = 0; i < numNodes; i++) {
46
+
47
+
48
+
49
+ figureEl = thumbElements[i]; // <figure> element
50
+
51
+
52
+
53
+ // include only element nodes
54
+
55
+ if(figureEl.nodeType !== 1) {
56
+
57
+ continue;
58
+
59
+ }
60
+
61
+
62
+
63
+ linkEl = figureEl.children[0]; // <a> element
64
+
65
+
66
+
67
+ size = linkEl.getAttribute('data-size').split('x');
68
+
69
+
70
+
71
+ // create slide object
72
+
73
+ item = {
74
+
75
+ src: linkEl.getAttribute('href'),
76
+
77
+ w: parseInt(size[0], 10),
78
+
79
+ h: parseInt(size[1], 10)
80
+
81
+ };
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+ if(figureEl.children.length > 1) {
90
+
91
+ // <figcaption> content
92
+
93
+ item.title = figureEl.children[1].innerHTML;
94
+
95
+ }
96
+
97
+
98
+
99
+ if(linkEl.children.length > 0) {
100
+
17
- http://photoswipe.com/documentation/getting-started.html
101
+ // <img> thumbnail element, retrieving thumbnail url
102
+
18
-
103
+ item.msrc = linkEl.children[0].getAttribute('src');
104
+
105
+ }
106
+
107
+
108
+
19
- Here is pure Vanilla JS implementation with IE8 support:
109
+ item.el = figureEl; // save link to element for getThumbBoundsFn
110
+
111
+ items.push(item);
112
+
113
+ }
114
+
115
+
116
+
117
+ return items;
118
+
119
+ };
120
+
121
+
122
+
123
+ // find nearest parent element
124
+
125
+ var closest = function closest(el, fn) {
126
+
127
+ return el && ( fn(el) ? el : closest(el.parentNode, fn) );
128
+
129
+ };
20
130
 
21
131
 
22
132
 
@@ -30,7 +140,261 @@
30
140
 
31
141
 
32
142
 
143
+ var eTarget = e.target || e.srcElement;
144
+
145
+
146
+
147
+ // find root element of slide
148
+
149
+ var clickedListItem = closest(eTarget, function(el) {
150
+
151
+ return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
152
+
153
+ });
154
+
155
+
156
+
157
+ if(!clickedListItem) {
158
+
159
+ return;
160
+
161
+ }
162
+
163
+
164
+
165
+ // find index of clicked item by looping through all child nodes
166
+
167
+ // alternatively, you may define index via data- attribute
168
+
169
+ var clickedGallery = clickedListItem.parentNode,
170
+
171
+ childNodes = clickedListItem.parentNode.childNodes,
172
+
173
+ numChildNodes = childNodes.length,
174
+
175
+ nodeIndex = 0,
176
+
33
- 〜〜〜〜〜
177
+ index;
178
+
179
+
180
+
181
+ for (var i = 0; i < numChildNodes; i++) {
182
+
183
+ if(childNodes[i].nodeType !== 1) {
184
+
185
+ continue;
186
+
187
+ }
188
+
189
+
190
+
191
+ if(childNodes[i] === clickedListItem) {
192
+
193
+ index = nodeIndex;
194
+
195
+ break;
196
+
197
+ }
198
+
199
+ nodeIndex++;
200
+
201
+ }
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ if(index >= 0) {
210
+
211
+ // open PhotoSwipe if valid index found
212
+
213
+ openPhotoSwipe( index, clickedGallery );
214
+
215
+ }
216
+
217
+ return false;
218
+
219
+ };
220
+
221
+
222
+
223
+ // parse picture index and gallery index from URL (#&pid=1&gid=2)
224
+
225
+ var photoswipeParseHash = function() {
226
+
227
+ var hash = window.location.hash.substring(1),
228
+
229
+ params = {};
230
+
231
+
232
+
233
+ if(hash.length < 5) {
234
+
235
+ return params;
236
+
237
+ }
238
+
239
+
240
+
241
+ var vars = hash.split('&');
242
+
243
+ for (var i = 0; i < vars.length; i++) {
244
+
245
+ if(!vars[i]) {
246
+
247
+ continue;
248
+
249
+ }
250
+
251
+ var pair = vars[i].split('=');
252
+
253
+ if(pair.length < 2) {
254
+
255
+ continue;
256
+
257
+ }
258
+
259
+ params[pair[0]] = pair[1];
260
+
261
+ }
262
+
263
+
264
+
265
+ if(params.gid) {
266
+
267
+ params.gid = parseInt(params.gid, 10);
268
+
269
+ }
270
+
271
+
272
+
273
+ return params;
274
+
275
+ };
276
+
277
+
278
+
279
+ var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
280
+
281
+ var pswpElement = document.querySelectorAll('.pswp')[0],
282
+
283
+ gallery,
284
+
285
+ options,
286
+
287
+ items;
288
+
289
+
290
+
291
+ items = parseThumbnailElements(galleryElement);
292
+
293
+
294
+
295
+ // define options (if needed)
296
+
297
+ options = {
298
+
299
+
300
+
301
+ // define gallery index (for URL)
302
+
303
+ galleryUID: galleryElement.getAttribute('data-pswp-uid'),
304
+
305
+
306
+
307
+ getThumbBoundsFn: function(index) {
308
+
309
+ // See Options -> getThumbBoundsFn section of documentation for more info
310
+
311
+ var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
312
+
313
+ pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
314
+
315
+ rect = thumbnail.getBoundingClientRect();
316
+
317
+
318
+
319
+ return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
320
+
321
+ }
322
+
323
+
324
+
325
+ };
326
+
327
+
328
+
329
+ // PhotoSwipe opened from URL
330
+
331
+ if(fromURL) {
332
+
333
+ if(options.galleryPIDs) {
334
+
335
+ // parse real index when custom PIDs are used
336
+
337
+ // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
338
+
339
+ for(var j = 0; j < items.length; j++) {
340
+
341
+ if(items[j].pid == index) {
342
+
343
+ options.index = j;
344
+
345
+ break;
346
+
347
+ }
348
+
349
+ }
350
+
351
+ } else {
352
+
353
+ // in URL indexes start from 1
354
+
355
+ options.index = parseInt(index, 10) - 1;
356
+
357
+ }
358
+
359
+ } else {
360
+
361
+ options.index = parseInt(index, 10);
362
+
363
+ }
364
+
365
+
366
+
367
+ // exit if index not found
368
+
369
+ if( isNaN(options.index) ) {
370
+
371
+ return;
372
+
373
+ }
374
+
375
+
376
+
377
+ if(disableAnimation) {
378
+
379
+ options.showAnimationDuration = 0;
380
+
381
+ }
382
+
383
+
384
+
385
+ // Pass data to PhotoSwipe and initialize it
386
+
387
+ gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
388
+
389
+ gallery.init();
390
+
391
+ };
392
+
393
+
394
+
395
+ // loop through all gallery elements and bind events
396
+
397
+ var galleryElements = document.querySelectorAll( gallerySelector );
34
398
 
35
399
 
36
400
 
@@ -44,6 +408,28 @@
44
408
 
45
409
 
46
410
 
411
+ // Parse URL and open gallery if it contains #&pid=3&gid=1
412
+
413
+ var hashData = photoswipeParseHash();
414
+
415
+ if(hashData.pid && hashData.gid) {
416
+
417
+ openPhotoSwipe( hashData.pid , galleryElements[ hashData.gid - 1 ], true, true );
418
+
419
+ }
420
+
421
+ };
422
+
423
+
424
+
425
+ // execute above function
426
+
427
+ initPhotoSwipeFromDOM('.my-gallery');
428
+
429
+ ```
430
+
431
+
432
+
47
433
  ###試したこと
48
434
 
49
435
  リンクをクリックしたら〜としたいので上記を始めとするすべてのThumbnailsをlinkに変更してみましたが、サムネイルクリックが生きている状態です。