-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss-lightbox.js
64 lines (56 loc) · 1.66 KB
/
css-lightbox.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
var anchors = document.getElementsByClassName('lightbox-overlay');
var anchorIDs = getLightboxAnchorIDs();
var numAnchors = anchorIDs.length;
for (var i = 0; i < anchors.length; i++) {
anchors[i].addEventListener('click', closeLightboxClick);
}
if (anchorIDs.length >= 1) {
window.addEventListener('keypress', closeLightboxEsc);
if (anchorIDs.length >= 2) {
window.addEventListener('keypress', navigateLightboxes);
}
}
function navigateLightboxes(event) {
var currentAnchor = window.location.hash;
var currentAnchorIndex = anchorIDs.indexOf(currentAnchor);
if (currentAnchorIndex != -1) {
if (37 === event.keyCode) {
prevLightbox(numAnchors, currentAnchorIndex);
} else if (39 === event.keyCode) {
nextLightbox(numAnchors, currentAnchorIndex);
}
}
}
function prevLightbox(numAnchors, currentAnchorIndex) {
if (currentAnchorIndex === 0) {
window.location.hash = anchorIDs[numAnchors -1 ];
} else {
window.location.hash = anchorIDs[currentAnchorIndex - 1];
}
}
function nextLightbox(numAnchors, currentAnchorIndex) {
if (currentAnchorIndex == numAnchors - 1) {
window.location.hash = anchorIDs[0];
} else {
window.location.hash = anchorIDs[currentAnchorIndex + 1];
}
}
function closeLightboxClick(event) {
if ( anchorIDs.indexOf("#" + event.target.id) != -1 ) {
window.location.hash = "#no-image";
}
}
function closeLightboxEsc(event) {
if ( 27 === event.keyCode ) {
if ( anchorIDs.indexOf(window.location.hash) != -1 ) {
window.location.hash = "#no-image";
}
}
}
function getLightboxAnchorIDs() {
var anchorIDsReturn = [];
for (var i = 0; i < anchors.length; i++) {
anchorIDsReturn[i] = "#" + anchors[i].id;
}
return anchorIDsReturn;
}