JS模拟Alert与Confirm对话框
这2个例子都是用原生JS写的,主要是用JS拼接了界面,并未做过多的事件监听。,样式用了Css3的一些特性。
调用方式则为:
//Alert
Alert.show('我警告你哦~'); //Confirm
Confirm.show('我是确认对话框',function(){
doSomething();
});
组件详情看下面的具体代码:
1.CSS样式
由于这2个组件的样式差不多,所用共用了一样的css,样式代码如下:
/**
* dialog
*/
.dialog {
top:40%;
left:40%;
width: 250px;
min-height: 100px;
position:fixed;
z-index:;
text-align: center;
padding:10px;
border:solid #bcc5c1 1px;
background:#FFF;
border-radius:3px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
padding:0px;
behavior: url(PIE.htc);
} .dialog .dialog-header {
position:relative;
width:100%;
height:30px;
margin:0px;
background:#0CF;
background:linear-gradient(top,#d1d4d3 0%,#c4c7c8 100%);
background:-webkit-linear-gradient(top,#d1d4d3 0%,#c4c7c8 100%);
background:-moz-linear-gradient(top,#d1d4d3 0%,#c4c7c8 100%);
border-radius:3px 3px 0px 0px;
-moz-border-radius:3px 3px 0px 0px;
-webkit-border-radius:3px 3px 0px 0px;
behavior: url(PIE.htc);
} .dialog-header .header-left {
width: auto;
height:auto;
float:left;
margin:7px;
} .dialog-header .header-right {
width: auto;
height:auto;
float:right;
margin:7px;
} .dialog .dialog-content {
font-size:14px;
height:100%;
width:96%;
float:left;
font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
color:#424242;
padding:5px;
} .dialog-content .content-main {
width: 100%;
min-height: 40px;
line-height:25px;
float:left;
white-space: normal;
} .dialog-content .content-btn {
float:left;
width:100%;
height:28px;
margin-top:10px;
} .btn {
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
text-align:center;
vertical-align:middle;
margin-right:5px;
padding:5px 20px 5px 20px;
text-decoration:none;
border:#c4c7c8 solid 1px;
border-radius:3px;
background:#d1d4d3;
background:linear-gradient(top,#d1d4d3 0%,#c4c7c8 100%);
background:-webkit-linear-gradient(top,#d1d4d3 0%,#c4c7c8 100%);
background:-moz-linear-gradient(top,#d1d4d3 0%,#c4c7c8 100%);
color:#111c24; } .btn:hover {
background:#d1d4d3;
background:linear-gradient(top,#c4c7c8 0%,#d1d4d3 100%);
background:-webkit-linear-gradient(top,#c4c7c8 0%,#d1d4d3 100%);
background:-moz-linear-gradient(top,#c4c7c8 0%,#d1d4d3 100%);
color:#111c24;
} .alert-content {
text-align: left;
text-indent: 20px;
} .alert {
margin-left:140px;
}
2.Alert
Alert 主要是模拟了界面,并显示提示信息。JS代码.Demo 可查看:http://wewoor.github.io/CNUI/document.html 。
//Alert
var Alert = { func : function(){},
name : "dialog",
show : function(msg){ //show function
var confirm = document.createElement("div");
confirm.className = this.name;
confirm.id = this.name;;
var confirmHeader = document.createElement("div");
confirmHeader.className = "dialog-header";
var headerSpan = document.createElement("span");
headerSpan.className = "dialog-title";
var confirmContent = document.createElement("div");
confirmContent.className = "dialog-content";
var contentSpan = document.createElement("span");
contentSpan.className = "content-main alert-content";
var contentBtnDiv = document.createElement("div");
contentBtnDiv.className="content-btn";
var btnConfirm = document.createElement("a"); //确认按钮
btnConfirm.href = "javascript:Alert.hide()";
btnConfirm.className = "btn alert"; //按钮添加
contentBtnDiv.appendChild(btnConfirm); confirmContent.appendChild(contentSpan);
confirmContent.appendChild(contentBtnDiv); confirmHeader.appendChild(headerSpan); confirm.appendChild(confirmHeader);
confirm.appendChild(confirmContent); //default button
headerSpan.innerHTML = "警示框!";
btnConfirm.innerHTML = "确认";
contentSpan.innerHTML = "这是一个警示框!";
if(msg != null && msg != undefined){
contentSpan.innerHTML = msg;
} document.body.appendChild(confirm);
return confirm;
},
hide : function(){
var confirm = document.getElementById(this.name);
if(confirm != null && confirm != undefined){
document.body.removeChild(confirm);
}
}
}
3.Confirm 组件
confirm对话框并没有像原生的confirm组件返回true 或者false,而是点击确认按钮后执行了传入的回调函数,点击取消按钮则移除了这个组件。demo请看:http://wewoor.github.io/CNUI/document.html
//Confirm
var Confirm = {
func : function(){},
name : "dialog",
show : function(msg,call){ //show function
var confirm = document.createElement("div");
confirm.className = this.name;
confirm.id = this.name;;
var confirmHeader = document.createElement("div");
confirmHeader.className = "dialog-header";
var headerSpan = document.createElement("span");
headerSpan.className = "dialog-title";
var confirmContent = document.createElement("div");
confirmContent.className = "dialog-content";
var contentSpan = document.createElement("span");
contentSpan.className = "content-main";
var contentBtnDiv = document.createElement("div");
contentBtnDiv.className="content-btn";
var btnConfirm = document.createElement("a"); //确认按钮
btnConfirm.href = "javascript:Confirm.callback()";
btnConfirm.className = "btn";
var btnCancle = document.createElement("a"); //取消按钮
btnCancle.className = "btn";
btnCancle.href = "javascript:Confirm.hide()"; //按钮添加
contentBtnDiv.appendChild(btnConfirm);
contentBtnDiv.appendChild(btnCancle); confirmContent.appendChild(contentSpan);
confirmContent.appendChild(contentBtnDiv); confirmHeader.appendChild(headerSpan); confirm.appendChild(confirmHeader);
confirm.appendChild(confirmContent); //default style
headerSpan.innerHTML = "对话框";
contentSpan.innerHTML = "这是确认对话框?";
btnConfirm.innerHTML = "确认";
btnCancle.innerHTML = "取消"; //if the property html is not null and not undefined
//just set this property.
if(msg != undefined){
contentSpan.innerHTML = msg;
} //set callback
if(call != null && call != undefined){
if(typeof(call) == "function"){
this.func = call;
}
} document.body.appendChild(confirm);
return confirm;
}, hide : function(){
var confirm = document.getElementById(this.name);
if(confirm != null && confirm != undefined){
document.body.removeChild(confirm);
}
},
callback : function(){
//执行call回调方法
this.func();
//隐藏confirm对象
this.hide();
}
}
JS模拟Alert与Confirm对话框的更多相关文章
- [转]jQuery插件实现模拟alert和confirm
本文转自:http://www.jb51.net/article/54577.htm (function () { $.MsgBox = { Alert: function (title, msg) ...
- 模拟alert和confirm
啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了... 来看插件的实现代码吧: (function () { $ ...
- 自编jQuery插件实现模拟alert和confirm
现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了自己定制一个的想法...... 啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的al ...
- js的alert和confirm美化
--前言-- window对象的alert和confirm标准方法在不同浏览器的显示效果不太相同,有个相同点是都不是很美观.我们的想法是使用js和css分别仿照它们,提供另一套函数,使在不同浏览器的有 ...
- 模拟alert,confirm 阻塞状态
/*** * 模拟alert弹窗 * content 为弹框显示的内容 * 确定按钮对应的下面取消关闭显示框 * **/function oAlert(content) { var oWrap = $ ...
- 最近总想着写一个模拟alert和confirm插件,代替原生的
msgbox-confirm github: https://github.com/tong-mikasa/msgbox-confirm 主要js代码,scss代码 (function($) { $ ...
- 原生 js 模拟 alert 弹窗
复制头部的 js 代码到你的 js 文件的任何地方,调用Chef.alert方法传入相应的参数即可并没有什么功能,只是一个提示的作用,可能样式比 alert 的弹窗好看点,css是写在js里的,只要你 ...
- 在Android的webview中定做js的alert,confirm和prompt对话框的方法
在Android的webview中定制js的alert,confirm和prompt对话框的方法 http://618119.com/archives/2010/12/20/199.html 1.首先 ...
- (转)在Android的webview中定制js的alert,confirm和prompt对话框的方法
1.首先继承android.webkit.WebChromeClient实现MyWebChromeClient. 2.在MyWebChromeClient.java中覆盖onJsAlert,onJsC ...
随机推荐
- centos性能监控系列一:常用监控命令
Linux系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工具来判断究竟是哪一部分(内存.CPU.硬盘--)出了问题 下面就让我们了解一下这些常用的性能监控工具. 1.upti ...
- solr 导入数据
从sqlserver导入数据到solr, solr 采用的版本6.0.1,并且本机解压到:F:\Tool\solr-6.0.1: 1. 命令启动solr,创建core 启动,进入solr文件目录下,执 ...
- fis自动化部署
1,自动部署到远程服务器 (1),参考:https://github.com/fex-team/receiver (2),接收服务代码目录:/var/www/html/fis/receiver-mas ...
- IdentityHashMap的使用场景
IdentityHashMap的使用场景 JDK1.4就加入了这个map类型,它是使用 == 判断相等,而不是hashmap的equals方法判断相等. 那么,它有什么应用场合呢? 当然是需要我们必须 ...
- 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”
摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...
- 烂泥:LVM学习之逻辑卷LV及卷组扩容VG
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上篇文章中介绍了有关LVM基础的知识,这篇文章我们来介绍如何给LVM的逻辑卷LV及卷组VG扩容. LVM的逻辑卷,我们知道它最后相当于一个分区,既然是一 ...
- jsp EL 表达式
EL表达式 EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有E ...
- nginx命令详解
nginx的configure命令支持以下参数: --prefix=path 定义一个目录,存放服务器上的文件 ,也就是nginx的安装目录.默认使用 /usr/local/nginx. --s ...
- Unity 2D Sprite Lighting
2D游戏中也可以使用灯光?这真是一个好消息,接下来,我将为大家写一下教程 操作步骤 1.创建一个Materilas,修改Shader为 2.创建一个Sprite(使用黑色的图片) 3.创建一个Poin ...
- 会报编译器警告的Xcode 6.3新特性:Nullability Annotations
最近在用Xcode 6.3写代码,一些涉及到对象的代码会报如下编译器警告: 1 Pointer is missing a nullability type specifier (__nonnull o ...