@@ -5,6 +5,8 @@ let images = []
5
5
let currentImage = null
6
6
let currentIndexIndex = 0
7
7
8
+ let hideContainer
9
+
8
10
function findOrCreateLightboxContainer ( ) {
9
11
const lightboxContainerSelector = '.lightbox-container'
10
12
@@ -24,8 +26,7 @@ function findOrCreateLightboxContainer () {
24
26
25
27
addImageZoomListener ( lightBoxContainer )
26
28
27
- const hideContainer = ( e ) => {
28
- e . stopPropagation ( )
29
+ hideContainer = ( ) => {
29
30
lightBoxContainer . classList . remove ( 'show' )
30
31
document . body . classList . remove ( 'no-scroll' )
31
32
currentImage = null
@@ -39,8 +40,14 @@ function findOrCreateLightboxContainer () {
39
40
e . stopPropagation ( )
40
41
switchImage ( 1 )
41
42
} )
42
- lightBoxContainer . querySelector ( '.lightbox-control-close' ) . addEventListener ( 'click' , hideContainer )
43
- lightBoxContainer . addEventListener ( 'click' , hideContainer )
43
+ lightBoxContainer . querySelector ( '.lightbox-control-close' ) . addEventListener ( 'click' , ( e ) => {
44
+ e . stopPropagation ( )
45
+ hideContainer ( )
46
+ } )
47
+ lightBoxContainer . addEventListener ( 'click' , ( e ) => {
48
+ e . stopPropagation ( )
49
+ hideContainer ( )
50
+ } )
44
51
45
52
document . body . appendChild ( lightBoxContainer )
46
53
}
@@ -172,6 +179,25 @@ const init = () => {
172
179
e . stopPropagation ( )
173
180
}
174
181
} )
182
+
183
+ window . addEventListener ( 'keydown' , function ( e ) {
184
+ if ( ! currentImage ) {
185
+ return
186
+ }
187
+
188
+ if ( e . key === 'ArrowRight' ) {
189
+ switchImage ( 1 )
190
+ e . stopPropagation ( )
191
+ } else if ( e . key === 'ArrowLeft' ) {
192
+ switchImage ( - 1 )
193
+ e . stopPropagation ( )
194
+ } else if ( e . key === 'Escape' ) {
195
+ if ( hideContainer ) {
196
+ hideContainer ( )
197
+ e . stopPropagation ( )
198
+ }
199
+ }
200
+ } )
175
201
}
176
202
177
203
init ( )
0 commit comments