1
1
function escapeModal ( ) {
2
- const currentVisibility = document . getElementById ( "modal" ) . classList ;
2
+ const currentModalVisibility = document . getElementById ( "modal" ) . classList ;
3
3
document . addEventListener ( "keydown" , function ( event ) {
4
- if ( event . key === "Escape" && currentVisibility . contains ( "d-none" ) == false ) {
4
+ if ( event . key === "Escape" && currentModalVisibility . contains ( "d-none" ) == false ) {
5
5
toggleModal ( ) ;
6
6
}
7
7
} )
@@ -23,19 +23,36 @@ function removeModalContent() {
23
23
modalContent . innerHTML = "" ;
24
24
}
25
25
26
- function toggleModal ( ) {
26
+ function saveStartingScrollHeight ( ) {
27
+ const startingScrollHeight = window . pageYOffset ;
28
+ const main = document . getElementById ( "main" ) ;
29
+ main . dataset . scrollHeight = startingScrollHeight ;
30
+ }
31
+
32
+ function revertToStartingScrollHeight ( ) {
33
+ const main = document . getElementById ( "main" ) ;
34
+ const startingScrollHeight = main . dataset . scrollHeight ;
35
+ window . scrollTo ( 0 , startingScrollHeight ) ;
36
+ delete main . dataset . scrollHeight ;
37
+ }
38
+
27
39
28
- const currentVisibility = document . getElementById ( "modal" ) . classList ;
40
+ function toggleModal ( ) {
41
+ const currentModalVisibility = document . getElementById ( "modal" ) . classList ;
29
42
const mainLayerClasses = document . getElementById ( "main" ) . classList ;
30
43
31
- if ( currentVisibility . contains ( "d-none" ) ) {
32
- currentVisibility . remove ( "d-none" ) ;
44
+ if ( currentModalVisibility . contains ( "d-none" ) ) {
45
+ saveStartingScrollHeight ( ) ;
46
+ currentModalVisibility . remove ( "d-none" ) ;
33
47
mainLayerClasses . add ( "position-fixed" )
48
+ currentModalVisibility . add ( "open" ) ;
34
49
attachModalListeners ( ) ;
35
50
removeModalContent ( ) ;
36
51
}
37
52
else {
38
- currentVisibility . add ( "d-none" ) ;
53
+ revertToStartingScrollHeight ( ) ;
54
+ currentModalVisibility . add ( "d-none" ) ;
55
+ currentModalVisibility . remove ( "open" ) ;
39
56
mainLayerClasses . remove ( "position-fixed" )
40
57
detachModalListeners ( ) ;
41
58
}
0 commit comments