forked from Serrin/imgLightbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imglightbox.js
64 lines (57 loc) · 1.99 KB
/
imglightbox.js
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
/**
* imglightbox
* @version 1.1.0
* @author Miyako Jones <[email protected]>
* @see https://github.com/miyakoj/
*
* <link href="imglightbox.min.css" rel="stylesheet" type="text/css">
* <script src="imglightbox.min.js"></script>
*/
var imgLightbox = (function(){
"use strict";
return {
config: {
version: "1.1.0",
download: false,
externalOpen: false
},
open: function (filename, config = {}) {
if (typeof config.download !== "boolean") {config.download = this.config.download;}
if (typeof config.externalOpen !== "boolean") {config.externalOpen = this.config.externalOpen;}
var parentEls = document.getElementById("imgLightbox").style;
parentEls.display = "flex";
var imgArea = document.getElementById("imgLightbox-image-area");
// remove an existing img element from a previous lightbox display
var existingImg = imgArea.getElementsByTagName("img");
if (existingImg.length != 0) {
existingImg[0].remove();
}
// display images in an img element so that the image ratio is maintained
var img = document.createElement("img");
img.src = filename;
img.style.width = "100%";
img.style.height = "100%";
img.style.objectFit = "contain";
imgArea.appendChild(img);
var dlbtn = document.getElementById("imgLightbox-button-download");
if (config.download) {
dlbtn.style.display = "inline-block";
dlbtn.href = filename;
dlbtn.download = filename;
} else {
dlbtn.style.display = "none";
}
var eobtn = document.getElementById("imgLightbox-button-external");
if (config.externalOpen) {
eobtn.style.display = "inline-block";
eobtn.href = filename;
eobtn.target = "_blank";
} else {
eobtn.style.display = "none";
}
},
close: function () {
document.getElementById("imgLightbox").style.display="none";
}
};
}());