美文网首页
封装弹出框插件

封装弹出框插件

作者: 你怀中的猫 | 来源:发表于2022-05-14 16:36 被阅读0次

封装思想

1、先封装一个Alerts类

用来生成弹出框的页面结构

  • 1.1 属性:
    提示头
    提示信息
    字体颜色
  • 1.2 方法:
    创建页面结构
    头部提示模块
    提示信息内部展示
    按钮模块
    成功按钮
    取消按钮
2、封装子类

例:成功

  • 2.1 属性
    提示头 type
    提示信息 text
    字体颜色 color
3、、封装工厂

插件js代码

// 先封装一个 Alerts()
function Alerts() { }

// 给父类中 封装一个创建页面结构的方法
Alerts.prototype.create = function () {
    // 1. 先创建蒙版
    var mask = document.createElement('div');
    mask.className = 'mask';
    mask.id = 'mask';
    // 2. 设置css样式
    mask.style.width = '100%';
    mask.style.height = '100%';
    mask.style.position = 'absolute';
    mask.style.top = '0px';
    mask.style.left = '0px';
    mask.style.background = 'rgba(0,0,0,0.5)';
    mask.style.zIndex = '999999999999999999';

    // 3. 将蒙版 加入到 body
    var bodys = document.getElementsByTagName('body')[0];
    bodys.appendChild(mask);

    // 4. 创建内容区域
    var fbox = document.createElement('div');
    fbox.className = 'fbox';
    fbox.id = 'fbox';
    // 给内容区域 设置 样式
    fbox.style.width = '430px';
    fbox.style.height = '300px';
    fbox.style.position = 'absolute';
    fbox.style.top = '0px';
    fbox.style.left = '0px';
    fbox.style.right = '0px';
    fbox.style.bottom = '0px';
    fbox.style.margin = 'auto';
    fbox.style.background = '#fff';
    fbox.style.borderRadius = '10px';
    fbox.style.overflow = 'hidden';


    //添加至mask中
    mask.appendChild(fbox);
    // 给内容区域添加头部
    fbox.appendChild(this.top());
    // 给内容区域添加提示信息区域
    fbox.appendChild(this.cont());
    // 给内容区域添加按钮区域
    fbox.appendChild(this.bottom());
}


// 给Alerts添加一个创建头部的方法
Alerts.prototype.top = function () {
    var div = document.createElement('div');
    div.className = 'top';
    div.style.height = '50px';
    div.style.width = '90%';
    div.style.padding = '0 5%';
    div.style.borderBottom = '1px solid #000';
    div.style.fontSize = '24px';
    div.style.fontWeight = 'bold';
    div.style.lineHeight = '50px';
    div.style.color = this.color;
    div.innerText = this.type;
    return div;
}

// 给Alerts添加一个创建提示消息的方法
Alerts.prototype.cont = function () {
    var div = document.createElement('div');
    div.className = 'cont';
    div.style.height = '160px';
    div.style.width = '90%';
    div.style.padding = '20px 5% 0px';
    div.style.borderBottom = '1px solid #000';
    div.style.fontSize = '18px';
    div.style.lineHeight = '40px';
    div.style.color = this.color;
    div.innerText = this.text;
    return div;
}

//给Alerts添加一个创建按钮区域的方法
Alerts.prototype.bottom = function () {
    var div = document.createElement('div');
    div.className = 'bottom';
    div.style.width = '90%';
    div.style.padding = '10px 5% 0px';
    div.style.fontSize = '16px';
    div.style.lineHeight = '40px';

    //添加成功按钮
    div.appendChild(this.sure());
    //如this.flag = true; 添加取消按钮
    if (this.flag) {
        div.appendChild(this.del());
    }
    return div;
}


// 给Alerts添加一个创建成功按钮的方法
Alerts.prototype.sure = function () {
    var div = document.createElement('div');
    div.className = 'sure';
    div.style.width = '110px';
    div.style.height = '40px';
    div.style.fontSize = '16px';
    div.style.lineHeight = '40px';
    div.style.color = '#fff';
    div.style.backgroundColor = '#007aff';
    div.style.borderRadius = '5px';
    div.style.textAlign = 'center';
    div.style.marginLeft = '20px';
    div.style.float = 'right';
    div.style.cursor = 'pointer';
    div.innerText = '确认';
    //用that接收以下this
    var that = this;
    //给成功按钮添加一个点击方法,点击后移出
    div.onclick = function () {
        mask.remove();
        that.callback();
    }
    return div;
}

