If I understand correctly you are using Featherlight as a "lightbox", and when that box opens you want to resize the box to be the same size as the image inside it.
So essentially what you want to do is:
- Add code which triggers after the lightbox opens
- Have that code check the dimensions of the image
- Update the dimensions of the
<iframe>
to the image's dimensions
If I have that right, the tricky part (especially if you are new to web development) is that you need to understand that all web development revolves around "documents" (web pages), and when you use any frame you wind up with multiple "documents": one for the parent that has the <iframe>
and one for the page within that <iframe>
.
So, first off you need to write logic that will trigger when the lightbox opens. You'll have to read the documentation for Featherlight to see if it gives you a "hook" for that. If not you'll have to add your own "onClick" logic that occurs when the user makes the click to open the light box. If you do the latter you might need to use a window.setTimeout
to wait a second or two for Featherlight to create the <iframe>
.
Once that logic triggers it will need to wait for the <iframe>
to load. You can do this by attaching an onLoad
event handler to the iframe (from your onClick
handler). When that handler is triggered the <iframe>
will be loaded and you can check the image's size.
To actually access the <iframe>
document see this SO: Accessing the document object of a frame with JavaScript.
Once you do that you can access the image (something like iframeDocument.getElementsByTagAndClassName('img')[0]
, if there's only one image on the page) and check it's height
and width
CSS properties.
Finally, once you've done that you change the height
and width
CSS properties of the <iframe>
and you should be all set.