我新的Javascript和我有实施PhotoSwipe插件问题:PhotoSwipe:关闭画廊动画错矩形(缩略图)
我试图实施使用jQuery网页PhotoSwipe插件。大部分工作正常(打开一个画廊,浏览幻灯片)。当我关闭画廊时,问题就会发生。问题:
我点击图片1,这会打开PhotoSwipe lightbox,我导航到幻灯片2,然后关闭画廊。这关闭了画廊。但关闭动画播放图像1,而我期待它播放图像2.
它在PhotoSwipe演示页面上正常工作,所以它是我的代码中的错误。如果我复制/粘贴演示页面的JavaScript代码,它可以正常工作。
我相信我错过了一些代码,它将当前显示的幻灯片与相应的缩略图进行绑定。演示页面的主要问题是:我很难理解vanilla JS演示,哪个部分负责什么操作。 请帮我找到缺失的功能。
这里是我的PhotoSwipe码 “即可启动画廊” 事件:
$('.my-gallery').each(function() {
var $pic = $(this);
var items = itemArray;
var $pswp = $('.pswp')[0];
$pic.on('click', 'figure', function(event) {
event.preventDefault();
var $index = $(this).index();
var options = {
index: $index,
getThumbBoundsFn: function(index) {
// get element we clicked on to determine its position in window
var thumbnail = event.target;
// get position of element relative to viewport
var rect = thumbnail.getBoundingClientRect();
// get window scroll Y
var pageYScroll = window.pageYOffset ||
document.documentElement.scrollTop;
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
}
}
// Initialize PhotoSwipe
var lightBox = new PhotoSwipe($pswp, PhotoSwipeUI_Default, items, options);
lightBox.init();
});
});
我的画廊(精简到2张幻灯片):从JSON产生
<div class="row my-gallery" itemscope itemtype="http://schema.org/ImageGallery" id="img-gallery">
<figure itemprop="associatedMedia" itemscope="" itemtype="http://schema.org/ImageObject">
<a href="images/nature/DSC_7216.jpg" itemprop="contentUrl" data-size="1200x795">
<img src="images/nature/DSC_7216_t.jpg" itemprop="thumbnail">
</a>
</figure>
<figure itemprop="associatedMedia" itemscope="" itemtype="http://schema.org/ImageObject">
<a href="images/nature/DSC_7218.jpg" itemprop="contentUrl" data-size="1200x795">
<img src="images/nature/DSC_7218_t.jpg" itemprop="thumbnail">
</a>
</figure>
</div>
产品阵列:
[
{
"src": "images/nature/DSC_7216.jpg",
"msrc": "images/nature/DSC_7216_t.jpg",
"w":1200,
"h":795
},
{
"src": "images/nature/DSC_7218.jpg",
"msrc": "images/nature/DSC_7218_t.jpg",
"w":1200,
"h":795
}
]
JSON被硬编码为ap元素,使用jQuery解析器进行解析:
var itemArray = jQuery.parseJSON($(imageListSelector).html());
您可以找到full page with problem on GitHub
你能帮我找到我缺少的是什么?
编辑: 我跟踪这个问题到这部分代码在原PhotoSwipe演示:
var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
如果我有一个固定的缩略图选择更换这一部分(像我有在我的jQuery中 - 它包含“事件目标”参考),我可以强制PhotoSwipe演示重复我的行为 - 缩小在同一图像上执行。与我的情况不完全相同,但足够接近。
现在我只需要弄清楚如何改变我的getThumbBoundsFn
,而不是使用event.target
实际缩略图参考...我可能必须更新我的itemArray包括链接figure
元素,像原来的PhotoSwipe演示。正如我之前写的,我还是Javascript的新手,所以搞清楚一些事情需要时间。任何帮助将不胜感激。