模态框封装(弹窗)

目的:练习面向对象写插件(模态框)

1.多个模态框一样的,而且每次点击都会出来一个,怎么做呢?

构造函数。把模态框封装一个构造函数model,每次new都会产出一个模态框,所以点击不同的按钮就是在做new模态框,实例化。

2.模态框有什么功能?打开功能(显示),关闭功能,而且每个模态框都包含着两个功能

  • open功能

  • close功能

问:open和close方法写到哪里?

构造函数Model的原型对象上,共享方法

BUG:多次点击会显示很多模态框

解决:准备open显示的时候,先判断页面中有没有modal盒子,有就移除,没有就添加

话不多数,上代码。

最主要的就是JS部分,由于代码比较短,这里将JS放在HTML写

<!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>Document</title>
    <link rel="stylesheet" href="../模态框封装css/模态框.css">
</head>

<body>
    <button id="delete">删除</button>
    <button id="login">登录</button>

    <!-- <div class="modal">
        <div class="header">温馨提示<i></i></div>
        <div class="body">您没有删除权限操作</div>
    </div> -->
    <script>
        //1.Modal 构造函数封装 -模态框
        function Modal(title = '', message = '') {
            // console.log(title,message);
            //创建Modal模态框盒子
            //1.1 创建div标签
            this.modalBox = document.createElement('div')
            //1.2 给div标签添加类名为modal
            this.modalBox.className = "modal"
            //1.3 model盒子内部填充两个div标签,并且修改文字内容
            this.modalBox.innerHTML = `
            <div class="close"><i>X</i></div>
            <div class="header">${title}</div>
            <div class="body">${message}</div>
             `
            console.log(this.modalBox);
        }
        // new Modal("温馨提示","你没有权限删除")
        // new Modal("友情提示","你还没有登录")
        //2.给构造函数原型对象挂载open方法
        Modal.prototype.open = function () {
            //先来判断页面中是否有modal盒子,如果有就先删除,否则继续添加
            const box=document.querySelector('.modal')
            box&&box.remove()
            //注意这个函数不要使用箭头函数,因为要用到this指向
            //把刚才创建的modalBox显示到页面body中
            document.body.append(this.modalBox)
            //要等着盒子显示出来,就可以绑定点击事件了,所以close一定要在open中写
            this.modalBox.querySelector('i').addEventListener('click',()=>{
                //这个地方用箭头函数,此处this指向上一级,也就是实例对象
                this.close();
            })
        }
        //3.给构造函数原型对象挂载close方法
        Modal.prototype.close = function () {
            this.modalBox.remove()

        }

        //测试一下 点击  删除按钮
        document.querySelector('#delete').addEventListener('click', () => {
            //先调用Modal构造函数
            const del = new Modal("温馨提示:", "您目前没有权限删除~");
            //实例对象调用open方法
            del.open()
        })
        //测试一下 点击  登录按钮
        document.querySelector('#login').addEventListener('click', () => {
            //先调用Modal构造函数
            const del = new Modal("友情提示:", "您还没有注册呢~");
            //实例对象调用open方法
            del.open()
        })
    </script>
</body>

</html>

Scss部分

