@@ -42,19 +42,44 @@ export function initModals() {
4242 let targets = document . querySelectorAll ( ".modal-trigger" ) ;
4343 if ( typeof targets != "undefined" && targets != null ) {
4444 for ( var i = 0 , len = targets . length ; i < len ; i ++ ) {
45- targets [ i ] . addEventListener ( 'click' , function ( event ) {
46- var modalID = this . getAttribute ( 'data-modal' ) ;
47- document . querySelector ( '#' + modalID ) . classList . add ( 'is-active' ) ;
48- } )
45+ targets [ i ] . addEventListener ( "click" , function ( event ) {
46+ console . log ( "click modal" ) ;
47+ var modalID = this . getAttribute ( "data-modal" ) ;
48+ document . querySelector ( "#" + modalID ) . classList . add ( "is-active" ) ;
49+ const scrollY = document . documentElement . style . getPropertyValue (
50+ "--scroll-y"
51+ ) ;
52+ const body = document . body ;
53+ body . style . width = "100%" ;
54+ body . style . paddingRight = "15px" ;
55+ body . style . position = "fixed" ;
56+
57+ body . style . top = `-${ scrollY } ` ;
58+ } ) ;
4959 }
5060 }
5161
52- targets = document . querySelectorAll ( ".modal-close " ) ;
62+ targets = document . querySelectorAll ( ".modal-dismiss " ) ;
5363 if ( typeof targets != "undefined" && targets != null ) {
5464 for ( var i = 0 , len = targets . length ; i < len ; i ++ ) {
55- targets [ i ] . addEventListener ( 'click' , function ( event ) {
56- this . closest ( '.modal' ) . classList . remove ( 'is-active' ) ;
57- } )
65+ targets [ i ] . addEventListener ( "click" , function ( event ) {
66+ console . log ( "click modal close" ) ;
67+ const body = document . body ;
68+ const scrollY = body . style . top ;
69+ body . style . position = "" ;
70+ body . style . paddingRight = "" ;
71+ body . style . width = "" ;
72+ body . style . top = "" ;
73+ window . scrollTo ( 0 , parseInt ( scrollY || "0" ) * - 1 ) ;
74+ this . closest ( ".modal" ) . classList . remove ( "is-active" ) ;
75+ } ) ;
5876 }
5977 }
60- }
78+
79+ window . addEventListener ( "scroll" , ( ) => {
80+ document . documentElement . style . setProperty (
81+ "--scroll-y" ,
82+ `${ window . scrollY } px`
83+ ) ;
84+ } ) ;
85+ }
0 commit comments