前端原生自定义confirm

前端原生自定义confirm

样式图片
在这里插入图片描述大致样式是这样的喜欢的可以讨论一下
html代码

<style>
        /*自制confirm样式*/
            .backgCover{
                position: fixed;
                top: 0;
                bottom: 0;
                left: 0;
                right: 0;
                text-align: center;
                /* background: #ccc; */
                z-index: 2030;
            }
            .cover {
                display: inline-block;
                width: 420px;
                padding-bottom: 10px;
                vertical-align: middle;
                background-color: #fff;
                border-radius: 4px;
                border: 1px solid #ebeef5;
                font-size: 18px;
                box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
                text-align: left;
                overflow: hidden;
                backface-visibility: hidden;
                margin-top: 15%;
            }
            .confirmbox {
                width: 100%;
                height: 100%;
            }
            .confirmbox .line1{
                font-size: 14px;
                border-bottom: 1px solid #ccc;
                font-weight: bold;
                height: 30px;
                line-height: 30px;
                padding-left: 10px;
                /* background-color: aliceblue; */
            }
            .contText{
                display: flex;
                align-items: center;
                height: 95px;
                font-size: 14px;
            }
            .button-out-box {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: 5px
        }
        .button-out-box > button{
             
            width: 50px;
            height: 30px;
            margin-right: 10px !important;
            border-radius: 5px;
            cursor: pointer;
            color: #fff;
            border: 1px solid #ccc;
            background-color: #3366cc;
            margin-top:5px;

        }
        .backgCoverModel{
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            opacity: .5;
            background: #000;
            z-index: 2029;
        }
    </style>

js部分

  

        var myConfirm = function(title,content, type, resCallback) {
        var divCont = '<div class="backgCover">' + '<div class="cover"><div class="confirmbox"><div class="line1">' + title + 
            '<img id="delCancle" src="./del.png" style="cursor: pointer;height:16px;width:16px;float:right;margin: 7px 10px 0px 0px;">' +
            '</div>'+ '<div class="contText">'+ '<img src="./message.png" style="height:32px;width:32px;margin:0px 10px;">'+ content + 
            '</div>'+'<div class="line2 button-out-box">';
        if(type == 1){ 
            divCont += '<button class="cancle" id="cancle" style="background-color:#fff;color:#000">' + '取消' + '</button><button class="sure" id="sure">' + '确定' + '</button>';
        }else if(type == 2) {
            divCont += '<button class="cancle" id="cancle">' + '确定' + '</button>';
        }
            divCont += '</div></div></div> </div>' + '<div class="backgCoverModel"></div>';
            $('body').append(divCont)
            $('.backgCover').show();
            $('.backgCoverModel').show();
            $('#cancle').click(function(){
                $('.backgCover').remove();
                $('.backgCoverModel').remove();
                resCallback({
                    'status': false
                });
            });
            $('#sure').click(function(){
                $('.backgCover').remove();
                $('.backgCoverModel').remove();
                resCallback({
                    'status': true
                });
            });
            $('#delCancle').click(function(){
                $('.backgCover').remove();
                $('.backgCoverModel').remove();
                resCallback({
                    'status': false
                });
            });
        }
        // //传1两个按钮 2 一个按钮
        // myConfirm('配件添加成功提示','是否确定同步spu?','1',function(res){
        //         console.log(res);
        //         if(res.status){
        //         //用户点击确定 
        //         console.log(1111)						
        //         }else {
        //         //用户点击取消
        //         console.log(222)
        //         }
        //     });
        function open1(){
            myConfirm('配件添加成功提示','是否确定同步spu?','2',function(res){
                console.log(res);
                if(res.status){
                //用户点击确定 
                console.log(1111)						
                }else {
                //用户点击取消
                console.log(222)
                }
            });
        }
        function open2(){
            myConfirm('配件添加成功提示','是否确定同步spu?','1',function(res){
                console.log(res);
                if(res.status){
                //用户点击确定 
                console.log(444)						
                }else {
                //用户点击取消
                console.log(555)
                }
            });
        }

把open1 和open2 两个事件随便绑定上两个按钮上就可以啦
事件绑定

<button οnclick="open1()">点击打开样式一</button>
        <button οnclick="open2()">点击打开样式二</button>

在这里插入图片描述样式一
在这里插入图片描述样式二

后续事件写在myConfirm里面就可以啦
记得引入一下jQuery

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值