以下是使其运行所需的最低代码,我认为。我从Using PhotoSwipe with jQuery获得了主要代码,稍作修改。另外,我从文档中添加了一个函数,当您单击缩略图时启用缩放效果,因为我认为这是PhotoSwipe必要的酷炫功能的一部分。它假定您的缩略图位于具有“缩略图”类(第1行)的容器中,并且它假定缩略图容器内部是简单的标签(第5行和第23行)。它也没有动态加载PhotoSwipe HTML,我只是把它粘贴在我的页面底部。我将最终将其作为PHP代码片段加入。我相信,通过更多的工作,这些代码可以合并到主PhotoSwipe js中,并且可以使用简单的$('.my-gallery').photoswipe({options})
这样的简单工具进行投资。那样就好了。
哦,另外,为了让它工作,您需要包含这里的photoswipe.css和“default-skin”文件https://github.com/dimsemenov/PhotoSwipe/tree/master/dist。
$('.thumbnails').each(function() {
var $pic = $(this),
getItems = function() {
var items = [];
$pic.find('a').each(function() {
var $href = $(this).attr('href'),
$size = $(this).data('size').split('x'),
$width = $size[0],
$height = $size[1];
var item = {
src : $href,
w : $width,
h : $height
}
items.push(item);
});
return items;
}
var items = getItems();
var $pswp = $('.pswp')[0];
$pic.on('click','a',function(event) {
event.preventDefault();
var $index = $(this).index();
var thumbnail = $(this)[0]
var options = {
index: $index,
bgOpacity: 0.7,
showHideOpacity: true,
getThumbBoundsFn: function(index) {
// get window scroll Y
var pageYScroll = window.pageYOffset || document.documentElement.scrollTop;
// optionally get horizontal scroll
// get position of element relative to viewport
var rect = thumbnail.getBoundingClientRect();
// w = width
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
}
}
// Initialize PhotoSwipe
var lightBox = new PhotoSwipe($pswp, PhotoSwipeUI_Default, items, options);
lightBox.init();
})
})
你是什么意思?他们在这里有你需要的所有文档:http://photoswipe.com/documentation/getting-started.html – odedta
http://codepen.io/dimsemenov/pen/ZYbPJM/试试这个 –
这也是我找到的文档,并且它很像“您可能会问,为什么PhotoSwipe不会通过JS自动添加此代码,原因很简单 - 只是为了节省文件大小,以防您需要修改布局。” - 我不需要修改布局,并问自己是否有人分叉PhotoSwipe正在做什么文档说,它不会做:通过js注入或类似的添加必要的代码:) – Alex