Skip to content

Commit 4cf8eaa

Browse files
cssninjacssninja
authored andcommitted
fixed modals and prevented scroll when modal is open
1 parent 235414c commit 4cf8eaa

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/js/libs/utils/utils.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/pages/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ <h3 class="subtitle is-5 is-muted">We'd love to hear from You</h3>
418418

419419

420420
<div id="auth-modal" class="modal">
421-
<div class="modal-background"></div>
421+
<div class="modal-background modal-dismiss"></div>
422422
<div class="modal-content">
423423
<div class="flex-card auth-card">
424424
<div class="tabs-wrapper">
@@ -447,7 +447,7 @@ <h3 class="subtitle is-5 is-muted">We'd love to hear from You</h3>
447447
</div>
448448
</div>
449449
</div>
450-
<button class="modal-close is-large" aria-label="close"></button>
450+
<button class="modal-close is-large modal-dismiss" aria-label="close"></button>
451451
</div>
452452

453453

0 commit comments

Comments
 (0)