//给Alerts添加一个创建取消按钮的方法
Alerts.prototype.del = function () {
    var div = document.createElement('div');
    div.className = 'del';
    div.style.width = '110px';
    div.style.height = '40px';
    div.style.fontSize = '16px';
    div.style.lineHeight = '40px';
    div.style.color = '#333';
    div.style.backgroundColor = '#ccc';
    div.style.borderRadius = '5px';
    div.style.textAlign = 'center';
    div.style.marginLeft = '20px';
    div.style.float = 'right';
    div.style.cursor = 'pointer';
    div.innerText = '取消';
    //给取消按钮添加一个点击方法,点击后移出
    div.onclick = function () {
        mask.remove();
    }
    return div;
}

//创建一个成功类
function Success(text) {
    this.type = '成功';
    this.text = text;
    this.color = 'green';
    this.flag = false;
}
//继承父类
Success.prototype = new Alerts();
//恢复构造函数指向
Success.prototype.constructor = Success;

//创建一个失败类
function Err(text) {
    this.type = '失败';
    this.text = text;
    this.color = 'red';
    this.flag = false;
}
//继承父类
Err.prototype = new Alerts();
//恢复构造函数指向
Err.prototype.constructor = Err;

//创建一个提示类
function Tip(text, fl, callback) {
    this.type = '提示';
    this.text = text;
    this.color = 'orange';
    this.flag = fl || false;
    this.callback = callback;
}
//继承父类
Tip.prototype = new Alerts();
//恢复构造函数指向
Tip.prototype.constructor = Tip;

//封装工厂函数
function aler(type, text, fl) {
    var obj = {};
    switch (type) {
        case '成功':
            obj = new Success(text);
            break;
        case '失败':
            obj = new Err(text);
            break;
        case '提示':
            obj = new Tip(text, fl);
            break;
    }

    //生成弹出框
    obj.create();
    return obj;
}


function aler(O) {
    var obj = {};
    switch (O.type) {
        case '成功':
            obj = new Success(O.text);
            break;
        case '失败':
            obj = new Err(O.text);
            break;
        case '提示':
            obj = new Tip(O.text, O.fl, O.callback);
            break;
    }

    //生成弹出框
    obj.create();
    return obj;
}

调用插件代码

//将js代码路径引进来
<script src="Alerts.js"></script>
  aler({
            type: '提示',
            text: '数据传入有误',
            fl: true,
            callback : function(){
                console.log(true);
            }
        })

相关文章

  • 封装弹出框插件

    封装思想 1、先封装一个Alerts类 用来生成弹出框的页面结构 1.1 属性:提示头提示信息字体颜色 1.2 方...

  • 2018-10-13Bootstrap09

    Bootstrap 弹出框(Popover)插件 弹出框(Popover)与工具提示(Tooltip)类似,提供了...

  • XXAlertView使用

    XXAlertView对UIAlertController封装,能够方便的实现警告框。 使用方法 弹出提示框 弹出...

  • 自定义一个在鼠标移入弹窗时不消失的基于Bootstrap的pop

    首先,Bootstrap 弹出框(Popover)插件的用法。 通过 data 属性:如需添加一个弹出框(popo...

  • bootstrap弹出框popover:

    增加一个弹出框,为按钮标签添加data-toggle=”popover”使用弹出框(popover)插件,必须使用...

  • BootStrap[第十六章:弹出框和警告框插件]

    一.弹出框 弹出框即点击一个元素弹出一个包含标题和内容的容器。 弹出框插件有很多属性来配置提示的显示,具体如下: ...

  • 基于原生JS封装的Modal对话框插件

    基于原生JS封装Modal对话框插件 原生JS封装Modal对话框插件,个人用来学习原理与思想,只有简单的基本框架...

  • js+css插件库

    1、swiper.js 轮播插件2、layer.js 弹出框插件3、an...

  • 封装简便弹出框

    每次写弹出框的时候,UIAlertView的代理写得特别麻烦,而且当代码越多,找起来就比较麻烦,我用block进行...

  • 自定义android弹出框

    概述 android上原生的弹出框和ios原生的弹出框视觉差距较大,为了与ios的视觉效果类似,所以封装了这个库。...

网友评论

      本文标题:封装弹出框插件

      本文链接:https://www.haomeiwen.com/subject/dvxmurtx.html