Devm
Devendra Maharjan
Profile
Contact
Tutorials
Javascript
Pass method as parameter
Arrow method for calling async method
Create popup
Linux
Hide output in command
Apply command to output of command
Install LAMP
Magento 2 deploy script
PHP
Bash
CSS
Create line with div
Dynamic icon
Mail
Linkedin
Git Hub
Close
Create modal popup in javascript
class modal { param; enabled; container; content; header; popup; close; constructor(param){ this.param = param; } create(){ this.container = document.createElement('div'); this.popup = document.createElement('div'); this.content = document.createElement('div'); this.header = document.createElement('div'); this.close = document.querySelector('.close'); this.container.style.display = 'none'; this.popup.style.display = 'none'; const lineDiv = document.createElement('div'); lineDiv.style.width = '95%'; lineDiv.style.borderTop = '1px solid #ddd'; lineDiv.style.margin= '30px auto'; const container = this.container; const popup = this.popup; const content = this.content; const header = this.header; const close = this.close; container.style.height = document.documentElement.scrollHeight+'px'; container.style.width = screen.width+'px'; container.style.position = 'absolute'; container.style.top = '0'; container.style.backgroundColor = '#000'; container.style.opacity = '0.5'; popup.style.height = '80%'; popup.style.width = '80%'; popup.style.top = '0'; popup.style.bottom = '0'; popup.style.left = '0'; popup.style.right = '0'; popup.style.margin = 'auto'; popup.style.position = 'absolute'; popup.style.backgroundColor = '#fff'; header.style.height = Math.floor(2.5 * (80 * screen.width/100) / 100) + 'px'; header.style.width = '95%'; header.style.position = 'relative'; close.style.position = 'absolute'; close.style.right = '0'; close.style.display = 'block'; header.style.paddingTop = Math.floor(2.5 * (80 * screen.width/100) / 100) + 'px'; header.style.margin = 'auto'; header.style.position = 'relative'; const popupHeight = (60 * screen.height/100) ; content.classList.add('modal-content'); content.style.margin = 'auto'; content.style.height = ((80 * popupHeight/100) - Math.floor(2.5 * (80 * screen.height/100) / 100)) + 'px'; content.style.width = '95%'; content.style.overflowY = 'scroll'; content.style.overflow = 'auto'; document.body.append(container); document.body.append(popup); header.append(close); popup.append(header); popup.append(lineDiv); popup.append(content); close.addEventListener('click', (e) => { e.preventDefault(); this.hide(); }) } show(){ this.enabled = true; this.container.style.display = 'block'; this.popup.style.display = 'block'; } hide(){ this.enabled = false; this.container.style.display = 'none'; this.popup.style.display = 'none'; } enabled(){ return this.enabled ; } } const modalObj = new modal({}); modalObj.create();