.modal {
    margin: 0 auto;
    width: 300px;
    height: 180px;
    background-color: rgb(255, 255, 255);
    border-radius: 3px;
    display: flex;
    flex-direction: column;
    font-size: 22px;
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    padding: 20px 30px;
    box-shadow: 2px 2px 2px 3px rgb(212, 211, 211);
    .close{
        width: 100%;
        height:20px;
        i{
            display: block;
            width: 20px;
            height: 20px;
            float: right;
            cursor: pointer;
            color: rgb(108, 104, 104);
        }
    }
    .header {
        width: 100%;
        height: 30%;
        line-height: 60px;
       
    }
    .body {
        width: 100%;
        height: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

注意:这个小案例没有加遮罩,弹窗显示,还能点击页面中其他功能。不过点击归点击,页面始终最多显示一个弹窗。

也可以加个遮罩。open的时候,显示遮罩,close的时候,关闭遮罩,下面就是更改后的内容

<!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>Document</title>
    <link rel="stylesheet" href="../模态框封装css/模态框.css">
</head>

<body>
    <div class="zhezhao"></div>
    <button id="delete">删除</button>
    <button id="login">登录</button>

    <!-- <div class="modal">
        <div class="header">温馨提示<i></i></div>
        <div class="body">您没有删除权限操作</div>
    </div> -->
    <script>
        //获取一下遮罩
        var zhezhao=document.querySelector(".zhezhao")
        //1.Modal 构造函数封装 -模态框
        function Modal(title = '', message = '') {
            // console.log(title,message);
            //创建Modal模态框盒子
            //1.1 创建div标签
            this.modalBox = document.createElement('div')
            //1.2 给div标签添加类名为modal
            this.modalBox.className = "modal"
            //1.3 model盒子内部填充两个div标签,并且修改文字内容
            this.modalBox.innerHTML = `
            <div class="close"><i>X</i></div>
            <div class="header">${title}</div>
            <div class="body">${message}</div>
             `
            console.log(this.modalBox);
        }
        // new Modal("温馨提示","你没有权限删除")
        // new Modal("友情提示","你还没有登录")
        //2.给构造函数原型对象挂载open方法
        Modal.prototype.open = function () {
            //先来判断页面中是否有modal盒子,如果有就先删除,否则继续添加
            const box=document.querySelector('.modal')
            box&&box.remove()
            //注意这个函数不要使用箭头函数,因为要用到this指向
            //把刚才创建的modalBox显示到页面body中
            document.body.append(this.modalBox)
            //要等着盒子显示出来,就可以绑定点击事件了,所以close一定要在open中写
            this.modalBox.querySelector('i').addEventListener('click',()=>{
                //这个地方用箭头函数,此处this指向上一级,也就是实例对象
                this.close();
            })
        }
        //3.给构造函数原型对象挂载close方法
        Modal.prototype.close = function () {
            this.modalBox.remove()
            zhezhao.style.display="none"
        }

        //测试一下 点击  删除按钮
        document.querySelector('#delete').addEventListener('click', () => {
            //先调用Modal构造函数
            const del = new Modal("温馨提示:", "您目前没有权限删除~");
            //实例对象调用open方法
            del.open()
            zhezhao.style.display="block"
        })
        //测试一下 点击  登录按钮
        document.querySelector('#login').addEventListener('click', () => {
            //先调用Modal构造函数
            const del = new Modal("友情提示:", "您还没有注册呢~");
            //实例对象调用open方法
            del.open()
            zhezhao.style.display="block"
        })
    </script>
</body>

</html>
.modal {
    margin: 0 auto;
    width: 300px;
    height: 180px;
    position: relative;
    background-color: rgb(255, 255, 255);
    border-radius: 3px;
    display: flex;
    flex-direction: column;
    font-size: 22px;
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    padding: 20px 30px;
    box-shadow: 2px 2px 2px rgb(212, 211, 211);
    z-index: 10000000;
    .close{
        width: 100%;
        height:20px;
        i{
            display: block;
            width: 20px;
            height: 20px;
            float: right;
            cursor: pointer;
            color: rgb(108, 104, 104);
        }
    }
    .header {
        width: 100%;
        height: 30%;
        line-height: 60px;
       
    }
    .body {
        width: 100%;
        height: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
.zhezhao{
    width: 100%;
    height: 100%;
    display: none;
    position: absolute;
    z-index: 999;
    background-color: rgb(38, 38, 38);
    opacity: 0.5;
}

至于效果可以手动复制粘贴试一下

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一款流行的JavaScript框架,它在构建用户界面方面非常强大。以下是一个示例代码,用于封装一个基本的弹窗组件: ```javascript <template> <div> <button @click="showModal">打开弹窗</button> <div v-if="isOpen" class="modal"> <div class="modal-content"> <slot></slot> <button @click="closeModal">关闭弹窗</button> </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { name: 'Modal', setup() { const isOpen = ref(false); const showModal = () => { isOpen.value = true; }; const closeModal = () => { isOpen.value = false; }; return { isOpen, showModal, closeModal }; }, }; </script> <style scoped> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-content { background-color: white; padding: 20px; } button { margin-top: 10px; } </style> ``` 这个弹窗组件包含了一个按钮和一个模态框。当点击按钮时,模态框会显示出来,并且渲染出插槽内容。关闭按钮可以用于关闭模态框。在模态框外部点击也可以关闭模态框。 这个组件使用Vue3的Composition API来定义逻辑。通过ref函数创建一个响应式引用isOpen,用于跟踪模态框的开启和关闭状态。showModal方法用于打开模态框,closeModal方法用于关闭模态框。使用slot插槽来动态渲染弹窗内容。 在样式上,将模态框设置为固定定位,并使用背景色来实现半透明遮罩效果。模态框内容使用白色背景并设置内边距。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值