移動端彈出層,帶確定、取消、其他、回撥函式
$(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
}
});
}
});