jQuery)扩展jQuery系列之一 模拟alert,confirm(一)


Posted in Javascript onDecember 04, 2010

效果图

jQuery)扩展jQuery系列之一 模拟alert,confirm(一)

全部代码
/** 
* @author xing 
*/ 
(function($){ 
$.extend({ 
alert:function(html,callback){ 
var dialog=new Dialog(); 
dialog.build('alert',callback,html); 
}, 
confirm:function(html,callback){ 
var dialog=new Dialog(); 
dialog.build('confirm',callback,html); 
} 
}); 
var Dialog=function(){ 
var render={ 
template:' <div class="alertParent"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!</div><div class="btnBar"><input type="button" value="确定" id="sure"/></div></div></div>', 
templateConfirm:' <div class="alertParent" id="confirmPanel"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!</div><div class="btnBar"><input type="button" value="取消" id="cancel"/><input type="button" value="确定" id="sure"/></div></div></div>', 
/** 
* 根据需要生成对话框 
* @param {Object} type 
* @param {Object} callback 
* @param {Object} html 
*/ 
renderDialog:function(type,callback,html){ 
if(type=='alert'){ 
this.renderAlert(callback,html); 
}else{ 
this.renderConfirm(callback,html); 
} 
}, 
/** 
* 生成alert 
* @param {Object} callback 
* @param {Object} html 
*/ 
renderAlert:function(callback,html){ 
var temp=$(this.template).clone().hide(); 
temp.find('div.alertHtml').html(html); 
$(document.body).append(temp); 
this.setPosition(temp); 
temp.fadeIn(); 
this.bindEvents('alert',temp,callback); 
}, 
/** 
* 生成confirm 
* @param {Object} callback 
* @param {Object} html 
*/ 
renderConfirm:function(callback,html){ 
var temp=$(this.templateConfirm).clone().hide(); 
temp.find('div.alertHtml').html(html); 
$(document.body).append(temp); 
this.setPosition(temp); 
temp.fadeIn(); 
this.bindEvents('confirm',temp,callback); 
}, 
/** 
* 设定对话框的位置 
* @param {Object} el 
*/ 
setPosition:function(el){ 
var width=el.width(), 
height=el.height(), 
pageSize=this.getPageSize(); 
el.css({ 
top:(pageSize.h-height)/2, 
left:(pageSize.w-width)/2 
}); 
}, 
/** 
* 绑定事件 
* @param {Object} type 
* @param {Object} el 
* @param {Object} callback 
*/ 
bindEvents:function(type,el,callback){ 
if(type=="alert"){ 
if($.isFunction(callback)){ 
$('#sure').click(function(){ 
callback(); 
$('div.alertParent').remove(); 
}); 
}else{ 
$('#sure').click(function(){ 
$('div.alertParent').remove(); 
}); 
} 
}else{ 
if($.isFunction(callback)){ 
$('#sure').click(function(){ 
callback(); 
$('div.alertParent').remove(); 
}); 
}else{ 
$('#sure').click(function(){ 
$('div.alertParent').remove(); 
}); 
} 
$('#cancel').click(function(){ 
$('div.alertParent').remove(); 
}); 
} 
}, 
/** 
* 获取页面尺寸 
*/ 
getPageSize:function(){ 
return { 
w:document.documentElement.clientWidth, 
h:document.documentElement.clientHeight 
} 
} 
} 
return { 
build:function(type,callback,html){ 
render.renderDialog(type,callback,html); 
} 
} 
} 
})(jQuery);

因为我们的alert,并不需要选择器的支持,所以我们采用$.extend这样声明
$.extend({ 
alert:function(html,callback){ 
}, 
confirm:function(html,callback){ 
} 
});

其次我们声明一个单体来生成这个组件到页面,这个单体返回一个公共的方法build来创建这个组件
var Dialog=function(){ 
return { 
build:function(type,callback,html){ 
render.renderDialog(type,callback,html); 
} 
} 
}

