javascript实现好看的可复用弹窗插件

 更新时间:2022年05月08日 13:53:03   作者:mondayes  
这篇文章主要为大家详细介绍了javascript实现好看的可复用弹窗插件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • JavaScript编写一个简易购物车功能

    JavaScript编写一个简易购物车功能

    这篇文章主要为大家详细介绍了JavaScript简易购物车功能的编写代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Bootstrap modal使用及点击外部不消失的解决方法

    Bootstrap modal使用及点击外部不消失的解决方法

    这篇文章主要为大家详细介绍了Bootstrap modal使用及点击外部不消失的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • 使用js画图之画切线

    使用js画图之画切线

    这篇文章主要介绍了使用js绘制几何图形系列教程中的画切线篇,非常方便我们好好熟悉理解javascript,推荐给大家
    2015-01-01
  • JS跨域请求的问题解析

    JS跨域请求的问题解析

    在本篇内容里小编给大家整理了关于解决JS跨域请求的问题知识点,需要的朋友们参考学习下。
    2018-12-12
  • js charAt的使用示例

    js charAt的使用示例

    这篇文章主要介绍了js charAt的使用方法,需要的朋友可以参考下
    2014-02-02
  • javaScript之split与join的区别(详解)

    javaScript之split与join的区别(详解)

    下面小编就为大家带来一篇javaScript之split与join的区别(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • 延时加载JavaScript代码提高速度

    延时加载JavaScript代码提高速度

    这篇文章主要介绍了延时加载JavaScript代码提高速度的相关资料,需要的朋友可以参考下
    2015-12-12
  • 可拖拽组件slider.js使用方法详解

    可拖拽组件slider.js使用方法详解

    这篇文章主要为大家详细介绍了可拖拽组件slider.js的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • webpack 如何同时输出压缩和未压缩的文件的实现步骤

    webpack 如何同时输出压缩和未压缩的文件的实现步骤

    这篇文章主要介绍了webpack 如何同时输出压缩和未压缩的文件的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • layui复选框的全选与取消实现方法

    layui复选框的全选与取消实现方法

    今天小编就为大家分享一篇layui复选框的全选与取消实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09

最新评论