-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Responsive images for the slideshow? #13
Comments
The problem here is, that PhotoSwipe does not use the picture which is displayed in the page but the one which is linked. Example:
In this case the image used for the lightbox is Of course it is possible for the frontend script to check if the Detecting WebP might be possible as well as mentioned on https://stackoverflow.com/questions/5573096/detecting-webp-support:
|
Using a class that creates the markup for the responsive picture element I managed to create a slide object as mentioned here. The slide object is created from the image array that WordPress holds, reading from the Regarding your plugin, what would be useful, would be a field where one could pass the WordPress image data array and then your JS creates the responsive slide object from that. Yes - shifting the array of srcset to 1 or 2 sizes above the current viewport, i.e. Could even try to read from the picture element, as you only need the srcset values. Exploring further how your plugin works. Is there a specific reason you chose jQuery for your js? Yes - on the webp support as well, thank you! |
Thanks for the pointer to the documentation. I'll have a look into that and see how I can integrate this to my plugin. About jQuery - well, it is shipped in WordPress anyway, so was is no need to avoid it and this part in the frontend is much easier with jQuery:
Eventhough it is possible to do this with plain JavaScript as well using I did some quick tests: on my server the version of jQuery shipped with WordPress needs about 35 KB transfer volume (about 100 KB minified JS compressed using gzip) and adds no download time at all since thanks to HTTP/2 everything is downloaded in parallel. So the additional overhead is already not very big. Maybe I remove jQuery as dependency in a future version, so if no other active plugin/theme needs it, it won't get used at all. |
I might open another issue with your kind permission where we can discuss the JS/jQuery bits. I say, no script is as good as no script. Though I like your comparison and wonder about the point you make with "this part" being easier. Regardless, to keep on topic here, I got the following going. The Like this I The data attribute name of the button is the key to the this.slide = photoswipe_data.find(
element => element.gallery_id === this.gallery_id
) So you can have multiple galleries and feed them the slide objects that you previously generated. What I found notable, when doing responsive images, the Now about generating that JSON data with the Like this I give the user the option to just Including a |
Just a quick reply about your points: a) have slideshows independent of the page markup This is not possible in WordPress. WordPress only provides the final markup and no structured information about the content. My plugin must use the markup since not all images come from galleries and not all gallery plugins have hooks to get the gallery data before the frontend markup is created. For example Gutenberg has a gallery block as well, but there is no hook for get attributes like a single caption for the whole gallery (see https://wordpress-demo.arnowelzel.de/lightbox-with-single-caption-for-multiple-images/ and https://wordpress-demo.arnowelzel.de/lightbox-with-photoswipe/). b) hence dot not generate the slide object data from the markup At least the backend must parse the markup anyway - as explained above. However at the moment the backend only uses a simple regex to find images inside links in the markup to add the required attributes for the frontend. The frontend is then using the DOM to build a list of all images for PhotoSwipe based on the attributes from the backend and to identify the captions in the markup (which can be quite tricky as for example with the common caption in Gutenberg galleries). If this would be done in the backend too, it would create a lot of additional load in the backend - and it's not needed since the browser has the DOM anyway and JavaScript can handle that quite well. c) can, but do not have to show every slide This could be controlled with additional attributes in the image, similar to the gallery ID which already groups images to be shown within a lightbox loop if needed. However - from the end users perspective: how to add this information in the backend editor? As long as you just use TinyMCE you may switch to HTML view and add the required attributes in the markup manually. But with Gutenberg image blocks? Maybe there is an option to extend Gutenberg blocks to include additional information for other plugins, but I'm not sure if I'm willing to spend time on this. c) can have multiple slideshows per page or post with minimal JS There are already mulitple slideshows using the gallery ID attribute which is set by the backend if the user wants to have each gallery in its own slideshow. In the frontend this only requires two lines of code for a different selector if a users clicks an image which is part of a gallery which has a ID different to 1 (which indicates "this is part of a gallery"). So what is your point? d) have each slideshow open on click of the corresponding button per data attribute Hmm - I don't get your point again. I have to deal with existing link elements in the markup which need additional event handlers attached, so clicking the images will open the lightbox. And the part which adds the event handler to every link element which maches the selector Of course this all can be done with plain JavaScript as well - it's just a bit more code. |
About removing jQuery: I created a branch which provides a version without jQuery. See here: https://github.com/arnowelzel/lightbox-photoswipe/blob/no-jquery/js/frontend.js Without jQuery a lot of additional checks are needed so the script gets nearly twice as big and this is just a "proof of concept" yet. There may still be bugs especially for getting the captions from the DOM - and as I already explained, there is no other way to do this. |
Yeah no my post was never about things I am missing from your plugin, sorry if it comes across like that. I played around a lot with the plugin and then focused on the slide object data and figured, since I am in control for this particular use case, I could get the data and work with that. That worked out fine, so I posted the approach. Getting data from Backend vs collecting data from Frontend really. And surly, while working on that, it crossed my mind to make a plugin, but then it hit me, like you say, there are no hooks for this, to get the image data needed, one cannot know what image should be in a slideshow or not and what its source is, unless all this is coded directly into the theme/template. Yes, Gutenberg is even heavier in that aspect. Totally get your point here. To come to that. My point in c) and d) was that with the data coming from the Backend I have a few lines of JS in the Frontend and one or multiple slideshows working. I guess I just mentioned this as I like to keep things down to only what is really needed, for the particular case, of course. Also, yes, having the gallery id coming from the data and getting that object by getting the gallery id from the data attribute and using it as key to find the right object in the data makes it possible that either a link, button or a thumbnail can be used to open the slide or slideshow or slideshows. Again, all this is in a context of where one can generate the slide objects, in the context of a plugin this of course is different. Great you made a JS version! Thank you for your work. Again, yes, I see what you mean with not having control of what will be shown and hence having to react upon that in the plugin context. |
The JS version does not work properly, so I reverted back to jQuery. I won't go back to pure JS very soon since I can't test the plugin with hundrets of themes and plugins which might cause a pure JS version to break. Also see: |
I am not a developer and my knowledge of code is limited. But Arno just help me with an issue related to the Visual Portfolio plugin from the Wordpress repository. It uses Photoswipe as well and it works with responsive images (example). I just scrolled through the plugin’s class-images.php. Maybe there is some code to get inspired from? |
JFTR: Lightbox with PhotosSwipe not using jQuery any longer after some fixes in 2.94 |
Interesting development. When you posted that this is much easier with jQuery I did not really see what you meant.
But now it seems you solved this with using vanilla JS. Nice one! I am intrigued. From a coding perspective, what are/were the main pain points for you mentioning that this above is better done with JS? Trying to learn and understand, not a critique. Or to put it simple, what lines of your current JS code would replace above lines of the formerly used jQuery code and what made you take the decision that vanilla JS would be the right way to go forward? Concerning being more "free" from WordPress while still being able to provide PhotoSwipe. All the points you mentioned are right about WP. And this is sad, kind of. There a no hooks or functions that can, before a post is rendered, create an object of all post data, including images. In your case you can only react to rendered markup. You would have lot more options if you step in before that. If there would be hooks, etc., one could pull from the image data from that and create the gallery JSON data. I solved it by creating custom fields and creating the gallery in the backend/dashbaord like that, using ACF. But using the Gutenberg editor, this is out of question. Well perhaps with ACF Blocks, but have not tried that. I find this is more a WP design issue rather than anything to do with your plugin. Just trying to discuss really and see what clever ways there would be to "work around" WP. In fact, often when working with WP and wanting to do something a bit more fancier, well I find myself working "around" WP instead of "with" WP, I am sure you know what I mean. Did you at any point have an other ideas to how this could be done? Integrating logic into each post editor and post data through a plugin that would be able to "know" what image sources are being used in the post. Then giving the user options what to do with those images concerning PhotoSwipe. Then working with already rendered source and reacting to that would be out of the way. But yeah, WP might not like that approach. |
About jQuery: jQuery was less to type and therefore there are fewer chances for errors. For example - this is the code with jQuery to get an event handler attached to each
And this is the same in vanilla JS - and just the minimal version assuming that all browsers will understand that without any graceful degradation for older browsers:
A good thing about jQuery besides the fact that you write less code is the better compatibility. You don't have to think about how to implement something which runs on every major browser. jQuery will usually deal with browser specific differences. On the other hand most browsers behave more or less the same since most of them are based either on Chromium or Gecko. Even Microsoft decided to use Chromium for their "Edge" browser. So dealing with the quirks of Internet Explorer is no longer neccessary. Therefore using vanilla JS is the better way nowadays. And about better ways to integrate Photoswipe: There are already page builders, galleries and other extensions for WordPress which use the official version of Photoswipe as well like Elementor, Visual Composer or WooCommerce (for product images). Maybe it would be a good idea to bring all these projects together so they won't only use their own version of Photoswipe but allow to use the version from my WP plugin. I already had a number of support requests where people asked me how to disable the old version of Photoswipe in a theme or gallery plugin, so they can use the version from my plugin. A problem is however - some of these projects are commercial ones and they don't like to rely on a free open source project. |
Hi Arno,
First of all thank you for your plugin. I really like it, am an avid follower of photoswipe, using it in its pure form a lot. Now I am working on a WordPress site and am trying to get responsive images going for the slideshow.
I use the following responsive picture syntax.
It is the Changing image sizes, high-DPI images & different image types use case from here.
https://dev.opera.com/articles/responsive-images/
(Note this is not about art direction, where completely different images are served based on viewport.)
This serves webp format images to browsers that support it and falls back to jpg format images for older browsers.
In fact I also combine this with lazysizes.
The whole thing then looks something like this.
You might be curious about having the
width
andheight
attributes in there, well this is because of this upcoming feature. It helps with reducing page jank.Regardless of all this, when I wrap this picture element into a link so that it can be shown through your excellent lightbox-photoswipe plugin I have to use the biggest source of that image, because if I don't, and the user is on a moderately large desktop they will either see a too small and blurry or a too large and blurry image in the slideshow. This is because the image dimensions need to be specified with the data attributes. For desktop this is not an issue. But..
This of course leads to loading huge images to users on mobile devices. So while the responsive picture element syntax takes care of that with having all the possible
src
andsrcset
attribute values, this is a bit trickier to accomplish when I like to use responsive images in the slideshow.Short, how do you get responsive images for the slideshow going with your plugin? I can see in the photoswipe documentation that the native
picture
element and thesrcset
attribute are not supported. And they don't need to be supported. Thepicture
element is merely the thumbnail that then opens the slideshow. On a large screen this thumbnail has a large size and on mobile it is small.I guess all this leads to the following question. How could I generate or populate this slide object with the already existing sizes and then feed those to your plugin? More than happy to help you write code, frontend or backend, for this. What do you think?
The text was updated successfully, but these errors were encountered: