Skip to content

Commit afca9cb

Browse files
verementminimaluminium
authored andcommitted
Fix problem showing dropdown after window resize
When the browser window is resized, a new click event listener is added to the window but the old one is not removed. The old one ends up preventing the newly-made dropdown from opening. Fix this by remembering the click event listener function added to the window, and remove it before making the new dropdown after a window resize.
1 parent b56bec2 commit afca9cb

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

assets/js/dropdown.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
});
1717
}
1818

19+
var windowClickListener;
1920
const makeDropdown = function () {
2021
if (mediaQuery.matches) return;
2122
const submenuItems = [];
@@ -62,11 +63,12 @@
6263
document.body.classList.toggle('is-dropdown-open');
6364
});
6465

65-
window.addEventListener('click', function (e) {
66+
windowClickListener = function (e) {
6667
if (!toggle.contains(e.target) && document.body.classList.contains('is-dropdown-open')) {
6768
document.body.classList.remove('is-dropdown-open');
6869
}
69-
});
70+
};
71+
window.addEventListener('click', windowClickListener);
7072
}
7173

7274
imagesLoaded(head, function () {
@@ -75,6 +77,7 @@
7577

7678
window.addEventListener('resize', function () {
7779
setTimeout(function () {
80+
window.removeEventListener('click', windowClickListener);
7881
nav.innerHTML = navHTML;
7982
makeDropdown();
8083
}, 1);

0 commit comments

Comments
 (0)