弹出框优化实例(alert和confirm)
在项目过程中会遇到需要使用自己定义的弹出框的情况。以前用过ymprompt,但是它太复杂而且不好自己操控。所以自己写了一个弹出框实例。
主要有两类弹出框alert和confirm。基于jQuery
alert([title,]des[,callback]):
title: 弹出框标题,string类型,可选
des: 弹出框内容描述,string类型,必须
callback: 弹出框点击按钮后的回调函数,可选
效果图如下

confirm([title,]des,callback):
title: 弹出框标题,string类型,可选
des: 弹出框内容描述,string类型,必须
callback: 弹出框点击按钮后的回调函数,必须
效果图如下

处理上都比较简单,需要注意的有以下几点:
1.对传入参数的多样处理
2.完成初始化以后需要按钮上焦点
3.响应回调之前需要把弹出框隐藏起来。
4.由于我们自定义的弹出框,confirm不能使用if(confirm('确认?') == true){expression}这种方式而是使用回调来处理。
完整源码alert.js如下(可以根据需要自己做拓展)。
var confirmCallbacks = '';
var alertCallbacks = '';
/*至少一个参数des*/
function alert(title,des,callback){
if(!title){
top.alert('请为alert函数输入正确的参数!');
return;
}else if(!des){
des = title;
title = '信息提示';
}else if(des && !callback){
if(typeof des == 'function'){
callback = des;
des = title;
title = '信息提示';
}
}
$('.xgalert .modal-header').addClass('warning')
$('#alert_title').html(title);
$('#alert_des').html(des);
$('.btn.btn-warning').html('确定').show();
$('.btn.btn-primary').hide();
alertCallbacks = callback;
$('.xgalert').addClass('active');
setTimeout(function(){$('.btn.btn-warning')[0].focus()},100);
} //注意此处confirm不能使用if(confirm('确认?') == true){expression}这种方式,
//只能把expression写到回调中
//至少两个参数des,callback
function confirm(title,des,callback){
if(!title || !des){
top.alert('请为confirm函数输入正确的参数!');
return; }else if(!callback){
callback = des;
des = title;
title = '信息提示';
}
$('.xgalert .modal-header').removeClass('warning')
$('.xgalert').addClass('active')
$('#alert_title').html(title);
$('#alert_des').html(des);
$('.btn.btn-warning').html('取消').show();
$('.btn.btn-primary').html('确定').show();
confirmCallbacks = callback;
$('.xgalert').addClass('active')
setTimeout(function(){$('.btn.btn-warning')[0].focus()},100);
} $(function(){
$(document).on('click','.xgalert .btn.btn-warning',function(){
//必须要先关闭弹出框,然后在执行回调(防止回调中有对弹出框的处理导致显示错误)
$('.xgalert').removeClass('active'); if(alertCallbacks){
alertCallbacks();
}
alertCallbacks = ''; }).on('click','.xgalert .close',function(){
$('.xgalert').removeClass('active');
}).on('click','.xgalert .btn.btn-primary',function(){
//必须要先关闭弹出框,然后在执行回调(防止回调中有对弹出框的处理导致显示错误)
$('.xgalert').removeClass('active'); if(confirmCallbacks){
confirmCallbacks();
}else{
alert('没有回调函数!')
}
confirmCallbacks = ''; });
}) window.alert = alert;
window.confirm = confirm;
html与css代码如下,后面有一个例子,不过需要注意一点是alert、confirm是函数,连续调用最终只会呈现最后一次调用的结果。
<!DOCTYPE html>
<html lang="ch-cn">
<head>
<meta charset="utf-8">
<script type="text/javascript" src='jquery-1.9.1.js'></script>
<script type="text/javascript" src='alert.js'></script>
<style type="text/css">
html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
margin: 0;
padding:0;
}
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
} body{
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.xgalert{
height: 100%;
width: 100%;
position: fixed;
top: 0px;
display: none;
z-index: 10000 !important;
}
.xgalert.active{
display: block;
}
.xgalert .modal-preview{
position: fixed;
height: 100%;
width: 100%;
background: #000;
background: rgba(0,0,0,0.5);
filter: alpha(opacity=50);
/*z-index: 100000;使用这种方式实现ie8半透明用了position: fixed/absolute/relative;就不能使用z-index*/
}
.xgalert .modal-preview .modal{
top: 50%;
position: relative;
margin-top: -114px;
margin-left: auto;
margin-right: auto;
width: 450px;
display: block;
overflow: hidden;
}
.modal-preview .modal .modal-dialog {
width: 90%;
margin: 30px auto;
position: relative;
}
.modal-content {
-webkit-border-radius: 0;
-webkit-background-clip: padding-box;
-moz-border-radius: 0;
-moz-background-clip: padding;
border-radius: 0;
background-clip: padding-box;
-webkit-box-shadow: 0 0 40px rgba(0,0,0,.5);
-moz-box-shadow: 0 0 40px rgba(0,0,0,.5);
box-shadow: 0 0 40px rgba(0,0,0,.5);
color: #000;
background-color: #fff;
border: rgba(0,0,0,0);
}
.modal-header {
min-height: 16.43px;
padding: 10px 15px 10px 20px;
background-color: #f5f5f5;
}
.modal-primary .modal-header {
border-bottom: 3px solid #4374e0;
}
.warning {
border-bottom: 3px solid #c2002d!important;
color: #f4b400!important;
}
.close {
float: right;
font-size: 26px;
font-weight: 700;
line-height: 1;
color: #000;
text-shadow: 0 1px 0 #fff;
filter: alpha(opacity=20);
opacity: .2;
font-family: inherit;
}
button.close {
-webkit-appearance: none;
padding: 0;
cursor: pointer;
background: 0 0;
border: 0;
}
.modal-title {
margin: 0;
line-height: 1.42857143;
font-family: inherit;
font-weight: 500;
}
.modal-body {
position: relative;
padding: 15px;
}
p {
line-height: 22px;
margin: 0 0 10px;
}
.modal-footer {
padding-top: 12px;
padding-bottom: 14px;
border-top: 0;
background-color: #f5f5f5;
padding: 15px;
text-align: right;
}
.btn {
cursor: pointer;
vertical-align: middle;
margin: 0;
position: relative;
display: inline-block;
color: #fff;
-webkit-box-shadow: 0 1px 0 rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 0 rgba(0,0,0,.05);
box-shadow: 0 1px 0 rgba(0,0,0,.05);
-webkit-transition: all .15s ease;
-moz-transition: all .15s ease;
-o-transition: all .15s ease;
transition: all .15s ease;
-webkit-border-radius: 2px;
-webkit-background-clip: padding-box;
-moz-border-radius: 2px;
-moz-background-clip: padding;
border-radius: 2px;
background-clip: padding-box;
font-size: 13px;
font-family: inherit;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border: 1px solid transparent;
-webkit-appearance: button;
color: #444;
padding: 6px 12px;
}
.btn-primary, .btn-primary:focus {
background-color: #427fed!important;
border-color: #427fed;
color: #fff;
}
.btn-warning, .btn-warning:focus {
background-color: #f4b400!important;
border-color: #f4b400;
color: #fff;
}
.modal-footer .btn+.btn {
margin-bottom: 0;
margin-left: 5px;
}
</style>
</head>
<body >
<div class='xgalert'>
<div class="modal-preview">
<div class="modal modal-primary">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 id='alert_title' class="modal-title"></h4>
</div>
<div class="modal-body">
<p id='alert_des'></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">确定</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">取消</button>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script type="text/javascript">
alert("alert");
//alert("自定义标题","alert");
//alert("自定义标题","alert",function(){alert("这个是alert回调结果")});
//alert("alert",function(){alert("这个是alert回调结果")});
//confirm("confirm没有回调会提示错误");
//confirm("confirm没有回调会提示错误",function(){alert("这个是comfirm回调结果")});
//confirm("自定义标题","confirm没有回调会提示错误",function(){alert("这个是comfirm回调结果")});
</script>
</html>
如果觉得本文不错,请点击右下方【推荐】!
弹出框优化实例(alert和confirm)的更多相关文章
- 弹出框JBox实例
前几天做的考试系统的一些后台弹出框的一些模板.主要是因为普通的弹出框样式不是很好,颜色也不能调换.这里我们用的是JBox,还是从师傅那得知的.自己小实验了下,这里就做个小结. JBox 插件说明 - ...
- 自动化测试基础篇--Selenium弹出框alert
摘自https://www.cnblogs.com/sanzangTst/p/7685304.html 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认 ...
- 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选
原生Js封装的弹出框-弹出窗口-页面居中-多状态可选 实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...
- bootstrap-js(六)弹出框
实例 为任意元素添加一小块浮层,用于存放非主要信息. 弹出框的标题和内容的长度都是零的话将永远不会被显示出来. 初始化 由于性能的原因,工具提示和弹出框的 data 编程接口(data api)是必须 ...
- 自动化测试-12.selenium的弹出框处理
前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...
- JavaScript中的三种弹出框的区别与使用
JavaScript中有三种原生的弹出框,分别是alert.confirm.prompt.分别表示弹出框.确认框.信息框. 以下是示例代码: <!DOCTYPE html> <htm ...
- Python+Selenium笔记(九):操作警告和弹出框
#之前发的 driver.switch_to_alert() 这句虽然可以运行通过,但是会弹出警告信息(这种写法3.x不建议使用) 改成 driver.switch_to.alert就不会了. (一 ...
- vue弹出框的封装
依旧是百度不到自己想要的,就自己动手丰衣足食 弹出框做成单独的组件confirm.vue; <template> <transition name="mask-bg-fad ...
- Bootstrap-Plugin:弹出框(Popover)插件
ylbtech-Bootstrap-Plugin:弹出框(Popover)插件 1.返回顶部 1. Bootstrap 弹出框(Popover)插件 弹出框(Popover)与工具提示(Tooltip ...
随机推荐
- iOS 删除已经配置的类库和移除CocoaPods
引言 我们使用CocoaPods非常高效地将一些第三方类库导入到我们的项目中,但是不由得产生一个疑问:如果发现某个类库不适用,甚至是整个CocoaPods我们都不想再在项目中持有,那么我们要怎么把这些 ...
- mongoDB研究笔记:分片集群的工作机制
上面的(http://www.cnblogs.com/guoyuanwei/p/3565088.html)介绍了部署了一个默认的分片集群,对mongoDB的分片集群有了大概的认识,到目前为止我们还没有 ...
- Nim编码风格
介绍 Nim语言不限制开发人员使用哪种具体的编码风格, 但为了社区的发展,在编写一些标准库的时候还是应该遵从统一的编码风格 这篇文章会列出一系列的编码风格准则,供大家参考. 但值得注意的是,有很多例外 ...
- C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import)
上一篇学习完了MEF的基础知识,编写了一个简单的DEMO,接下来接着上篇的内容继续学习,如果没有看过上一篇的内容, 请阅读:http://www.cnblogs.com/yunfeifei/p/392 ...
- 走进AngularJs(四)自定义指令----(中)
上一篇简单介绍了自定义一个指令的几个简单参数,restrict.template.templateUrl.replace.transclude,这几个理解起来相对容易很多,因为它们只涉及到了表现,而没 ...
- 浅谈Excel开发:三 Excel 对象模型
前一篇文章介绍了Excel中的菜单系统,在创建完菜单和工具栏之后,就要着手进行功能的开发了.不论您采用何种方式来开发Excel应用程序,了解Excel对象模型尤其重要,这些对象是您与Excel进行交互 ...
- No resource found that matches the given name 'android:Widget.Material.A解决方案
1:首先新建空白工作区 2:先import appcompat_v7 appcompat_v7在一个类似这样的地方, C:\mywork\android\android-sdk-windows\ext ...
- 对Google cloud platform 做了点研究
Google也推出了云计算基础服务, 加上微软Azure,亚马逊AWS, 都齐活了. 下面是研究了一下对其的一个初步了解. 计算: Compute Engine IaaS平台,提供VM,操作灵 ...
- Docker镜像
docker镜像123? 额,由于没有实验环境,没有亲手实践,因此理解可能有不对的地方. 反正也是学习笔记,以后再修改吧... docker的镜像跟virtualbox的镜像不一样.在虚拟机中,镜像是 ...
- 每天一个linux命令(43):killall命令
Linux系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进程,如果要找到我们需要杀死的进程,我们还需要在 ...