item.el = figureEl; // 保存链接元素 for getThumbBoundsFn
items.push(item);
}
return items;
};
// 查找最近的父节点
var closest = function closest(el,fn) {
return el && ( fn(el) ? el : closest(el.parentNode,fn) );
};
// 当用户点击缩略图触发
var onThumbnailsClick = function(e) {
e = e || window.event;
e.preventDefault ? e.preventDefault() : e.returnValue = false;
var eTarget = e.target || e.srcElement;
// find root element of slide
var clickedListItem = closest(eTarget,function(el) {
return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
});
if(!clickedListItem) {
return;
}
// find index of clicked item by looping through all child nodes
// alternatively,you may define index via data- attribute
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(index >= 0) {
// open PhotoSwipe if valid index found
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];
}
var openPhotoSwipe = function(index,galleryElement,disableAnimation,fromURL) {
var pswpElement = document.querySelectorAll('.pswp')[0],gallery,options,items;
// define gallery index (for URL)
galleryUID: galleryElement.getAttribute('data-pswp-uid'),getThumbBoundsFn: function(index) {
// See Options -> getThumbBoundsFn section of documentation for more info
var thumbnail = items[index].el.getElementsByTagName('img')[0],// find thumbnail
pageYScroll = window.pageYOffset || document.documentElement.scrollTop,rect = thumbnail.getBoundingClientRect();