自定义 confirm 确认框

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义 confirm 确认框相关的知识,希望对你有一定的参考价值。

ys_confirmation.css

.ys-confirmation{
    position:fixed;
    left:0;
    right:0;
    top:0;
    bottom:0;
    display:none;
    background-color:rgba(0,0,0,0.4);
    z-index: 99999;
}

.ys-confirmation .ys-confirmation-content{
    position:absolute;
    left:30px;
    right:30px;
    top:50%;
    display:block;
    background-color:#fff;
    margin:auto;
    border-radius: 8px;
    padding-bottom:51px;
    box-sizing: border-box;
}
.ys-confirmation .ys-confirmation-content .ys-confirmation-message{
    color:#222;
    line-height: 20px;
    font-size:14px;
    text-align: center;
    padding:25px 15px;
}
.ys-confirmation .ys-confirmation-content .ys-confirmation-btn-group{
    position:absolute;
    left:0;
    right:0;
    bottom:0;
    display:block;
    width:100%;
    height:51px;
    border-top:1px solid #e5e5e5;
}

.ys-confirmation .ys-confirmation-content .ys-confirmation-btn-group a{
    position:absolute;
    top:0;
    bottom:0;
    display:block;
    width:50%;
    height:50px;
    line-height: 50px;
    text-align: center;
    font-size:17px;
    color:#ff7920;
}

.ys-confirmation .ys-confirmation-content .ys-confirmation-btn-group a.ys-confirmation-cancel-btn{
    left:0;
}

.ys-confirmation .ys-confirmation-content .ys-confirmation-btn-group a.ys-confirmation-ok-btn{
    right:0;
    border-left:1px solid #e5e5e5;
    font-weight: bold;
}

ys_confirmation.js

(function($){

    var container = null;

    var currentCallback = null;

    var html =  "<div class=‘ys-confirmation‘>                              "+
                "  <div class=‘ys-confirmation-content‘>                   "+
                "     <div class=‘ys-confirmation-message‘></div>"+
                "     <div class=‘ys-confirmation-btn-group‘>             "+
                "        <a class=‘ys-confirmation-cancel-btn‘>取消</a>  "+
                "        <a class=‘ys-confirmation-ok-btn‘>确定</a>      "+
                "     </div>                                              "+
                "  </div>                                                  "+
                "</div>                                           ";

    function render(){
        container = $(html).appendTo("body");
    }

    function show(message,callback){

        currentCallback = callback;

        $(container).find(".ys-confirmation-message").html(message);
        $(container).css("visibility","hidden");
        $(container).show();
        var popupContentHeight = parseInt($(container).find(".ys-confirmation-content").css("height"));
        $(container).find(".ys-confirmation-content").css({
            "margin-top":(-1)*popupContentHeight/2+"px"
        });
        $(container).css("visibility","initial");
    }

    function hide(){
        $(container).hide();

        currentCallback = null;
    }

    function bindEvents(){
        container.on("touchend",".ys-confirmation-cancel-btn",function(e){
            e.stopPropagation();
            e.preventDefault();
            hide();
        });

        container.on("touchend",".ys-confirmation-ok-btn",function(e){
            e.stopPropagation();
            e.preventDefault();
            currentCallback();
            hide();
        });
    }

    var initialized = false;

    function init(){
        if(initialized){
            return;
        }
        initialized = true;
        render();
        bindEvents();

    }


    function showConfirmation(message,callback){
        init();

        show(message,callback);


    }

    var ConfirmationWidget = {};

    ConfirmationWidget.showConfirmation=showConfirmation;

    window.ConfirmationWidget = ConfirmationWidget;
})(jQuery);


component.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="static/css/common/ys_confirmation.css">
    <script src="static/js/vendor/jquery-2.1.3.min.js"></script>
    <script src="static/js/common/ys_confirmation.js"></script>
</head>
<body>
    <input id="confirm-btn" type="button" value="确认"/>

<script>
    $("#confirm-btn").click(function(e){
        e.stopPropagation();
        e.preventDefault();

        ConfirmationWidget.showConfirmation("确认提交?",function(){
            alert("提交");
        });
    });
</script>
</body>
</html>


技术分享

以上是关于自定义 confirm 确认框的主要内容,如果未能解决你的问题,请参考以下文章

JQUERY 自定义confirm,怎么知道用户点击的是确认按钮,还是取消按钮

vue的确认弹框加格式

如何在 Yii2 Gridview 中自定义默认数据确认对话框

用js的confirm弹出一个框 点击确定后如何执行一个php函数?

微信小程序之----弹框组件modal

Layui的layer.confirm弹框用法,很详细