|
| 1 | +--- |
| 2 | +id: lightbox |
| 3 | +title: Lightbox |
| 4 | +layout: default |
| 5 | +--- |
| 6 | + |
| 7 | +# Lightbox |
| 8 | + |
| 9 | +A lightbox is a modal that displays an image or video in a full-screen overlay. It's great for things like clicking on thumbnails to view a larger [image](#image-lightbox) or [video](#video-lightbox). |
| 10 | + |
| 11 | +## Image Lightbox |
| 12 | + |
| 13 | +The simplest way to trigger a lightbox in reveal.js is to add the `data-preview-image` attribute to an `img` tag. Clicking on the image below, will open the same image in an overlay. |
| 14 | + |
| 15 | +```html |
| 16 | +<img src="reveal.png" data-preview-image> |
| 17 | +``` |
| 18 | + |
| 19 | +<div class="reveal reveal-example"> |
| 20 | + <div class="slides"> |
| 21 | + <section> |
| 22 | + <img src="/images/logo/reveal-black-text-sticker.png" data-preview-image width="400px"> |
| 23 | + </section> |
| 24 | + </div> |
| 25 | +</div> |
| 26 | + |
| 27 | +Lightboxes aren't limited to opening the image src. You can open any image you like by assigning a value to the `data-preview-image` attribute. |
| 28 | + |
| 29 | +```html |
| 30 | +<img src="reveal.png" data-preview-image="mastering.svg"> |
| 31 | +``` |
| 32 | + |
| 33 | +<div class="reveal reveal-example"> |
| 34 | + <div class="slides"> |
| 35 | + <section> |
| 36 | + <img src="/images/logo/reveal-black-text-sticker.png" data-preview-image="/images/docs/mastering.svg" width="400px"> |
| 37 | + </section> |
| 38 | + </div> |
| 39 | +</div> |
| 40 | + |
| 41 | + |
| 42 | +## Video Lightbox |
| 43 | + |
| 44 | +Video lightboxes work the same way as image lightboxes except you use the `data-preview-video` attribute instead. |
| 45 | + |
| 46 | +```html |
| 47 | +<video src="video.mp4" data-preview-video></video> |
| 48 | +<img src="reveal.png" data-preview-video="video.mp4"> |
| 49 | +``` |
| 50 | + |
| 51 | +<div class="reveal reveal-example"> |
| 52 | + <div class="slides"> |
| 53 | + <section> |
| 54 | + <div class="r-hstack" style="gap: 40px;"> |
| 55 | + <video src="https://static.slid.es/site/homepage/v1/homepage-video-editor.mp4" data-preview-video width="400px"></video> |
| 56 | + <img src="/images/logo/reveal-black-text-sticker.png" data-preview-video="https://static.slid.es/site/homepage/v1/homepage-video-editor.mp4" width="400px"> |
| 57 | + </div> |
| 58 | + </section> |
| 59 | + </div> |
| 60 | +</div> |
| 61 | + |
| 62 | +## Lightbox Media Size |
| 63 | +The sizing of media in the lightbox can be controlled using the `data-preview-fit` attribute. The following fit modes are supported: |
| 64 | + |
| 65 | +| Value | Effect | |
| 66 | +| :------------------- | :----------------------------------------------------------------------- | |
| 67 | +| scale-down (default) | Scale media down if needed to fit in the lightbox. | |
| 68 | +| contain | Scale media up and down to fit the lightbox without cropping. | |
| 69 | +| cover | Scale media to cover the entire lightbox, even if some of it is cut off. | |
| 70 | +```html |
| 71 | +<img src="reveal.png" data-preview-image data-preview-fit="cover"> |
| 72 | +``` |
| 73 | + |
| 74 | +<div class="reveal reveal-example"> |
| 75 | + <div class="slides"> |
| 76 | + <section> |
| 77 | + <img src="/images/logo/reveal-white-text.svg" data-preview-image data-preview-fit="cover" width="400px"> |
| 78 | + </section> |
| 79 | + </div> |
| 80 | +</div> |
| 81 | + |
| 82 | +## Works on Any Element |
| 83 | + |
| 84 | +Note that the lightbox feature works on any element, not just images and videos. For example, you can trigger an image or video lightbox from clicking a button or link. |
| 85 | + |
| 86 | +```html |
| 87 | +<a data-preview-image="image.png">📸 Open Logo</a> |
| 88 | +<button data-preview-video="video.mp4">🎥 Open Video</button> |
| 89 | +``` |
| 90 | + |
| 91 | +<div class="reveal reveal-example"> |
| 92 | + <div class="slides"> |
| 93 | + <section> |
| 94 | + <a data-preview-image="/images/logo/reveal-black-text-sticker.png">📸 Open Logo</a> |
| 95 | + <br/> |
| 96 | + <br/> |
| 97 | + <button data-preview-video="https://static.slid.es/site/homepage/v1/homepage-video-editor.mp4">🎥 Open Video</button> |
| 98 | + </section> |
| 99 | + </div> |
| 100 | +</div> |
| 101 | + |
| 102 | +## Iframe Lightbox |
| 103 | + |
| 104 | +It's also possible to preview links in iframe lightboxes. The syntax for this is slightly different from how image and video lightboxes work. To enable a link preview, you'll need to add the `data-preview-link` attribute to an anchor tag. It does not accept any value and will always use the anchor's `href` attribute as the source for the iframe. |
| 105 | + |
| 106 | +```html |
| 107 | +<a href="https://hakim.se" data-preview-link>Open Hakim's Website</a> |
| 108 | +``` |
| 109 | + |
| 110 | +<div class="reveal reveal-example"> |
| 111 | + <div class="slides"> |
| 112 | + <section> |
| 113 | + <a href="https://hakim.se" data-preview-link>Open Hakim's Website</a> |
| 114 | + </section> |
| 115 | + </div> |
| 116 | +</div> |
| 117 | + |
| 118 | +Note that this will only work if the link allows for embedding. Many domains prevent embedding via `x-frame-options` or `Content-Security-Policy`. |
0 commit comments