您现在的位置是:群英 > 开发技术 > web开发
JS实现可复用弹窗插件的方法是什么
Admin发表于 2022-05-18 17:32:27511 次浏览
这篇文章给大家介绍了“JS实现可复用弹窗插件的方法是什么”的相关知识,讲解详细,步骤过程清晰,对大家进一步学习和理解“JS实现可复用弹窗插件的方法是什么”有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。


本文实例为大家分享了javascript实现可复用弹窗插件的具体代码,供大家参考,具体内容如下

效果图

下面是详细代码

index.html

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>index</title>
        <link rel="stylesheet" href="./componet.css" >
    </head>
    <body>
        <button id="button"> 弹窗 </button>
        <script src="./componet.js"></script>
        <script>
            var btn = document.queryselector("#button");
            btn.addeventlistener("click", function() {
                new alertbox({
                    message: "哈哈哈哈哈哈",
                    success: "确认",
                    cancel: "取消",
                    successcallback: function() {
                        console.log("确认。。。。。。。。。")
                    },
                    cancelcallback: function() {
                        console.log("取消。。。。。。。。。。。")
                    }
                });
            })
        </script>
    </body>
</html>

componet.css

.alert-box{
    width: 250px;
    height: 150px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -75px 0 0 -125px;
    box-shadow: 0px 0px 10px 7px #ced6e0;
    border-radius: 5px;
    background-color: #ffffff;
}
 
.alert-box-message{
    height:108px;
    text-align: center;
    line-height: 108px;
    font-size: 14px;
}
 
.alert-box-buttonwrap{
    height: 40px;
    display: flex;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    overflow: hidden;
}
.alert-box-button{
    height: inherit;
    text-align: center;
    line-height: 40px;
    flex:1;
    cursor: pointer;
    font-size: 14px;
    background-color: white;
    border-top: solid 1px #ced6e0;
    transition: background-color 0.5s;
}
.alert-box-button:hover{
    background-color: #dee1e6;
    color: rgb(88, 88, 88);
}
.alert-box-button:nth-child(1){
    border-right: solid 1px #ced6e0;
}
 
.alert-box-show{
    -webkit-animation: alert-show 0.3s;
    -webkit-animation-fill-mode: forwards;
    animation: alert-show 0.3s;
    animation-fill-mode: forwards;
}
.alert-box-hidden{
    -webkit-animation: alert-hidden 0.3s;
    -webkit-animation-fill-mode: forwards;
    animation: alert-hidden 0.3s;
    animation-fill-mode: forwards;
} 
 
@keyframes alert-show{
    from{transform: scale(0);}
    to{transform: scale(1);}
}
@-webkit-keyframes alert-show{
    from{transform: scale(0);}
    to{transform: scale(1);}
}
 
@keyframes alert-hidden{
    from{transform: scale(1);}
    to{transform: scale(0);}
}
@-webkit-keyframes alert-hidden{
    from{transform: scale(1);}
    to{transform: scale(0);}
}

componet.js

function alertbox(options) {
    this.message = options.message;
    this.callback = options.callback;
    this.success = options.success;
    this.cancel = options.cancel;
    this.successcallback = options.successcallback;
    this.cancelcallback = options.cancelcallback;
    this.createbox();
    this.buttonaddevent();
}
alertbox.prototype.createbox = function() {
    let body = document.getelementsbytagname("body")[0];
    this.fragment = document.createdocumentfragment();
    this.box = cre("div");
    this.box.classlist.add("alert-box", "alert-box-show");
    let message = cre("div");
    message.textcontent = this.message;
    message.classlist.add("alert-box-message");
    this.box.appendchild(message);
    let buttonwrap = cre("div");
    buttonwrap.classlist.add("alert-box-buttonwrap");
    this.successbtn = cre("div");
    this.successbtn.classlist.add("alert-box-button");
    this.successbtn.textcontent = this.success || "确认";
    buttonwrap.appendchild(this.successbtn);
    if (this.cancel) {
        this.cancelbtn = cre("div");
        this.cancelbtn.classlist.add("alert-box-button");
        this.cancelbtn.textcontent = this.cancel;
        buttonwrap.appendchild(this.cancelbtn);
    }
    this.box.appendchild(buttonwrap);
    this.fragment.appendchild(this.box);
    body.appendchild(this.fragment);
}
 
alertbox.prototype.buttonaddevent = function() {
    this.successbtn.addeventlistener("click", () => {
        let fn = this.successcallback;
        fn();
        this.box.classlist.add("alert-box-hidden");
        settimeout(() => {
            reme(this.box);
        }, 310)
    });
    if (this.cancel) {
        this.cancelbtn.addeventlistener("click", () => {
            let fn = this.cancelcallback;
            fn();
            this.box.classlist.add("alert-box-hidden");
            settimeout(() => {
                reme(this.box);
            }, 310)
        });
    }
}
 
function cre(element) {
    return document.createelement(element);
}
 
function reme(element) {
    document.body.removechild(element);
}



关于“JS实现可复用弹窗插件的方法是什么”的内容就介绍到这,感谢各位的阅读,相信大家对JS实现可复用弹窗插件的方法是什么已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: js,弹窗
相关信息推荐
2022-06-16 09:25:23 
摘要:判断方法:1、去除数组中的重复值,语法“$newArr=array_unique($arr);”;2、获取去重后数组的长度,如果数组长度为1,则原数组中的元素都相同,语法“if(count($newArr)==1){//都相同时的操作}”。
2022-07-04 17:44:35 
摘要:这篇文章主要为大家介绍了Golang高性能持久化解决方案BoltDB数据库介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
2022-06-21 17:02:38 
摘要:本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于类数组和可迭代对象的实现原理,包括了把对象本身构造成迭代器、String的迭代器等等相关内容,下面一起来看一下吧,希望对大家有帮助。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部