替换PhotoSwipe相册中的图片

3

我在我的网页上有一个PhotoSwipe图库,是通过编程方式创建的,就像这样:

var instance = window.Code.PhotoSwipe.attach(image, options)

现在我想更新画廊中的图片,或在同一位置放置一个新的画廊。

为相同的 DOM 元素 创建新画廊会省略以下错误:

Code.PhotoSwipe.activateInstance:
Unable to active instance as another instance is already active for this target

使用Code.PhotoSwipe.detatch(instance)将实例从元素中分离也没有帮助。

有什么想法可以填充新图库,或者将其删除,以便我可以在同一位置创建一个新的图库?


你好,我想要做的就是让画廊在用户滑动到下一张图片时刷新,因为在滑动时会向DOM添加一张图片。我很难做到这一点!最终你成功了吗?有什么建议吗?谢谢。 - Adam Waite
我也遇到了同样的问题。想要在图库初始化后更新并替换一些图片。你们有没有解决这个问题?我不能分离和重新创建实例,因为那会对用户可见。 - Louis B.
3个回答

8
我发现避免出错的唯一方法是在执行 detatch 前调用 unsetActivateInstance
window.Code.PhotoSwipe.unsetActivateInstance(instance);

window.Code.PhotoSwipe.detatch(instance); 

2

你也可以通过调用隐藏当前活动实例,而不是分离它

window.Code.PhotoSwipe.activeInstances[0].instance.hide(0)

0

我尝试了上面所有的建议,但都没有很好地解决问题。

所以我的解决方案就是简单地不要为相同的ID创建两次画廊:

    instance = window.Code.PhotoSwipe.getInstance(myuniqueid);
    if (window.Code.Util.isNothing(instance)) {
        // Only initialize if there is no gallery with this ID already.
        myPhotoSwipe = window.Code.PhotoSwipe.attach(window.document.querySelectorAll(galleryimagesselector), {captionAndToolbarAutoHideDelay: 0, jQueryMobile: true, preventSlideshow: true, enableMouseWheel: false, enableKeyboard: false}, myuniqueid);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接