Support » Plugin: Lightbox with PhotoSwipe » Opening photoswipe via javascript API

  • Resolved lovor

    (@lovor)


    Hello, I have to open a gallery (which is anyway possible to open by clicking on it) as well on a button which is situated next to it.

    For that purpose, I should be able to call a photoswipe opening function in frontend.min.js, to open the lightbox.

    I did not find other way (except maybe sending click event to element in question, I did not go with this), than to modify your javascript code by exposing openPhotoSwipe function:

    line 196:
    window.openPhotoSwipe = function(element_index, group_index, element, fromURL, returnToUrl) {

    However, this change will break with next update of your plugin, so I tried to dequeue that script enqueued in your plugin, and then enqueue my version in functions.php, but I am losing plugin buttons functionality.

    Therefore, currently the best option is probably to send click event.

    Is there a better way to do this? Would you consider to expose simple API so it can be called from Javascript?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Arno Welzel

    (@awelzel)

    Instead of exposing openPhotoSwipe() as global function I’d would create a new function with the prefix lbwps similar to window.lbwpsPhotoSwipe which is the PhotoSwipe instance when the lightbox is open.

    So this could be:

    window.lbwpsOpenPhotoSwipe(linkObject)
    window.lbwpsOpenPhotoSwipeById(id)

    Where linkObject is the image link to open and id would be the ID. Depending on if you have an object or just an ID you can then use the appropriate function.

    Usage example:

    <a id="image1" href="myimage.jpg" ...><img src="myimage-300x300.jpg" ...></a>
    
    <button type="button" onClick="window.lbwpsOpenPhotoSwipeById('image1');">Open me!</button>

    Would this work for you?

    • This reply was modified 3 months, 3 weeks ago by Arno Welzel.
    • This reply was modified 3 months, 3 weeks ago by Arno Welzel.
    Plugin Author Arno Welzel

    (@awelzel)

    I also create a Github issue for this:

    https://github.com/arnowelzel/lightbox-photoswipe/issues/66

    Thread Starter lovor

    (@lovor)

    I switched currently to invoking element.click() and it works fine. It feels somewhat of a hack, but it works and now I am fine with that.

    So, please do not hurry with changes in code – consider these proposed changes just as a suggestion for future improvements.

    All the best and thanks!

    Plugin Author Arno Welzel

    (@awelzel)

    I think using element.click() is totally fine.

    Also see here: https://caniuse.com/mdn-api_htmlelement_click

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.