var toast = new Object;
toast.init = function() {
$('body')
.append(
'<div id="alert_div_container" style="left:40%;top:1%;max-width:50%;position:absolute;z-index:99999; opacity:1;display:none"></div>');
this.isInit=true;
}
toast.alertDivNum = 0;
toast.isInit=false;
toast.clearAlertDiv = function(divId) {
this.alertDivNum--;
$("#" + divId).remove();
if (this.alertDivNum <= 0) {
$("#alert_div_container").hide();
}
};
toast.intervalShow = function(divId) {
this.alertDivNum++;
setTimeout(function() {
$("#" + divId).fadeOut(300);
toast.clearAlertDiv(divId);
}, 2500);
}
toast.generateId = function() {
var date = new Date();
return 'alert_div' + date.valueOf();
};
toast.success = function(msg) {
if(!this.isInit){
this.init();
}
var html = '';
var divId = toast.generateId();
if (msg) {
html = '<div id="'
+ divId
+ '" class="alert alert-success text-center">'
+ msg + '</div>';
} else {
html = '<div id="'
+ divId
+ '" class="alert alert-success text-center"><strong>操作成功!</strong></div>';
}
$("#alert_div_container").append(html);
$("#alert_div_container").show();
this.intervalShow(divId);
};
toast.error = function(msg) {
if(!this.isInit){
this.init();
}
var html = '';
var divId = toast.generateId();
html = '<div id="' + divId
+ '" class="alert alert-danger text-center"><strong></strong>'
+ msg + '</div>';
$("#alert_div_container").append(html);
$("#alert_div_container").show();
this.intervalShow(divId);
};
(function($) {
toast.confirm = function() {
var html = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">'
+ '<div class="modal-dialog modal-sm">'
+ '<div class="modal-content">'
+ '<div class="modal-header">'
+ '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>'
+ '<h4 class="modal-title" id="modalLabel">[Title]</h4>'
+ '</div>'
+ '<div class="modal-body">'
+ '<p>[Message]</p>'
+ '</div>'
+ '<div class="modal-footer">'
+ '<button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>'
+ '<button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>'
+ '</div>' + '</div>' + '</div>' + '</div>';
var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');
var generateId = function() {
var date = new Date();
return 'mdl' + date.valueOf();
}
var init = function(options) {
options = $.extend({}, {
title : "操作确认框",
message : "提示内容",
btnok : "确定",
btncl : "取消",
width : 200,
auto : false
}, options || {});
var modalId = generateId();
var content = html.replace(reg, function(node, key) {
return {
Id : modalId,
Title : options.title,
Message : options.message,
BtnOk : options.btnok,
BtnCancel : options.btncl
}[key];
});
$('body').append(content);
$('#' + modalId).modal({
width : options.width,
backdrop : 'static'
});
$('#' + modalId).on('hide.bs.modal', function(e) {
$('body').find('#' + modalId).remove();
});
return modalId;
}
return {
show : function(options) {
var id = init(options);
var modal = $('#' + id);
modal.find('.ok').removeClass('btn-primary').addClass(
'btn-success');
modal.find('.cancel').show();
return {
id : id,
bind : function(callback,callback2) {
if (callback && callback instanceof Function) {
modal.find('.ok').click(function() {
callback();
});
}
if (callback2 && callback2 instanceof Function) {
modal.find('.cancel').click(function() {
callback2();
});
}
}
};
}
}
}();
})(jQuery);
文件下载地址:toast.js
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于