首页 > 其他分享 >前天为群友写的,使用面向对象的写法封装一个Modal Dialogue Box

前天为群友写的,使用面向对象的写法封装一个Modal Dialogue Box

时间:2022-09-24 19:11:49浏览次数:51  
标签:Box modelEle Dialogue const 群友 style height cancelButtonEle confirmButtonEle

image

q群里的群友提出的问题 “如何使用面向对象的语法封装一个Modal框”
我们都知道,现在使用vue ,都是用组件去封装的,
怎么用对象去封装呢?我比较感兴趣,做了以下尝试
下面直接上代码

点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>new1Model</title>
</head>
<body>
    <script>
        class Model {
            constructor({height=200,width=600,title='标题',cancelText='取消',confirmText='确认',content='无'}={}) {
                this.height = height;
                this.width = width;
                this.title = title;
                this.cancelText = cancelText;
                this.confirmText = confirmText;
                this.content = content
                this.isDesroty = false
            }
            create(){
                this.isDesroty = false
                // body
                const bodyEle = document.getElementsByTagName('body')[0]
                this.bodyEle = bodyEle
                //主体
                const modelEle = document.createElement('div')
                this.modelEle = modelEle
                modelEle.style.zIndex = '999'
                modelEle.style.position = 'absolute'
                modelEle.style.top = '50%'
                modelEle.style.left = '50%'
                modelEle.style.transform = `translate(calc(-50% - ${Math.random() > 0.5? - 100 * Math.random():100 *Math.random()}px),calc(-50% - ${Math.random() > 0.5? - 100 * Math.random():100 *Math.random()}px))`
                modelEle.style.height = this.height + 'px'
                modelEle.style.width = this.width + 'px'
                modelEle.style.backgroundColor = '#fff'
                modelEle.style.display = 'flex'
                modelEle.style.flexDirection = 'column'
                modelEle.style.alignContent = 'space-between'
                modelEle.style.border = '1px solid #999'

                
                //顶部
                const topEle = document.createElement('div')
                topEle.style.height = '30px'
                topEle.style.lineHeight = '30px'
                topEle.style.borderBottom = '1px solid #999'
                topEle.innerText = this.title

                //中间
                const middleEle = document.createElement('div')
                middleEle.innerText = this.content
                middleEle.style.flex = 1

                //底部
                const bottomEle = document.createElement('div')
                bottomEle.style.height = '40px'
                bottomEle.style.display = 'flex'
                bottomEle.style.borderTop = '1px solid #999'
                bottomEle.style.justifyContent = 'flex-end'
                
                //确认按钮
                const confirmButtonEle = document.createElement('div')
                confirmButtonEle.style.marginLeft = '10px'
                confirmButtonEle.style.height = '40px'
                confirmButtonEle.style.width = '120px'
                confirmButtonEle.style.backgroundColor = 'blue'
                confirmButtonEle.style.color = 'white'
                confirmButtonEle.style.display = 'flex'
                confirmButtonEle.style.justifyContent = 'center'
                confirmButtonEle.style.alignItems = 'center'
                
                confirmButtonEle.innerText = this.confirmText
                
                
                //取消按钮
                const cancelButtonEle = document.createElement('div')
                cancelButtonEle.style.marginLeft = '10px'
                cancelButtonEle.style.height = '40px'
                cancelButtonEle.style.width = '120px'
                cancelButtonEle.style.backgroundColor = 'red'
                cancelButtonEle.style.color = 'white'
                cancelButtonEle.style.display = 'flex'
                cancelButtonEle.style.justifyContent = 'center'
                cancelButtonEle.style.alignItems = 'center'

                cancelButtonEle.innerText = this.cancelText

                bottomEle.appendChild(confirmButtonEle)
                bottomEle.appendChild(cancelButtonEle)
                modelEle.appendChild(topEle)
                modelEle.appendChild(middleEle)
                modelEle.appendChild(bottomEle)
                
                //挂载到页面上
                bodyEle.appendChild(modelEle)
            }
            destory(){
                this.bodyEle.removeChild(this.modelEle)
                this.isDesroty = true
            }
        }
       const model1 = new Model()
       model1.create()
       setInterval(() => {
           if(model1.isDesroty){
               model1.create()
           }else{
            model1.destory() 
           }
           
       }, 1 * 1000);
    </script>
</body>
</html>

标签:Box,modelEle,Dialogue,const,群友,style,height,cancelButtonEle,confirmButtonEle
From: https://www.cnblogs.com/justin999/p/16726290.html

相关文章

  • SAP ABAP ALV 的一些总结:Docking container 和 Dialogbox container
    Dockingcontainer停靠容器(CL_GUI_DOCKING_CONTAINER)不需要任何父容器,自定义屏幕上的自定义容器区域也不需要。创建和显示后,它停靠在屏幕的四个位置之一:顶部、底部、左侧......
  • 使用 MLBox 探索 AutoML
    使用MLBox探索AutoML作为数据科学家或机器学习工程师,有时我们想要自动化我们的一些任务。这就是AutoML的用武之地。AutoML只是从ML过程开始到结束运行的过程,并使......
  • 无法将类型string隐式转换为textbox wpf中
    大致意思是这样的,想把我获取到的字符串放入textbox中,然后我就写了这么一句a.textbox="获取到的字符串"  然后就有了下面的错误,啥也不说,强转a.textbox=(Textbox)"获取......
  • 关于 VirtualBox 虚拟机安装 Ubuntu 增强功能的各种坑
    VirtualBox是一个免费的虚拟机软件,我在上面安装过CentOS和Ubuntu,VirtualBox给CentOS安装“增强功能”是完全没问题的,复制双向、拖拽双向、屏幕自适应功能都可以正常......
  • VirtualBox-kernel-driver-not-installed-error(rc=-1908)
    在debian中更新的软件后发现virtualbox无法启动虚拟机,抛出错误说内核驱动未安装,请重新运行vboxconfig,多次运行或重启后无法解决错误,检查/usr/src目录发现当前内......
  • virtual box 下 Ddebian11自动挂载共享文件夹
    VBox共享文件夹添加方法网上有很多,此处不再赘述。只记录自动挂载方法。开机自动挂载,可以在  /etc/fstab 文件末添加一项Shared/home/username/SharedDirvboxsfrw,......
  • ENSP报错AR40 vbox提示错误NtCreateFile(\Device\VBoxDrvStub)
    在VBOX中打开AR提示如下:  此问题的产生一般是在vbox低版本卸载,然后重装新版本后出现。这个问题跟ENSP没关系,是VBox的问题。解决方法:在win10或者win11中首先卸载原......
  • FCM-toolbox
    FCM-toolbox:GitHub-SimonMarquis/FCM-toolbox:......
  • css-flexbox
    在缩放整个页面的时候,里面的元素可以随着页面缩放而变化设置display属性为flex就可以了display:flex;  决定容器中的主轴方向用flex-directionex:flex-direction......
  • css box model
        HTML:  css:  效果: 一起设置: border:widthstylecolor;exaple:  border:mediumdashedgreen; border-radius可以把四周钝化:  ......