自定义H5弹出框

自定义的组件代码如下:

/*
 *title 弹出框的标题
 *titleColor 弹出框标题颜色
 *message 提示语
 *messageColor 提示语颜色
 *backgroundColor 背景框颜色
 *callBack 回调方法
*/

class UIConfirmAlert {
    constructor(opts = {}) {
        this._defaultOpts = {
            zoom: 1,
            title: '提示',
            titleColor: '#fff',
            message: '提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语提示语',
            messageColor: '#fff',
            backgroundColor:"#494b5a",
            callBack:null
        };
        //其他参数
        this._opts = $.extend(this._defaultOpts, opts);
        //绘制组件
        this._draw();
    }

    /**
     * 绘制组件
     */
    _draw() {
        let testStr = `<div class="alert_mark" style="width:100%;
                                                    height:100%;
                                                    position:fixed;
                                                    z-index:1000;
                                                    top: 0;
                                                    bottom: 0;
                                                    left: 0;
                                                    right: 0;
                                                    justify-content: center;
                                                    display: flex;
                                                    align-items: center;
                                                    background-color:rgba(0,0,0,0.2);
                                                    zoom: ${this._opts.zoom};">
                            <div style="width:400px;background-color:${this._opts.backgroundColor};border-radius: 10px;">
                                <div class="alert_title" style="text-align: center;margin: 10px;color:${this._opts.titleColor}">${this._opts.title}</div>
                                <div style="height:1px;width:100%;border-bottom:1px solid gray;"></div>
                                <div style="justify-content: center;display: flex;align-items: center;">
                                    <div style="margin: 20px 10px 20px 10px;color:${this._opts.messageColor}">${this._opts.message}</div>
                                </div>
                                <div style="height:1px;width:100%;border-bottom:1px solid gray;"></div>
                                <div class="dialog-footer" style="justify-content: center;display: flex;align-items: center;margin: 10px;">
                                    <button type="button" class="dialog-btn-sure" style="background-color: #5a7bff;color:#fff;width:72px;height:32px;font-size:15px;padding: 0;border-radius: 21px;border-width: 0;">确认</button>
                                    <div style="width:30px;"></div>
                                    <button type="button" class="dialog-btn-cancel" style="background-color: #5a7bff;color:#fff;width:72px;height:32px;font-size:15px;padding: 0;border-radius: 21px;border-width: 0;">取消</button>
                                </div>
                            </div>
                    </div>`;

        var body = document.getElementsByTagName("body");
        $(body).append(testStr);
        this._bindEvent();
    }

    _bindEvent() {
        var self = this;
        $(".dialog-btn-sure").click(function () {
            self.cleanup()
        });
        $('.dialog-btn-cancel').click(function () {
            $(".alert_mark").remove()
        });
    }

    cleanup() {
        if (this._opts.callBack){
            this._opts.callBack();
        }
        $(".alert_mark").remove()
    }

}

下面是组件的初始化,此自定义的组件是直接添加在body上面的,直接初始化就可以使用

<!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>components test</title>
    <script src="./libs/jquery.min.js"></script>
    <script src="UIConfirmAlert.js"></script>
</head>

<body style="width: 8640px; height: 7680px;">
</body>
<script>
    new UIConfirmAlert({
        message: "确定要关闭窗口嘛?", callBack: function () {
            console.log("确定");
        }
    });
</script>

</html>

效果如下图所示
1588063840058.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 3,226评论 2 9
  • 官方文档 初始化 Initialization是为准备使用类,结构体或者枚举实例的一个过程。这个过程涉及了在实例里...
    hrscy阅读 1,216评论 0 1
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,511评论 1 38
  • 导语 当系统控件不能满足我们的需求的时候,这时候我们就需要自定义控件,根据我们的需求来定制一个能满足我们需求的控件...
    一个有故事的程序员阅读 6,794评论 2 14
  • 经常被夸独立, 其实没什么, 只不过我明白, 有些事自己不做, 也没有人会来帮你!
    大哼阅读 233评论 0 0

友情链接更多精彩内容