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 ...
随机推荐
- 解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load
一直用CentOS 6 习惯了,一下没适应过来.防火墙配置后执行service iptables save 出现”Failed to restart iptables.service: Unit ip ...
- 《第一行代码——Android》
<第一行代码——Android> 基本信息 作者: 郭霖 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115362865 上架时间:2014-7-14 出版日期:2014 ...
- 读书笔记——数据库的ADO开发总结
基本上是对 ADO+开发指南.pdf 的摘抄和总结,以及个人心得. 文章末尾有<ADO+开发指南.pdf>的资源下载,附带个人批注. --------------------------- ...
- mysql插入数据与删除重复记录的几个例子(收藏)
mysql插入数据与删除重复记录的几个例子 12-26shell脚本实现mysql数据的批量插入 12-26mysql循环语句插入数据的例子 12-26mysql批量插入数据(insert into ...
- Solr与HBase架构设计
摘要:本篇是本人在做一个大数据项目时,对于系统架构的一点总结,如何在保证存储量的情况下,又能保证数据的检索速度. 前提: Solr.SolrCloud提供了一整套的数据检索方案,HBase提 ...
- cmd获取系统时间
查看时间格式: echo %date% echo %time% 通过%date:~5,2%来组合得出当前日期,组合的效果为yyyymmdd,date命令得到的日期格式默认为yyyy-mm-dd,通过% ...
- JVM 运行时数据区域
Java虚拟机管理的内存包括以下几个运行时数据区域: 1.程序计数器: 程序计数器是一块比较小的内存空间,是当前线程执行的字节码行号指示器.Java多线程是通过线程轮流切换来实现的,所以每个线程都有一 ...
- Quicksum -SilverN
quicksum Given a string of digits, find the minimum number of additions required for the string to e ...
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
- mac os下得pycharm怎么设置mercurial?
捣鼓了一会儿,最终搞定了. 先把链接贴上来:https://www.jetbrains.com/pycharm/help/mercurial.html 如果你发现你的pycharm在设置mercuri ...