Swing Modal
Awesome modal with a swinging animation
CSSLab Is Awesome!
Modal content goes here!
<div class="swing-modal"> <div class="modal-background"> <div class="modal" id="csslab-swing-modal"> <h3 class="modal-header">CSSLab Is Awesome!</h3> <div class="modal-content"> <p>Modal content goes here!</p> <button class="modal-content-button modal-close">Close Modal</button> </div> </div> </div> <button class="modal-swing-button push-button-3d" data-id="csslab-swing-modal">Open Swing Modal</button> </div>
class Modal { constructor(modalButton) { this.element = modalButton; this.modalID = modalButton.dataset.id; this.modalNode = document.querySelector(`#${this.modalID}`); this.modalParent = this.modalNode.parentNode; this.open = false; this.modalInit(); } modalInit = () => { this.element.addEventListener('click', this.openModal); this.modalParent.addEventListener('mousedown', this.handleOverlayClick); let closeButtons = this.modalNode.querySelectorAll('.modal-close'); closeButtons.forEach(cur => cur.addEventListener('click', this.closeModal)) } openModal = () => { this.modalParent.classList.add('modal-background--active'); this.modalNode.classList.add('modal--active'); this.open = !this.open; } closeModal = () => { this.modalParent.classList.remove('modal-background--active'); this.modalNode.classList.remove('modal--active'); this.open = !this.open; } handleOverlayClick = (e) => { if (!e.target.closest('.modal')) { this.closeModal(); } } } let buttons = document.querySelectorAll('.modal-swing-button'); buttons.forEach(cur => new Modal(cur));
csslab
CSSLab is a platform dedicated to providing high-quality, scalable & reusable components that can be easily slotted into all kinds of web applications.
©2024 CSSLab, all rights reserved.