接着做关于alert组件的笔记

怎么又出来个中高级呢 对没错 就是出一个中高级来刷流量呵呵呵,但是中高级也不是白叫的 这次主要是增加了widget类,增加了自己绑定的事件和触发事件的方法!这么做是为什么呢 首先弥补浏览器自身事件交互的问题 最重要的就是 一个元素一个事件只能绑定一次!具体啥意思呢 就是一个元素只能添加一个click事件 要是在添加一个click事件就会把之前的click事件给覆盖掉(当然我就纳闷了你不会写一个事件里啊哈,但是说不定有特殊情况呢) 这是第一个原因,第二个原因就是尽量把一些关于dom的操作封装到组件的这个级别了,不要所有的事件操作都下降到dom这个层级 多了的化 会非常的乱套!为了解决上述两个问题,可以自己定义一个widget封装成一个类把事件绑定触发都放在这里,这样很多的时间绑定就可以直接写在调用组件的地方了 触发也简单了很多!不多说直接上图

下面是widget类

define(['jquery'] , function(){
function Widget(){
this.handlers = {};
}
Widget.prototype ={
on:function(type,handler){ if(typeof this.handlers[type] == 'undefined'){
this.handlers[type] = [];
}
this.handlers[type].push(handler);
return this;
},
fire:function(type,data){
if(this.handlers[type] instanceof Array){
var handlers = this.handlers[type]; for(var i = 0 ; i<handlers.length; i++){
handlers[i](data)
}
}
}
}
return {
Widget : Widget
}
})

把这个新的widget类引入到我们的组件中 如下是新window类

define(['jquery','widget'],function($,widget){
function Window(){
this.cfg = {
width:400,
height:200,
content:'我是默认文本内容',
handle:null,
title:'系统消息',
skinClassName:null,
hasCloseBtn:false,
hasMask:false,
handlerClosebtn:null
}
}
Window.prototype = $.extend({},new widget.Widget(),{
alert:function(cfg){ var that = this;
var CFG = $.extend(this.cfg,cfg);
//var boundingBox = $('<div class="window_boundingBox"></div>');
var boundingBox = $('<div class="window_boundingBox">'+
'<div class="window_header">'+CFG.title+'</div>'+
'<div class="window_body">'+CFG.content+'</div>'+
'<div class="window_footer"><input type="button" value="确定"></div>'+
'</div>'); boundingBox.appendTo('body') var btn = $('.window_footer input'); if(CFG.hasMask){ mask = $('<div class="window_mask"></div>');
mask.appendTo('body'); }
btn.appendTo(boundingBox);
btn.click(function(){
CFG.handle && CFG.handle();
boundingBox.remove();
mask && mask.remove();
}) boundingBox.css({
width:this.cfg.width + 'px',
height:this.cfg.height + 'px',
left:(CFG.x || (window.innerWidth - CFG.width)/2)+'px',
top:(CFG.y || (window.innerHeight - CFG.height)/2)+'px',
}) //右上角关闭按钮
if(CFG.hasCloseBtn){
var closeBtn = $('<span class="window_closeBtn">X</span>');
closeBtn.appendTo(boundingBox);
closeBtn.click(function(){
boundingBox.remove();
mask && mask.remove();
that.fire('close');
})
} //定制样式
if(CFG.skinClassName){
boundingBox.addClass(CFG.skinClassName);
} //绑定关闭样式
/*if(CFG.handlerClosebtn){
this.on('close',CFG.handlerClosebtn);
}*/
return this;
}
})
return {
Window:Window
}
})

最后在调用处的代码 很多触发事件代码就可以直接写在这里了很方便也简洁

require(['jquery','window'],function($,w){
var win = new w.Window();
win.alert({
width:500,
height:300,
content:'新年快乐',
title:'我是正确标题',
hasCloseBtn:true,
hasMask:true,
})
win.on('close' , function(){
alert('关闭');
})
})

最新文章

  1. LeetCode-Remove Duplicates from Sorted Array II
  2. 关于NGUI与原生2D混用相互遮盖的问题心得
  3. Linux系统安装时分区的选择(推荐)
  4. 第十八章:Android 打包部署
  5. android 4.4 状态栏隐藏
  6. PHP截取字符串 兼容utf-8 gb2312
  7. android EditText获取光标位置并安插字符删除字符
  8. Hartree-Fock理论(更新中)
  9. java线程(1)-线程同步
  10. MLlib--决策树
  11. centos7后台服务部署jar包
  12. sql的基本语句
  13. delphi 怎么实现主窗口退出时,有一个提示框?
  14. 基于ZooKeeper和Thrift构建动态RPC调用
  15. 详解掩膜mask
  16. [Windows Azure] Windows Azure Storage &amp; SQL Database
  17. 偏前端-纯css,手写轮播-(焦点切换 和 自动轮播 只可选择一种,两者不可共存)
  18. C++ Primer Plus学习:第十四章
  19. MySQL中文入库问题
  20. 《TCP/IP详解 卷1:协议》第3章 IP:网际协议

热门文章

  1. HDU 4165
  2. HDU 1285--确定比赛名次【拓扑排序 &amp;amp;&amp;amp; 邻接表实现】
  3. hdu5033 Building 单调队列
  4. javascript系列-class4.函数
  5. xBIM 基础04 日志操作
  6. awesome python 中文版 相见恨晚!(pythonNB的第三方资源库)
  7. 三个获取浏览器URL中参数值的方法
  8. Windows 10 秋季更新错误
  9. Activiti 23张表及7大服务详解
  10. CDR X6三折促销活动,可入