移动端弹出层,带确定、取消、其他、回调函数

$(document).ready(function(){
    $.MsgBox = {
        /*Alert没有回调
        *title:标题
        *msg:提示信息
        */
        Alert: function (title, msg) {
            GenerateHtml("alert", title, msg);
            btnOk();  //alert只是弹出消息,因此没必要用到回调函数callback
            btnNo();
        },
        /*Confirm有回调
        *title:标题
        *msg:提示信息
        *callbackOk:确定的回调函数
        *callbackNo:取消的回调函数
        */
        Confirm: function (title, msg, callbackOk,callbackNo) {
            GenerateHtml("confirm", title, msg);
            btnOk(callbackOk);
            btnNo(callbackNo);
        }
    }
    //生成Html
    function GenerateHtml(type, title, msg) {
        var _html = "";
        _html += '<div id="mb_box"></div><div id="mb_con"><span id="mb_tit">' + title + '</span>';
        _html += '<div id="mb_msg">' + msg + '</div><div id="mb_btnbox">';
        if (type == "alert") {
            _html += '<input id="mb_btn_ok" type="button" value="确定" />';
        }
        if (type == "confirm") {
            _html += '<input id="mb_btn_no" type="button" value="取消" />';
            _html += '<input id="mb_btn_ok" type="button" value="确定" />';
        }
        _html += '</div></div>';
        //必须先将_html添加到body,再设置Css样式
        $("body").append(_html); 
        //生成Css
         GenerateCss();
    }
    //生成Css
    function GenerateCss() {
        $("#mb_box").css({ width: '100%', height: '100%', zIndex: '99999', position: 'fixed',
            filter: 'Alpha(opacity=60)', backgroundColor: 'black', top: '0', left: '0', opacity: '0.6'
        });
        $("#mb_con").css({ zIndex: '999999', width: '80%', position: 'fixed',
            backgroundColor: 'White', borderRadius: '4px'
        });
        $("#mb_tit").css({ display: 'block', fontSize: '14px', color: '#444', padding: '10px 15px',
             borderRadius: '15px 15px 0 0',fontWeight: 'bold'
        });
        $("#mb_msg").css({ padding: '20px', lineHeight: '20px', fontSize: '14px'
        });
        $("#mb_btnbox").css({ margin: '6px 0 10px 0'});
        $("#mb_btn_ok,#mb_btn_no").css({ width: '85px', height: '30px', color: 'white', border: 'none' });
        $("#mb_btn_ok").css({color:'black',float:'right',marginRight:'20px',paddingBottom:'10px'});
        $("#mb_btn_no").css({color:'black',float:'left',marginLeft:'20px',paddingBottom:'10px'});
        
        var _widht = document.documentElement.clientWidth;   //屏幕宽
        var _height = document.documentElement.clientHeight; //屏幕高
        var boxWidth = $("#mb_con").width();
        var boxHeight = $("#mb_con").height();
        //让提示框居中
        $("#mb_con").css({ top: (_height - boxHeight) / 2 + "px", left: '10%'});
    }
    //确定按钮
    function btnOk(callbackOk) {
        $("#mb_btn_ok").on("click",function () {
            $("#mb_box,#mb_con").remove();
            if (typeof (callbackOk) == 'function') {
                callbackOk();
            }
        });
    }
    //取消按钮事件
    function btnNo(callbackNo) {
        $("#mb_btn_no").on("click",function () {
            $("#mb_box,#mb_con").remove();
            if(typeof (callbackNo) == 'function'){
                callbackNo();
            }else{
                return
            }
        });
    }
});
 

猜你喜欢

转载自blog.csdn.net/qq_38657283/article/details/85334299