自己写的基于bootstrap风格的弹框插件
自己写的一款基于bootstrap风格的弹框插件,暂时只有确认框、提示框。后续功能扩展、bug修改再更新。
;(function($){
//默认参数
var PARAMS;
var DEFAULTPARAMS =
{
width: 500,
title: '提示消息',
content: '',
okbtn: '确定',
cancelbtn: '取消',
headerBackground: 'info',
vbackdrop: 'static', //默认点击遮罩不会关闭modal
vkeyboard: true, //按esc关闭modal
confirmFn: new Object,
cancelFn: new Object
};
$.dialog = {
confirm: function(params){
$.dialog.initParmas(params);
$.dialog.Show('confirm', function(e){
if($.isFunction(PARAMS.confirmFn)){
PARAMS.confirmFn(e);
}
},
function(f){
if($.isFunction(PARAMS.cancelFn)){
PARAMS.cancelFn(f);
}
});
},
alert: function(params){
$.dialog.initParmas(params);
$.dialog.Show('alert', function(e){
if($.isFunction(PARAMS.confirmFn)){
PARAMS.confirmFn(e);
}
}, null);
},
Show: function(type, confirmCaller, cancelCaller){
var html = '<div class="modal fade" id="tipModal">'
+ '<div class="modal-dialog" style="width:'+PARAMS.width+'px"><div class="modal-content">'
+ '<div class="modal-header header_'+PARAMS.headerBackground+'">'
+ '<a class="close" data-dismiss="modal">×</a>'
+ '<h4 class="modal-title text-center">'+PARAMS.title+'</h4></div>'
+ '<div class="modal-body text-center body_content">'+PARAMS.content+'</div>'
+ '<div class="modal-footer">';
if(type=='confirm'){
html += '<button class="btn btn-default" id="btnCancel">'+PARAMS.cancelbtn+'</button>';
}
html += '<button class="btn btn-primary" id="btnOk">'+PARAMS.okbtn+'</button>';
html += '</div></div></div></div>';
$('body').append(html);
$('#tipModal').modal({backdrop:PARAMS.vbackdrop,keyboard:PARAMS.vkeyboard});
$.dialog.setDialogEvent(type, confirmCaller, cancelCaller);
},
initParmas: function(params){
if(params!= undefined && params!= null){
PARAMS = $.extend({}, DEFAULTPARAMS, params);
}
},
setDialogEvent: function(type, confirmCaller, cancelCaller){
switch(type){
case 'confirm':
$("#btnOk").click(function(){
$('#tipModal').modal('hide');
$('#tipModal').on('hidden.bs.modal', function(){
$('#tipModal').remove(); //要先remove modal
if($.isFunction(confirmCaller)){
confirmCaller(true);
}
});
});
$("#btnCancel").click(function(){
$('#tipModal').modal('hide');
$('#tipModal').on('hidden.bs.modal', function(){
$('#tipModal').remove();
if($.isFunction(cancelCaller)){
cancelCaller(false);
}
});
});
break;
case 'alert':
$("#btnOk").click(function(){
$('#tipModal').modal('hide');
$('#tipModal').on('hidden.bs.modal', function(){
$('#tipModal').remove();
if($.isFunction(confirmCaller)){
confirmCaller(true);
}
});
});
break;
};
$('#tipModal').on('hidden.bs.modal', function(){
$('#tipModal').remove();
});
$("#tipModal .close").click(function(){
$('#tipModal').on('hidden.bs.modal', function(){
$('#tipModal').remove();
});
});
//设置窗口可拖动
$('#tipModal .modal-header').Draggable($('#tipModal .modal-dialog'));
}
};
dialogConfirm = function(params){
$.dialog.confirm(params);
};
dialogAlert = function(params){
$.dialog.alert(params);
};
})(jQuery);
//拖动层
;(function($){
$.fn.extend({
Draggable: function(objMoved){
return this.each(function(){
//鼠标按下时的位置
var mouseDownPosiX, mouseDownPosiY;
//obj的初始位置
var objPosiX, objPosiY;
//鼠标移动的距离
var tempX, tempY;
//移动的对象
var obj = $(objMoved)==undefined ? $(this): $(objMoved);
//是否处于移动状态
var status = false;
$(this).mousedown(function(e){
status = true;
mouseDownPosiX = e.pageX;
mouseDownPosiY = e.pageY;
objPosiX = obj.css("left").replace("px", "");
objPosiY = obj.css("top").replace("px", "");
}).mouseup(function(){
status = false;
});
$(document).mousemove(function(e){
if(status){
tempX = parseInt(e.pageX) - parseInt(mouseDownPosiX) + parseInt(objPosiX);
tempY = parseInt(e.pageY) - parseInt(mouseDownPosiY) + parseInt(objPosiY);
obj.css({ "left": tempX + "px", "top": tempY + "px" });
}
//判断是否超出窗体
//计算出弹出层距离右边的位置
var dialogRight = parseInt($(window).width())-(parseInt(obj.css("left"))+parseInt(obj.width()));
var dialogBottom = parseInt($(window).height())-(parseInt(obj.css("top"))+parseInt(obj.height()));
var maxLeft = $(window).width()-obj.width();
var maxTop = $(window).height()-obj.height();
if(parseInt(obj.css("left"))<=0){
obj.css("left","0px");
}
if(parseInt(obj.css("top"))<=0){
obj.css("top","0px");
}
if(dialogRight<=0){
obj.css("left",maxLeft+'px');
}
if(dialogBottom<=0){
obj.css("top", maxTop+'px');
}
}).mouseup(function(){
status = false;
}).mouseleave(function(){
status = false;
});
});
}
});
})(jQuery)
html页面中调用:
<body>
<div class="box">
<button class="btn btn-default" id="btn_confirm">确认框</button>
<button class="btn btn-default" id="btn_cancel">提示框</button>
</div>
</body>
<script src="jquery/jquery.min.js"></script>
<script src="bootstrap/bootstrap.min.js"></script>
<script src="js/dialog.js"></script>
<script type="text/javascript">
$(function(){
$("#btn_confirm").click(function(){
dialogConfirm({
width: 500,
content: '确定要删除吗?',
headerBackground: 'info',
vbackdrop: true, //默认点击遮罩不会关闭modal
vkeyboard: true, //按esc关闭modal
confirmFn: function(e){
dialogAlert({
width: 300,
content: 'true',
headerBackground: 'success',
vbackdrop: 'static', //默认点击遮罩不会关闭modal
vkeyboard: true //按esc关闭modal
});
},
cancelFn: function(f){
alert(f);
}
})
}); $('#btn_cancel').click(function(){
dialogAlert({
width: 300,
content: '删除成功!',
headerBackground: 'error',
vbackdrop: 'static', //默认点击遮罩不会关闭modal
vkeyboard: true, //按esc关闭modal
});
});
});
</script>
感觉写的不是很好,后面修改了或者扩展了功能再更新。源码会上传到文件。
文件名是:dialogByCy.rar
自己写的基于bootstrap风格的弹框插件的更多相关文章
- 基于Bootstrap的下拉框插件bootstrap-select
写在前面: 在这次的项目中,没有再使用liger-ui做为前端框架了,改为了Bootstrap,这次也好接触下新的技术,在学习的过程中发现,Bootstrap的一些组件基本都是采用class的形式,就 ...
- vue项目中使用vue-layer弹框插件
vue-layer弹框插件 安装 npm i --save vue-layer 引用 import layer from 'vue-layer' Vue.prototype.$layer = lay ...
- 一不小心写了个bootstrap风格下拉控件 JqueryUI + bootstrap
受够了EasyUI的封闭,Bootstrap虽然华丽但是功能太渣,闲着无聊写个下拉控件玩玩吧,不喜勿喷哈... 第一步:先设计下我的下拉控件的样子 1.既然是bootstrap风格的,我想应该是这样的 ...
- 基于bootstrap的富文本框——wangEditor【欢迎增加开发】
先来一张效果图: 01. 引言 老早就開始研究富文本框的东西,在写完<深入理解javascript原型与闭包>之后,就想着要去做一个富文本框的插件的样例. 如今网络上开源的富文本框插件许多 ...
- ios UIWebView自定义Alert风格的弹框
之前开发过一个App,因为公司之前写好了网页版的内容和安卓版本的App,我进去后老板要求我ios直接用网页的内容,而不需要自己再搭建框架.我一听,偷笑了,这不就是一个UIWebView吗?简单! 但是 ...
- Bootstrap风格zTree树形菜单插件
这是一款bootstrap风格jQuery zTree树形菜单插件,支持自定义编辑.添加列表菜单.删除列表等功能的jQuery树形菜单代码.在线演示 具体代码实现: <!DOCTYPE html ...
- Bootstrap+Angularjs自制弹框
指令 directive('bsPopup', function ($parse) { return { require: 'ngModel', restrict: 'A', link: functi ...
- bootstrap 弹窗或者提示框插件 bootstrap-growl 和bootstrap-notify
Bootstrap简单好用的页面右上角咆哮提示框 - daidaineteasy的专栏 - CSDN博客https://blog.csdn.net/daidaineteasy/article/deta ...
- SweetAler弹框插件与分页器插件
目录 SweetAlert插件 自定义分页器 使用Django向数据库批量插入数据 自定义分页器的思路 自定义分页器组件 SweetAlert插件 sweetalert是一款基于Bootstrap的专 ...
随机推荐
- 多表关联 update
UPDATE t_invests INNER JOIN t_user_coupons ON t_invests.user_coupon_id = t_user_coupons.id SET t_inv ...
- Kafka Topic ISR不全,个别Spark task处理时间长
现象 Spark streaming读kafka数据做业务处理时,同一个stage的task,有个别task的运行时间比多数task时间都长,造成业务延迟增大. 查看业务对应的topic发现当topi ...
- PHP生成唯一会员卡号
我们将0-Z(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ)分别代表数值0-35,如字母Z代表35.这样的话我要得到一个5位的编号,最大信息量就是36的5次方了,36^5 ...
- debain 8安装为知笔记(how to install wiznote in debain 8)
刚装了debain8后想安装为知笔记,百度之后发现为知笔记原来是开源软件.代码托管在github上:https://github.com/WizTeam/WizQTClient 但是上面只有Ubunt ...
- 用sql语句建表
CREATE TABLE USER (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(30) NOT NULL, p ...
- handler和Timer的用法
final Handler handler = new Handler(){public void handleMessage(Message msg){if (msg.what == 0x123){ ...
- jQuery extend方法使用及实现
一.jQuery extend方法介绍 jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不同对象上方法,但在jQuery内部代码实现的是相同的,只是功能却不太一样 ...
- Express4.x常用API(二):Request
这是第二篇了,打算每天都拿些时间去详细的看看文档来自学下express吧,接下来就开始了,这次依然是有选择性的找API根据自己的理解翻译过来,方便日后开发时候的自己查阅,由于水平有限,大概也会在使用过 ...
- 基于OpenCV 的iOS开发
1.创建项目 2.https://sourceforge.net/projects/opencvlibrary/files/opencv-ios/2.4.13/opencv2.framework.zi ...
- java验证码前台技术
//下面是在前台jsp页面不用导工具的情况下制作的验证码的基本代码 $(function(){ //创建验证码 createCode(); jQuery.validator.addMethod( &q ...