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);
}

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

(0)

相关推荐

  • js弹出框、对话框、提示框、弹窗实现方法总结(推荐)

    一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹出对话框并输出一段提示信息 function ale() { //弹出一个对话框 alert("提示信息!"); } //弹出一个询问框,有确定和取消按钮 function firm() { //利用对话框返回的值 (true 或者 false) if (confirm("你确定提交吗?")) { aler

  • js弹窗返回值详解(window.open方式)

    test.php 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-

  • JS弹窗 JS弹出DIV并使整个页面背景变暗功能的实现代码

    1.首先写一个遮罩层div,然后再写一个弹窗的div <!-- 遮罩层 --> <div id="cover" style="background: #000; position: absolute; left: 0px; top: 0px; width: 100%; filter: alpha(opacity=30); opacity: 0.3; display: none; z-index: 2 "> </div> <!

  • js点击弹出div层实现可拖曳的弹窗效果

    弹出层.弹窗效果+拖曳功能 *{ margin:0px; padding:0px;} body{ font-size:12px; font:Arial, Helvetica, sans-serif; margin:25PX 0PX; background:#eee;} .botton{ color:#F00; cursor:pointer;} .mybody{width:600px; margin:0 auto; height:1500px; border:1px solid #ccc; pad

  • 关于vue.js弹窗组件的知识点总结

    首先在开发时需要考虑以下三点: 1.进入和弹出的动画效果. 2.z-index 的控制 3.overlay 遮盖层 关于动画 vue 对于动画的处理相对简单,给组件加入css transition 动画即可 <template> <div class="modal" transition="modal-scale"> <!--省略其它内容--> </div> </template> <script&g

  • JS实现自定义弹窗功能

    众所周知,浏览器自带的原生弹窗很不美观,而且功能比较单一,绝大部分时候我们都会按照设计图自定义弹窗或者直接使用注入layer的弹窗等等.前段时间在 慕课网 上看到了一个自定义弹窗的实现,自己顺便就学习尝试写了下,下面是主要的实现代码并添加了比较详细的注释,分享出来供大家参考.(代码用了ES6部分写法如需兼容低版本浏览器请把相关代码转成es5写法,后面有时间更新为一个兼容性较好的es5版本) HTML部分:(没什么内容 放置一个按钮调用函数,js中调用实例即可供参考) <!DOCTYPE html

  • js弹窗代码 可以指定弹出间隔

    代码如下: 复制代码 代码如下: <SCRIPT LANGUAGE="javascript"> var Time=10; //设置每次弹出的相格的时间以秒为单位,现在是一天 function Set(){ var Then=new Date(); Then.setTime(Then.getTime()+Time*1000); document.cookie="netbei=1;expires="+Then.toGMTString(); } var coo

  • js调用父框架函数与弹窗调用父页面函数的简单方法

    调用父级中的 aaa的函数 子页面中: onclick="window.parent.frames.aaa()" 父页面中: function aaa() { alert('bbbbb'); } frame框架里的页面要改其他同框架下的页面或父框架的页面就用parent window.opener引用的是window.open打开的页面的父页面. window.frames对象可以引用iframe里的页面,也可以引用frameset里的页面. 可以这样 window.frames[0]

  • js退出弹窗代码集合

    var leave=true; function stbs() { if (leave) stb.DOM.Script.window.open('http://www.jb51.net/','_blank'); } [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 支持 Windows XP SP2 xp sp3(即支持IE7)的超级弹退代码 使用说明: 1. 把SuperExitPopup.js这行 var popURL1 = 'http://tv.cnzz.cc'; 里面的网址改成

  • JS使用遮罩实现点击某区域以外时弹窗的弹出与关闭功能示例

    本文实例讲述了JS使用遮罩实现点击某区域以外时弹窗的弹出与关闭功能.分享给大家供大家参考,具体如下: HTML部分: <div id="div">点击除开div的区域可以弹出弹窗</div> <div id="cover"></div> <div id="box">点击除开div和弹窗的区域可以关闭弹窗</div> CSS部分: #div{ /*设置z-index属性必须设置

随机推荐