var initphotoswipefromdom = function(galleryselector) { // parse slide data (url, title, size ...) from dom elements // (children of galleryselector) var parsethumbnailelements = function(el) { var thumbelements = el.childnodes, numnodes = thumbelements.length, items = [], figureel, childelements, linkel, size, item; for(var i = 0; i < numnodes; i++) { figureel = thumbelements[i]; //
element // include only element nodes if(figureel.nodetype !== 1) { continue; } linkel = figureel.children[0]; // element size = linkel.getattribute('data-size').split('x'); // create slide object item = { src: linkel.getattribute('href'), w: parseint(size[0], 10), h: parseint(size[1], 10) }; if(figureel.children.length > 1) { //
content item.title = figureel.children[1].innerhtml; } if(linkel.children.length > 0) { // thumbnail element, retrieving thumbnail url item.msrc = linkel.children[0].getattribute('src'); } item.el = figureel; // save link to element for getthumbboundsfn items.push(item); } return items; }; // find nearest parent element var closest = function closest(el, fn) { return el && ( fn(el) ? el : closest(el.parentnode, fn) ); }; // triggers when user clicks on thumbnail var onthumbnailsclick = function(e) { e = e || window.event; e.preventdefault ? e.preventdefault() : e.returnvalue = false; var etarget = e.target || e.srcelement; var clickedlistitem = closest(etarget, function(el) { return el.tagname === 'li'; }); if(!clickedlistitem) { return; } // find index of clicked item var clickedgallery = clickedlistitem.parentnode, childnodes = clickedlistitem.parentnode.childnodes, numchildnodes = childnodes.length, nodeindex = 0, index; for (var i = 0; i < numchildnodes; i++) { if(childnodes[i].nodetype !== 1) { continue; } if(childnodes[i] === clickedlistitem) { index = nodeindex; break; } nodeindex++; } if(index >= 0) { openphotoswipe( index, clickedgallery ); } return false; }; // parse picture index and gallery index from url (#&pid=1&gid=2) var photoswipeparsehash = function() { var hash = window.location.hash.substring(1), params = {}; if(hash.length < 5) { return params; } var vars = hash.split('&'); for (var i = 0; i < vars.length; i++) { if(!vars[i]) { continue; } var pair = vars[i].split('='); if(pair.length < 2) { continue; } params[pair[0]] = pair[1]; } if(params.gid) { params.gid = parseint(params.gid, 10); } if(!params.hasownproperty('pid')) { return params; } params.pid = parseint(params.pid, 10); return params; }; var openphotoswipe = function(index, galleryelement, disableanimation) { var pswpelement = document.queryselectorall('.pswp')[0], gallery, options, items; items = parsethumbnailelements(galleryelement); // define options (if needed) options = { index: index, history: false, taptoclose: true, // define gallery index (for url) galleryuid: galleryelement.getattribute('data-pswp-uid'), getthumbboundsfn: function(index) { // see options -> getthumbboundsfn section of docs for more info var thumbnail = items[index].el.getelementsbytagname('img')[0], // find thumbnail pageyscroll = window.pageyoffset || document.documentelement.scrolltop, rect = thumbnail.getboundingclientrect(); return {x:rect.left, y:rect.top + pageyscroll, w:rect.width}; }, // history & focus options are disabled on codepen // remove these lines in real life: historyenabled: false, focus: false }; if(disableanimation) { options.showanimationduration = 0; } // pass data to photoswipe and initialize it gallery = new photoswipe( pswpelement, photoswipeui_default, items, options); gallery.init(); }; // loop through all gallery elements and bind events var galleryelements = document.queryselectorall( galleryselector ); for(var i = 0, l = galleryelements.length; i < l; i++) { galleryelements[i].setattribute('data-pswp-uid', i+1); galleryelements[i].onclick = onthumbnailsclick; } // parse url and open gallery if it contains #&pid=3&gid=1 var hashdata = photoswipeparsehash(); if(hashdata.pid > 0 && hashdata.gid > 0) { openphotoswipe( hashdata.pid - 1 , galleryelements[ hashdata.gid - 1 ], true ); } }; // execute above function initphotoswipefromdom('.my-gallery');