-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
422 lines (422 loc) · 18.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LiteLightbox</title>
<meta content="Matheus Avellar" name="author">
<meta name="description" content="LiteLightbox – A lightweight, plug'n'play lightbox JS library.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="no-referrer">
<link rel="preconnect" href="https://cdn.avl.la/" crossorigin>
<link rel="stylesheet" type="text/css" href="style.css?v=3">
</head>
<body>
<header>
<h1>LiteLightbox</h1>
<p>
This is a demo page for the LiteLightbox "library"(?). Check the source
code at <a href="https://github.com/MatheusAvellar/LiteLightbox">GitHub</a>.
</p>
<p>Table of contents:</p>
<ol>
<li><a href="#simple">Simplest example</a></li>
<li><a href="#captions">Lightbox captions</a></li>
<li><a href="#galleries">Galleries</a></li>
<li><a href="#no-previews">No previews</a></li>
</ol>
</header>
<hr>
<main>
<section>
<h2>What is it?</h2>
<p>
This code aims to create a progressively enhanced, lightweight,
Wikipedia-like lightbox plugin in vanilla JS. LiteLightbox is about
<strong>2.7 KB</strong> of raw minified JavaScript, at
<a href="litelightbox.min.js">litelightbox.min.js</a> (uncompressed!).
</p>
<p>
Additionally, for the default styling <i>a la</i> Wikipedia, there's
around <strong>5 KB</strong> of raw minified CSS, at
<a href="litelightbox.min.css">litelightbox.min.css</a>.
</p>
<figure class="full-width">
<pre><code><<span class="tag">link</span> <span class="attr">href</span>=<span class="str">"litelightbox.min.css"</span> <span class="attr">rel</span>=<span class="str">"stylesheet"</span> <span class="attr">type</span>=<span class="str">"text/css"</span>>
<<span class="tag">script</span> <span class="attr">src</span>=<span class="str">"litelightbox.min.js"</span>></<span class="tag">script</span>></code></pre>
</figure>
<p>
<b>What's a lightbox?</b> It's that thing where you see a thumbnail of an image
and, when you click it, it expands into the full-resolution image.
</p>
<p>
<b>Why should I use it?</b> Well, ideally, you don't want to load huge,
hundred-KB images into a user's browser unless they want to see them.
So, instead, you can load small previews and wait for users to click on
them to expand.
</p>
</section>
<section>
<h2 id="simple">
<a href="#simple">#</a>
Simplest example
</h2>
<p>
This is a simple, common use case. No captions, just a simple image.
The thumbnail is wrapped in an anchor, which acts as the fallback for
users with JavaScript disabled.
</p>
<p style="text-align:center">
<a href="images/1-real-2018-miolo.png" data-llb-src="href">
<img src="images/1-real-2018-miolo-250p.jpg"
alt="Photograph of a broken Brazilian coin.">
</a>
</p>
<figure class="full-width">
<figcaption>Code for this example:</figcaption>
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"big-version.png"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span>>
</<span class="tag">a</span>>
</code></pre></figure>
<p>
Alternatively, if your trigger is an anchor which links to the
full-resolution image via <code><span class="attr">href</span></code> already, you don't need to
write the path again into <code><span class="attr">data-llb-src</span></code>; instead, you can
pass a value of <code><span class="str">"href"</span></code> to it:
</p>
<figure class="full-width">
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="highlight">"href"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span>>
</<span class="tag">a</span>>
</code></pre></figure>
<p>
Don't forget your alt text! If the first child of the <code><span class="attr">data-llb-src</span></code>
element is an <code><<span class="tag">img</span>></code> with alt
text, then that alt text will be copied to the full-resolution image.
</p>
<figure class="full-width">
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span> <span class="highlight">alt="…"</span>>
</<span class="tag">a</span>>
</code></pre></figure>
<p>
To add an alt text specific to the full-resolution image, simply add the
attribute <code><span class="attr">data-llb-alt</span></code>:
</p>
<figure class="full-width">
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span> <span class="highlight">data-llb-alt="…"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span> <span class="attr">alt</span>=<span class="str">"…"</span>>
</<span class="tag">a</span>>
</code></pre></figure>
</section>
<section>
<h2 id="captions">
<a href="#captions">#</a>
Lightbox captions
</h2>
<p>
You can, of course, include captions in the lightbox. This is done via
the <code><span class="attr">data-llb-caption</span></code> attribute.
Note that lightbox captions do not show up in "thumbnail mode". They
only appear when the lightbox is opened.
Here's an example (click to see image attribution):
</p>
<p style="text-align:center">
<a href="images/Mini_prato_tipico_brasileiro.png"
data-llb-src="href">
<img src="images/Mini_prato_tipico_brasileiro-250p.png"
alt="Photograph of peanuts on a wooden table.">
</a>
<span data-llb-caption>
Photo taken on 8 June 2020 by
<a href="https://commons.wikimedia.org/wiki/User:Avelludo">User:Avelludo</a>.
Available at
<a href="https://commons.wikimedia.org/wiki/File:Mini_prato_t%C3%ADpico_brasileiro.png">Wikimedia Commons</a>.
</span>
</p>
<figure class="full-width">
<figcaption>Code for this example:</figcaption>
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span> <span class="attr">alt</span>=<span class="str">"…"</span>>
</<span class="tag">a</span>>
<<span class="tag">span</span> <span class="attr">data-llb-caption</span>>
(full-resolution caption)
</<span class="tag">span</span>>
</code></pre></figure>
<p>
The element with the <code><span class="attr">data-llb-caption</span></code>
attribute will be hidden on the main page, and its contents will be
placed inside a <code><<span class="tag">figcaption</span>></code>
element in the lightbox.
</p>
<p class="warning">
Note that, to include a lightbox caption, the caption wrapper element
(that is, the element with the <code><span class="attr">data-llb-caption</span></code> attribute)
<strong>MUST</strong> be the next direct sibling of the element with
the <code><span class="attr">data-llb-src</span></code> attribute. In this example, the
<code><<span class="tag">span</span> <span class="attr">data-llb-caption</span>></code>
element <strong>MUST</strong> come immediately after the
<code><<span class="tag">a</span> <span class="attr">data-llb-src</span>></code> element.
</p>
<p>
Be aware of permitted contents in HTML. For example, a
<code><<span class="tag">div</span>></code> element is not allowed inside a
<code><<span class="tag">p</span>></code> element. So, when building your caption with custom
HTML, make sure you're not breaking the DOM, so that your captions show
up properly.
</p>
<div style="margin-top:2.5rem;"></div>
<p>
With lightbox captions, images can have two different contexts – a
shortened caption that goes alongside the thumbnail, and a longer
caption that appears with the lightbox.
</p>
<div class="gallery">
<figure>
<a href="images/unsplash-TysS85XkjgI.jpg"
data-llb-src="href">
<img src="images/unsplash-TysS85XkjgI-250p.jpg"
alt="Photograph of peanuts on a wooden table.">
</a>
<div data-llb-caption>
<p>
This caption is only visible on the lightbox.
</p>
<p>
Photo published on 3 December 2019 at
<a href="https://unsplash.com/photos/TysS85XkjgI">Unsplash</a>
by <a href="https://unsplash.com/@isaidzib">Isai Dzib</a>.
</p>
<dl>
<dt>Camera</dt>
<dd>Canon, EOS REBEL T3i</dd>
</dl>
<dl>
<dt>Lens</dt>
<dd>50.0mm <em>f</em>/2.2</dd>
<dd>1/100s</dd>
<dd>ISO 200</dd>
</dl>
<dl>
<dt>Dimensions</dt>
<dd>4902 × 3268</dd>
</dl>
</div>
<figcaption>
<p>
This caption is only visible on the thumbnail.
Photo by <a href="https://unsplash.com/@isaidzib">Isai Dzib</a>
on <a href="https://unsplash.com/photos/TysS85XkjgI">Unsplash</a>.
</p>
</figcaption>
</figure>
</div>
<figure class="full-width">
<figcaption>Code for this example:</figcaption>
<pre><code><<span class="tag">figure</span>>
<<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span> <span class="attr">alt</span>=<span class="str">"…"</span>>
</<span class="tag">a</span>>
<<span class="tag">div</span> <span class="attr">data-llb-caption</span>>
<<span class="tag">p</span>>(full-resolution caption 1)</<span class="tag">p</span>>
<<span class="tag">p</span>>(full-resolution caption 2)</<span class="tag">p</span>>
</<span class="tag">div</span>>
<<span class="tag">figcaption</span>>
(thumbnail caption)
</<span class="tag">figcaption</span>>
</<span class="tag">figure</span>>
</code></pre></figure>
<p>
Note, again, that the element with the
<code><span class="attr">data-llb-caption</span></code>
attribute is the next direct sibling of the element with the
<code><span class="attr">data-llb-src</span></code> attribute.
</p>
<div style="margin-top:2.5rem;"></div>
<p>
Alternatively, if you use
<code><span class="attr">data-llb-caption</span>=<span class="str">"visible"</span></code>,
your caption can be the same for both the thumbnail and the lightbox.
</p>
<div class="gallery">
<figure>
<a href="images/unsplash-0v_1TPz1uXw.jpg"
data-llb-src="href">
<img src="images/unsplash-0v_1TPz1uXw-250p.jpg"
alt="A partially unpeeled banana.">
</a>
<figcaption data-llb-caption="visible">
<p>
This caption will appear both on the thumbnail and on the
lightbox. Photo by <a href="https://unsplash.com/@isaidzib">Isai Dzib</a>
on <a href="https://unsplash.com/photos/TysS85XkjgI">Unsplash</a>.
</p>
</figcaption>
</figure>
</div>
<figure class="full-width">
<figcaption>Code for this example:</figcaption>
<pre><code><<span class="tag">figure</span>>
<<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-version.png"</span> <span class="attr">alt</span>=<span class="str">"…"</span>>
</<span class="tag">a</span>>
<<span class="tag">figcaption</span> <span class="attr">data-llb-caption</span>=<span class="str">"visible"</span>>
(caption for thumbnail and lightbox)
</<span class="tag">figcaption</span>>
</<span class="tag">figure</span>>
</code></pre></figure>
</section>
<section>
<h2 id="galleries">
<a href="#galleries">#</a>
Galleries
</h2>
<p>
You can create galleries by adding the <code><span class="attr">data-llb-gallery</span></code>
attribute on the element that has <code><span class="attr">data-llb-src</span></code>.
Once open, the lightbox will display arrows for navigation. The user may
also press the arrow keys on the keyboard to flip through the gallery.
</p>
<div class="gallery">
<figure>
<a href="images/unsplash-XeHejTi3bbk.jpg"
data-llb-src="href" data-llb-gallery>
<img src="images/unsplash-XeHejTi3bbk-250p.jpg"
alt="A Polaroid camera.">
</a>
<figcaption>
<p>
Photo by <a href="https://unsplash.com/@pf91_photography">Patrick Ferretti</a>
on <a href="https://unsplash.com/photos/XeHejTi3bbk">Unsplash</a>.
</p>
</figcaption>
</figure>
<figure>
<a href="images/unsplash-fdogTkCRHG4.jpg"
data-llb-src="href" data-llb-gallery>
<img src="images/unsplash-fdogTkCRHG4-250p.jpg"
alt="A printed photograph on a wooden table.">
</a>
<figcaption data-llb-caption="visible">
<p>
Photo by <a href="https://unsplash.com/@rirri01">Rirri</a> on
<a href="https://unsplash.com/photos/fdogTkCRHG4">Unsplash</a>.
</p>
</figcaption>
</figure>
<figure>
<a href="images/unsplash-OIFgeLnjwrM.jpg"
data-llb-src="href" data-llb-gallery>
<img src="images/unsplash-OIFgeLnjwrM-250p.jpg"
alt="A front-facing Polaroid camera.">
</a>
<div data-llb-caption>
<p>
Photo published on 5 February 2021 at
<a href="https://unsplash.com/photos/OIFgeLnjwrM">Unsplash</a>
by <a href="https://unsplash.com/@pf91_photography">Patrick Ferretti</a>.
</p>
<dl>
<dt>Camera</dt>
<dd>Sony, ILCE-7M3</dd>
</dl>
<dl>
<dt>Lens</dt>
<dd>50.0mm <em>f</em>/10</dd>
<dd>1/160s</dd>
<dd>ISO 100</dd>
</dl>
<dl>
<dt>Dimensions</dt>
<dd>6000 × 4000</dd>
</dl>
</div>
<figcaption>
<p>
Photo by <a href="https://unsplash.com/@pf91_photography">Patrick Ferretti</a>
on <a href="https://unsplash.com/photos/OIFgeLnjwrM">Unsplash</a>.
</p>
</figcaption>
</figure>
</div>
<figure class="full-width">
<figcaption>Code for this example:</figcaption>
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-1.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span> <span class="highlight">data-llb-gallery</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-1.png"</span> <span class="attr">alt</span>=<span class="str">"…"</span>>
</<span class="tag">a</span>>
<<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-2.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span> <span class="attr">data-llb-gallery</span>>
<<span class="tag">img</span> <span class="attr">src</span>=<span class="str">"small-2.png"</span> <span class="attr">alt</span>=<span class="str">"…"</span>>
</<span class="tag">a</span>>
</code></pre></figure>
<p>
Currently, each page can only have a single gallery; that is, every
image tagged with <code><span class="attr">data-llb-gallery</span></code>
will be part of the same gallery. A further customization
option will be added in the future, allowing you to specify an identifier
for each separate gallery you wish to create.
</p>
</section>
<section>
<h2 id="no-previews">
<a href="#no-previews">#</a>
No previews
</h2>
<div>
This library works by hooking an "on click" event to an element with a
<code><span class="attr">data-llb-src</span></code> attribute. This
means that you don't need a thumbnail image to make it work. A simple
anchor (<code><<span class="tag">a</span>></code>) will suffice:
<a href="images/unsplash-0v_1TPz1uXw.jpg"
data-llb-src="href"
data-llb-alt="A partially unpeeled banana.">open big image</a>.
</div>
<figure class="full-width">
<figcaption>Code for this example:</figcaption>
<pre><code><<span class="tag">a</span> <span class="attr">href</span>=<span class="str">"big-version.png"</span> <span class="attr">data-llb-src</span>=<span class="str">"href"</span> <span class="attr">data-llb-alt</span>=<span class="str">"…"</span>>
open big image
</<span class="tag">a</span>>
</code></pre></figure>
<p>
Note that, since this example does not have an <code><<span class="tag">img</span>></code>
element with alt text as its first child, the <code><span class="attr">data-llb-alt</span></code>
attribute is used.
</p>
<p>
You could, in theory, use a button instead of an anchor, like this:
<button data-llb-src="images/unsplash-0v_1TPz1uXw.jpg">example button</button>
<span data-llb-caption>
Photo published on 12 June 2018 at
<a href="https://unsplash.com/photos/0v_1TPz1uXw">Unsplash</a>
by <a href="https://unsplash.com/@charlesdeluvio">Charles Deluvio</a>.
</span>
<br>
Or any other element, like a <code><<span class="tag">span</span>></code>:
<span tabindex="0" role="button" style="background-color:#de64ed;color:#fff"
data-llb-src="images/unsplash-0v_1TPz1uXw.jpg">example span</span>
<span data-llb-caption>
Photo published on 12 June 2018 at
<a href="https://unsplash.com/photos/0v_1TPz1uXw">Unsplash</a>
by <a href="https://unsplash.com/@charlesdeluvio">Charles Deluvio</a>.
</span>
</p>
<p class="warning">
Keep in mind that, in the event that the user does not have JavaScript
turned on, anchors will maintain the native behavior of redirecting the
user to the full-resolution image. Other elements, such as
<code><<span class="tag">button</span>></code>s, may not have these benefits. Additionally,
remember to make your trigger element accessible to keyboard users.
</p>
</section>
</main>
<footer>
<p>
Made with ♥︎︎<!--︎[Ref] stackoverflow.com/a/38452396/4824627-->
by Matheus Avellar<br>
</p>
</footer>
<!-- Minified with toptal.com/developers/cssminifier -->
<link href="litelightbox.min.css" rel="stylesheet" type="text/css">
<!-- Minified with freeformatter.com/javascript-minifier.html -->
<script src="litelightbox.min.js"></script>
<link rel="stylesheet" href="https://cdn.avl.la/font/inter.css">
</body>
</html>