我也在寻找一个解决方案,打开一个带有img链接的PhotoSwipe画廊。所以有一个带有图库图标的IMG。我想如果用户点击它,画廊就会打开。
有没有人知道我该怎么做?
我发现了这个。但这是在装入画廊时打开的。
<script type="text/javascript">
(function(window, PhotoSwipe){
document.addEventListener('DOMContentLoaded', function(){
var
options = {
preventHide: true
},
instance = PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );
instance.show(0);
}, false);
}(window, window.Code.PhotoSwipe));
</script>最好的建议
发布于 2013-03-21 07:25:52
我刚刚开始使用photoSwipe,所以我不确定这是否会起作用,但在我看来,你只需在点击事件上调用instance.show(0)。
假设我在页面上有这个元素:<a id="launch-gallery" href="#">Click to launch gallery</a>,我可以添加这个jQuery单击事件来启动图库:
$('#launch-gallery').click(function(evt){
evt.preventDefault(); // prevent regular click action
instance.show(0); // Make sure 'instance' variable is available to this function
});如果你没有使用jQuery,你可以在原生JavaScript中做同样的事情(只是有点冗长)。
我希望这能帮到你。
发布于 2016-04-08 11:28:04
请注意,我使用php (ajax)来传递图像的位置和大小,因此您仍然需要自己定义json数据。这就是我用Jquery实现的方法。
$('.element').off(); //in case it's a dynamically changing element
$('.element').on("click tap", function () {
var dataForPhpScript = $(this).parents('.gallery').attr("data-attr"); //data for php script
$.getJSON('urlToPHPFunction?dataID=' + dataForPhpScript, function (json) {
openPhotoSwipe(json);
});
});下面是photoswipe打开函数:
function openPhotoSwipe(jsonData) {
var pswpElement = document.querySelectorAll('.pswp')[0];
// define options (if needed)
var options = {
// history & focus options are disabled on CodePen
history: false,
focus: false,
showAnimationDuration: 0,
hideAnimationDuration: 0
};
var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, jsonData, options);
gallery.init();
}注意到jsonData应该看起来有点像这样:
[
{
src: 'https://placekitten.com/600/400',
w: 600,
h: 400
},
{
src: 'https://placekitten.com/1200/900',
w: 1200,
h: 900
}
];我意识到这个答案来得太晚了,但由于这是在谷歌搜索完全不同的东西时出现的(但与photoswipe相关),我想这可能会有用!
https://stackoverflow.com/questions/15496776
复制相似问题