接下来我们分别声明组件的HTML字符串
var render={<BR> template:' <div class="alertParent"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误! 
</div><div class="btnBar"><input type="button" value="确定" id="sure"/></div></div></div>',<BR> templateConfirm:' <div class="alertParent" 
id="confirmPanel"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!</div><div class="btnBar"><input type="button" value="取消" 
id="cancel"/><input type="button" value="确定" id="sure"/></div></div></div>'}<BR>

向里面填充方法
/** 
* 根据需要生成对话框 
* @param {Object} type 
* @param {Object} callback 
* @param {Object} html 
*/ 
renderDialog:function(type,callback,html){ 
if(type=='alert'){ 
this.renderAlert(callback,html); 
}else{ 
this.renderConfirm(callback,html); 
} 
}, 
/** 
* 生成alert 
* @param {Object} callback 
* @param {Object} html 
*/ 
renderAlert:function(callback,html){ 
var temp=$(this.template).clone().hide(); 
temp.find('div.alertHtml').html(html); 
$(document.body).append(temp); 
this.setPosition(temp); 
temp.fadeIn(); 
this.bindEvents('alert',temp,callback); 
}, 
/** 
* 生成confirm 
* @param {Object} callback 
* @param {Object} html 
*/ 
renderConfirm:function(callback,html){ 
var temp=$(this.templateConfirm).clone().hide(); 
temp.find('div.alertHtml').html(html); 
$(document.body).append(temp); 
this.setPosition(temp); 
temp.fadeIn(); 
this.bindEvents('confirm',temp,callback); 
}, 
/** 
* 设定对话框的位置 
* @param {Object} el 
*/ 
setPosition:function(el){ 
var width=el.width(), 
height=el.height(), 
pageSize=this.getPageSize(); 
el.css({ 
top:(pageSize.h-height)/2, 
left:(pageSize.w-width)/2 
}); 
}, 
/** 
* 绑定事件 
* @param {Object} type 
* @param {Object} el 
* @param {Object} callback 
*/ 
bindEvents:function(type,el,callback){ 
if(type=="alert"){ 
if($.isFunction(callback)){ 
$('#sure').click(function(){ 
callback(); 
$('div.alertParent').remove(); 
}); 
}else{ 
$('#sure').click(function(){ 
$('div.alertParent').remove(); 
}); 
} 
}else{ 
if($.isFunction(callback)){ 
$('#sure').click(function(){ 
callback(); 
$('div.alertParent').remove(); 
}); 
}else{ 
$('#sure').click(function(){ 
$('div.alertParent').remove(); 
}); 
} 
$('#cancel').click(function(){ 
$('div.alertParent').remove(); 
}); 
} 
}, 
/** 
* 获取页面尺寸 
*/ 
getPageSize:function(){ 
return { 
w:document.documentElement.clientWidth, 
h:document.documentElement.clientHeight 
} 
}

接下来就是对话框的实现
$.extend({ 
alert:function(html,callback){ 
var dialog=new Dialog(); 
dialog.build('alert',callback,html); 
}, 
confirm:function(html,callback){ 
var dialog=new Dialog(); 
dialog.build('confirm',callback,html); 
} 
});

调用方法:
$.confirm('确定删除?',function(){ 
alert('cccc'); 
});

jQuery)扩展jQuery系列之一 模拟alert,confirm(一)

$.alert('我的电脑');

jQuery)扩展jQuery系列之一 模拟alert,confirm(一)

最后就是CSS与HTML 了
div.alertParent{ 
padding:6px; 
background:#ccc; 
background:rgba(201,201,201,0.8); 
width:auto; 
position:absolute; 
-moz-border-radius:3px; 
font-size:12px; 
top:50px; 
left:50px; 
} 
div.alertContent{ 
background:#fff; 
width:350px; 
height:auto; 
border:1px solid #999; 
} 
h2.title{ 
width:100%; 
height:28px; 
background:#0698E9; 
text-indent:10px; 
font-size:12px; 
color:#fff; 
line-height:28px; 
margin:0; 
} 
div.alertHtml{ 
background:url(dtips.gif) 0 0 no-repeat; 
height:50px; 
margin:10px; 
margin-left:30px; 
text-indent:50px; 
line-height:50px; 
font-size:14px; 
} 
div.btnBar{ 
border-top:1px solid #c6c6c6; 
background:#f8f8f8; 
height:30px; 
} 
div.btnBar input{ 
background:url(sure.png) no-repeat; 
border:0; 
width:63px; 
height:28px; 
float:right; 
margin-right:5px; 
}

html
<div class="alertParent"><BR><div class="alertContent"><BR><h2 class="title">系统提示</h2><BR><div class="alertHtml"><BR>你的操作出现错误!<BR></div><BR> <div class="btnBar"><BR> <input 
type="button" value="确定"/><BR></div><BR></div><BR> </div><BR>

