Last active
November 27, 2018 15:01
-
-
Save dsternlicht/776d632c9e7ba1a6b12fa58f054ca5fc to your computer and use it in GitHub Desktop.
Main script of our vanilla JS popup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var btn = document.getElementById('modal_opener'); | |
var modal = document.querySelector('.modal'); | |
function attachModalListeners(modalElm) { | |
modalElm.querySelector('.close_modal').addEventListener('click', toggleModal); | |
modalElm.querySelector('.overlay').addEventListener('click', toggleModal); | |
} | |
function detachModalListeners(modalElm) { | |
modalElm.querySelector('.close_modal').removeEventListener('click', toggleModal); | |
modalElm.querySelector('.overlay').removeEventListener('click', toggleModal); | |
} | |
function toggleModal() { | |
var currentState = modal.style.display; | |
// If modal is visible, hide it. Else, display it. | |
if (currentState === 'none') { | |
modal.style.display = 'block'; | |
attachModalListeners(modal); | |
} else { | |
modal.style.display = 'none'; | |
detachModalListeners(modal); | |
} | |
} | |
btn.addEventListener('click', toggleModal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment