JavaScript singleton mode to implement custom pop-up box

JavaScript singleton mode to implement custom pop-up box

This article shares the specific code of JavaScript singleton mode to implement custom pop-up boxes for your reference. The specific content is as follows

Function

  • Customize pop-up title
  • Customize pop-up content
  • Customize the callback function for confirming and closing the pop-up window

Complete code

const Dialog = (function () {
 class Dialog {
   constructor () {
     this.ele = document.createElement('div')
     this.ele.className = 'dialog'
     document.body.appendChild(this.ele)
     this.callback = null
     this.setEvent()
   }
 
   setContent ({ text, topicText, confirmText, cancelText } = options) {
     this.ele.innerHTML = null
     const top = document.createElement('div')
     top.className = 'top'
     const topic = document.createElement('span')
     topic.className = 'topic'
     topic.innerHTML = topicText
     const close = document.createElement('span')
     close.className = 'close'
     close.innerHTML = '×'
     top.appendChild(topic)
     top.appendChild(close)
     const middle = document.createElement('div')
     middle.className = 'middle'
     const content = document.createElement('div')
     content.className = 'content'
     content.innerHTML = text
     middle.appendChild(content)
     const bottom = document.createElement('div')
     bottom.className = 'bottom'
     const confirm = document.createElement('button')
     confirm.className = 'confirm'
     confirm.innerHTML = confirmText
     const cancel = document.createElement('button')
     cancel.className = 'cancel'
     cancel.innerHTML = cancelText
     bottom.appendChild(confirm)
     bottom.appendChild(cancel)
     const wrap = document.createElement('div')
     this.ele.appendChild(top)
     this.ele.appendChild(middle)
     this.ele.appendChild(bottom)
     this.ele.style.display = 'block'
   }
 
   setEvent () {
     this.ele.addEventListener('click', e => {
       e = e || window.event
       const target = e.target || e.srcElement
       if (target.className === 'close') {
         this.ele.style.display = 'none'
         console.log('close')
       }
       if (target.className === 'confirm') {
         this.ele.style.display = 'none'
         this.callback(true)
       }
       if (target.className === 'cancel') {
         this.ele.style.display = 'none'
         this.callback(false)
       }
     })
   }
 }
 let instance = null
 return function (options, cb) {
   if (!instance) instance = new Dialog()
   instance.setContent(options)
   instance.callback = cb || function () {}
   return instance
 }
 })()
 
 const dialog = new Dialog({
 text: 'prompt text',
 topicText: 'Title',
 confirmText: 'Confirm',
 cancelText: 'Cancel'
 }, res => { console.log(res) })

Rendering

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • js+html5 realizes the effect of semi-transparent mask layer pop-up box
  • Toast usage in vue.js and example code using toast pop-up box
  • Encapsulation of js custom pop-up box plug-in
  • Easily implement js pop-up box display options
  • Vue.js implements pop-up window only once
  • js to achieve the effect of up, down, left, and right pop-up boxes
  • Implement the delivery address pop-up box selection based on layer.js and return the corresponding address information
  • JavaScript to achieve a pop-up box that cannot be closed
  • Example code of Bootstrap and Angularjs with homemade pop-up box
  • js rewrites the alert event (avoiding the URL appearing in the alert pop-up box title)

<<:  Sharing experience on MySQL slave maintenance

>>:  Proxy_pass method in multiple if in nginx location

Recommend

VUE implements timeline playback component

This article example shares the specific code of ...

MySQL green decompression version installation and configuration steps

Steps: 1. Install MySQL database 1. Download the ...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

Graphical explanation of the function call of proto file in Vue

1. Compile proto Create a new proto folder under ...

A bug fix for Tomcat's automatic shutdown

Preface Recently, a Java EE web project that has ...

Tutorial on installing Pycharm and Ipython on Ubuntu 16.04/18.04

Under Ubuntu 18.04 1. sudo apt install python ins...

Summary of some reasons why crontab scheduled tasks are not executed

Preface I recently encountered some problems at w...

Hexadecimal color codes (full)

Red and pink, and their hexadecimal codes. #99003...

Design theory: the basics of font design

<br />Words are the inevitable product of hu...

Docker View Process, Memory, and Cup Consumption

Docker view process, memory, cup consumption Star...

How to use gdb to debug core files in Linux

1.core file When a Segmentation fault (core dumpe...

Detailed steps to install mysql 8.0.18-winx64 on win10

1. First go to the official website to download t...

Nginx Location Configuration Tutorial from Scratch

Basics The matching order of location is "ma...