高手勿笑,为了说明实现的方式,我并没有仔细的去完善这段代码,仅仅是在写作的半小时内写出的,所以有很多地方没有去思考,有很多的纰漏,并且以一个比较笨的方式实现了这个模拟的alert,不过下次我们将通过构建Button的方式实现一个组件,会加入遮罩,ajax调用,iframe宽度高度自适应等更强大的功能。
Javascript 相关文章推荐
小议Function.apply()之二------利用Apply的参数数组化来提高 JavaScript程序性能
Nov 30 Javascript
基于MooTools的很有创意的滚动条时钟动画
Nov 14 Javascript
jquery控制背景音乐开关与自动播放提示音的方法
Feb 06 Javascript
基于jquery实现动态竖向柱状条特效
Feb 12 Javascript
jQuery自定义数值抽奖活动代码
Jun 11 Javascript
JavaScript 节流函数 Throttle 详解
Jul 04 Javascript
canvas绘制万花筒效果(代码分享)
Jan 20 Javascript
jQuery快速高效制作网页交互特效
Feb 24 Javascript
jQuery插件select2利用ajax高效查询大数据列表(可搜索、可分页)
May 19 jQuery
Vue配合iView实现省市二级联动的示例代码
Jul 27 Javascript
JS实现的杨辉三角【帕斯卡三角形】算法示例
Feb 26 Javascript
Openlayers3实现车辆轨迹回放功能
Sep 29 Javascript
高效的表格行背景隔行变色及选定高亮的JS代码
Dec 04 #Javascript
javascript innerHTML使用分析
Dec 03 #Javascript
统计出现最多的字符次数的js代码
Dec 03 #Javascript
解决jquery的.animate()函数在IE6下的问题
Dec 03 #Javascript
基于jQuery的左右滚动实现代码
Dec 03 #Javascript
基于jquery的finkyUI插件与Ajax实现页面数据加载功能
Dec 03 #Javascript
Web开发者必备的12款超赞jQuery插件
Dec 03 #Javascript
You might like
递归列出所有文件和目录
2006/10/09 PHP
php中XMLHttpRequest(Ajax)不能设置自定义的Referer的解决方法
2011/11/26 PHP
PHP性能优化准备篇图解PEAR安装
2011/12/05 PHP
phplot生成图片类用法详解
2015/01/06 PHP
jQuery页面图片伴随滚动条逐渐显示的小例子
2013/03/21 Javascript
javascript window.open打开新窗口后无法再次打开该窗口问题的解决方法
2014/04/12 Javascript
jquery带翻页动画的电子杂志代码分享
2015/08/21 Javascript
js实现跨域的多种方法
2015/12/25 Javascript
javascript多物体运动实现方法分析
2016/01/08 Javascript
Angular.js回顾ng-app和ng-model使用技巧
2016/04/26 Javascript
基于Marquee.js插件实现的跑马灯效果示例
2017/01/25 Javascript
JavaScript正则表达式简单实用实例
2017/06/23 Javascript
Angular.js项目中使用gulp实现自动化构建以及压缩打包详解
2017/07/19 Javascript
JS实现读取xml内容并输出到div中的方法示例
2018/04/19 Javascript
JS 获取文件后缀,判断文件类型(比如是否为图片格式)
2020/05/09 Javascript
[00:37]食人魔魔法师轮盘吉兆顺应全新至宝将拥有额外款式
2019/12/19 DOTA
python静态方法实例
2015/01/14 Python
把MySQL表结构映射为Python中的对象的教程
2015/04/07 Python
简述Python中的面向对象编程的概念
2015/04/27 Python
Python中条件判断语句的简单使用方法
2015/08/21 Python
python列表插入append(), extend(), insert()用法详解
2019/09/14 Python
Django生成PDF文档显示网页上以及PDF中文显示乱码的解决方法
2019/12/17 Python
Tensorflow 实现将图像与标签数据转化为tfRecord文件
2020/02/17 Python
Pandas之缺失数据的实现
2021/01/06 Python
Cole Haan官方网站:美国时尚潮流品牌
2017/12/06 全球购物
英国卫浴商店:Ergonomic Design
2019/09/22 全球购物
德国、奥地利和瑞士最大的旅行和度假门户网站:HolidayCheck
2019/11/14 全球购物
马来西亚网上花店:FlowerAdvisor马来西亚
2020/01/03 全球购物
小学生作文评语
2014/04/18 职场文书
企业安全生产月活动总结
2014/07/05 职场文书
2014年度安全生产目标管理责任书
2014/07/25 职场文书
幼儿园个人师德总结
2015/02/06 职场文书
统计员岗位职责
2015/02/11 职场文书
中秋节作文(五年级)之关于月亮
2019/09/11 职场文书
互联网创业商业模式以及赚钱法则有哪些?
2019/10/12 职场文书
JS封装cavans多种滤镜组件
2022/02/15 Javascript