封装的alert,confirm弹出框
Posted 羊羊羊丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装的alert,confirm弹出框相关的知识,希望对你有一定的参考价值。
不爱废话,直接贴代码
CSS :(主要弹出框css)
/*弹出层*/ .modal{ position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 1050; display: none; overflow: hidden; -webkit-overflow-scrolling: touch; outline: 0; background-color: rgba(0, 0, 0, 0.5); } .modal .modal-dialog{ position: relative; width:auto; margin: 10px; } .modal .modal-content{ position: relative; background-color: #fff; -webkit-background-clip: padding-box; background-clip: padding-box; border: 1px solid #999; border: 1px solid rgba(0, 0, 0, .2); border-radius: 6px; outline: 0; -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, .5); box-shadow: 0 3px 9px rgba(0, 0, 0, .5); max-width: 350px; min-width: 300px; margin: 0 auto; display: block; } .modal .modal-header{ padding: 15px; border-bottom: 1px solid #e5e5e5; } .modal .modal-header h3{ margin: 0; text-align: center; } .modal .modalclose{ position: absolute; right: -10px; top: -12px; } .modal .modalclose i{ color:#FE9900; font-size: 40px; } .modal .modal-body{ position: relative; padding: 15px; } .modal .modal-footer { /*padding: 10px;*/ text-align: right; border-top: 1px solid #e5e5e5; text-align: center; } .modal .modal-footer button{ margin: 10px; }
js:
/* *弹出框调用 Modal.alert({msg:‘111‘}) Modal.confirm({msg:‘111‘},function(is){console.log(is)}) **/ var dialog_model = ‘<div id="dialogModal" class="modal"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h3 >[Title]</h3></div><div class="modal-body"><p>[Message]</p></div><div class="modal-footer"><button type="button" class="btn btn-org ok" >[BtnOk]</button><button type="button" class="btn btn-default cancel">[BtnCancel]</button></div></div></div></div>‘ document.write(dialog_model) var alr = $("#dialogModal"); var windHeight = $(window).height(); alr.children().css({ "top":(windHeight/2)-165, "right": "15px", })
var ahtml = alr.html(); var Modal = { alert:function(options){
alr.html(ahtml); this._dialog(options); alr.fadeIn() alr.find(‘.cancel‘).hide(); alr.find(‘.ok‘).off(‘click‘).on(‘click‘,function() { alr.fadeOut() }); }, confirm:function(options,callback) {
alr.html(ahtml); this._dialog(options); alr.fadeIn() alr.find(‘.cancel‘).show(); alr.find(‘.ok‘).off(‘click‘).on(‘click‘,function() { callback && callback(true); }); alr.find(‘.cancel‘).off(‘click‘).on(‘click‘,function() { alr.fadeOut(); }) }, _dialog:function(options){ var reg = new RegExp("\\[([^\\[\\]]*?)\\]", ‘igm‘); alr.html(ahtml); var ops = { msg: "数据出错", title: "操作提示", btnok: "确定", btncl: "取消" } $.extend(ops, options); var html = alr.html().replace(reg, function(node, key) { return { Title: ops.title, Message: ops.msg, BtnOk: ops.btnok, BtnCancel: ops.btncl }[key]; }); alr.html(html); } }
(原创)
以上是关于封装的alert,confirm弹出框的主要内容,如果未能解决你的问题,请参考以下文章