javascript-模板方法模式-提示框归一化插件
模板方法模式笔记
父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤
实例:弹出框归一化插件
css样式
.alert{position: fixed;left: 50%;top: 50%;background-color: #ffffff;z-index:;width:400px;
border-radius: 5px;font-weight: bold;color: #535e66;box-shadow: 1px 1px 4px #eee,-1px -1px 4px #eee;margin-top:-9rem;margin-left:-200px;}
.alert h3{text-indent:2rem;border-bottom:1px solid #eef0f1;margin:;padding:1rem 0;}
.alert p{padding: 2rem 5rem;height: 5rem;overflow: hidden;margin:;border-bottom:1px solid #eef0f1;}
.alert .a-close{display: block;cursor: pointer;width: 12px;height: 12px;position: absolute;top: 1.2rem;right: 1.5rem;background: url(../img/icons.png) -48px -96px no-repeat;}
.alert .a-close:after{display: block;content: ""; clear: both;}
.alert .btn{display: inline-block;cursor: pointer;width: 4.5rem;height: 1.8rem;line-height: 1.8rem;text-align: center;color: #FFFFFF;
border-radius: 5px;margin:0.5rem;}
.alert .panelFooter{width:11rem;float:right;}
.alert .a-confirm{background-color: #0095d9;}
.alert .cancel{background-color: #546a79;}
.alert .right{float:right}
@media only screen and (min-width: 100px) and (max-width: 640px) {
.alert{width:80%;margin-left:-40%;}
.alert h3{text-indent: 1rem;}
.alert p{padding:1rem;}
.alert .a-close{right: 1rem;}
}
运用寄生组合继承方法
//原型式继承
function inheritobject(o){
//声明一个过渡函数对象
function F(){
}
//过渡原型对象继承父对象
F.prototype=o;
//返回过渡对象的一个实列,该实例的原型继承了父对象
return new F();
} /*
*寄生式继承 继承原型
* 传递参数subclass 子类
* 传递参数superclass 父类
* */
function inheritPrototype(subclass,superclass){
//复制一份父类的原型副本保存在变量中
var p=inheritobject(superclass.prototype);
//修正因为重写子类原型导致子类的constructor属性被修改
p.constructor=subclass;
//设置子类原型
subclass.prototype=p;
}
首先要创建基本提示框基类(模板类),然后其他提示框类只需要在继承的基础上,拓展自己所需即可,日后需求变更,修改基础类,所有提示框类实现的样式都会统一变化
基本提示框类:只实现一个提示内容、一个关闭按钮、一个确定按钮
var Alert = function(data){
if(!data) return;
this.content=data.content;
this.panel =document.createElement("div");//提示框面板
this.contentNode=document.createElement("p");
this.panelFooter=document.createElement("div");
this.confirmBtn=document.createElement("span");
this.closeBtn=document.createElement("b");
this.panel.className="alert";
this.panelFooter.className="panelFooter";
this.confirmBtn.className="btn a-confirm";
this.closeBtn.className="a-close";
this.confirmBtn.innerHTML=data.confirm || '确认';
this.contentNode.innerHTML=this.content;
this.success=data.success || function(){};
this.fail=data.fail || function(){};
}
Alert.prototype={
init :function(){
this.panel.appendChild(this.closeBtn);
this.panel.appendChild(this.contentNode);
this.panelFooter.appendChild(this.confirmBtn);
this.panel.appendChild(this.panelFooter);
document.body.appendChild(this.panel);
this.bindEvent();
this.show();
},
bindEvent : function(){
var me = this;
this.closeBtn.onclick = function(){
me.fail();
me.hide();
}
this.confirmBtn.onclick = function(){
me.success();
me.hide();
}
},
hide : function(){
this.panel.style.display ="none";
},
show :function(){
this.panel.style.display='block';
}
}
右侧按钮提示框类:继承基本提示框类,拓展需求,使按钮移动到右侧显示
//右侧按钮提示框
var RightAlert =function(data){
Alert.call(this,data);
this.panelFooter.className = this.panelFooter.className + ' right';
}
inheritPrototype(RightAlert,Alert);
标题提示框类 :继承基本提示框类,拓展需求,添加一个提示框标题
//标题提示框类
var TitleAlert=function(data){
Alert.call(this,data);
this.title = data.title;
this.titleNode = document.createElement("h3");
this.titleNode.innerHTML = this.title;
}
inheritPrototype(TitleAlert,Alert);
TitleAlert.prototype.init =function(){
this.panel.insertBefore(this.titleNode,this.panel.firstChild);
Alert.prototype.init.call(this); }
继承类也可作为模板
带有取消按钮的弹出框:继承标题提示框类,拓展需求,添加一个取消按钮
//继承类也可作为模板
var CancelAlert = function(data){
//继承标题提示框构造函数
TitleAlert.call(this,data);
//取消按钮文案
this.cancel = data.cancel;
this.cancelBtn = document.createElement('span');
this.cancelBtn.className='btn cancel';
this.cancelBtn.innerHTML = this.cancel || '取消';
}
inheritPrototype(CancelAlert,TitleAlert);
CancelAlert.prototype.init = function(){
TitleAlert.prototype.init.call(this);
this.panelFooter.appendChild(this.cancelBtn);
}
CancelAlert.prototype.bindEvent = function(){
var me=this;
TitleAlert.prototype.bindEvent.call(me);
this.cancelBtn.onclick = function(){
me.fail();
me.hide();
}
}
测试代码
new CancelAlert({
title : '提示',
content : '提示内容',
success : function(){
console.log('ok');
},
fail : function(){
console.log("cancel");
}
}).init();
浏览器显示

javascript-模板方法模式-提示框归一化插件的更多相关文章
- javascript模板方法模式
一:什么是模板方法模式: 模板方法模式由二部分组成,第一部分是抽象父类,第二部分是具体实现的子类,一般的情况下是抽象父类封装了子类的算法框架,包括实现一些公共方法及封装子类中所有方法的执行顺序,子类可 ...
- 轻松掌握:JavaScript模板方法模式
模板方法模式 假如我们有一些对象,各个对象之间有一些相同的行为,也有一些不同的行为,这时,我们就可以用模板方法模式来把相同的部分上移到它们的共同原型中(父类),而将不同的部分留给自己各自重新实现. 模 ...
- Jquery 弹出提示框输入插件 apprise 修改中文按钮以及使用说明
apprise的使用非常简单,引入js脚本和css <script type="text/javascript" src="/js/apprise-1.5.fu ...
- jQuery警告/确认/提示弹出对话框效果(替换传统JavaScript下的提示框)
http://www.51xuediannao.com/js/jquery/jquery_tsk/ http://www.jq22.com/demo/jqueryConfirm20160413/
- javascript中简单提示框
CSS部分 .help-tip{ width: 340px; border:1px solid #A0A0A0; background-color: #F8F8F8; border-radius: 5 ...
- JavaScript实现延时提示框
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- 基于jquery的提示框JavaScript 插件,类Bootstrap
目录 基于jquery的提示框JavaScript 插件,类Bootstrap 基于jquery的提示框JavaScript 插件,类Bootstrap 源码 github地址: https://gi ...
- 再起航,我的学习笔记之JavaScript设计模式17(模板方法模式)
模板方法模式 由模板方法模式开始我们正式告别结构型设计模式,开始行为型设计模式的学习分享 行为型设计模式用于不同对象之间职责划分或算法抽象,行为型设计模式不仅仅涉及类和对象,还涉及类或对象之间的交流模 ...
- javascript设计模式(张容铭)学习笔记 - 照猫画虎-模板方法模式
模板方法模式(Template Method):父类中定义一组操作算法骨架,而降一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤. 项目经理体验了各个页面的 ...
随机推荐
- ASP.NET MVC Model绑定(三)
ASP.NET MVC Model绑定(三) 前言 看过前两篇的朋友想必对Model绑定有个大概的了解,然而MVC框架给我们提供了更高的可扩展性的提供程序编程模式,也就是本篇的主题了,会讲解一下Mod ...
- 使用boilerplate模版创建解决方案
返回总目录<一步一步使用ABP框架搭建正式项目系列教程> 话不多说,让我们开始干吧!对于还没有接触ABP框架或者接触时间还不是很长的小伙伴来说,我建议还是使用官方建议的做法,那就是到ABP ...
- TODO:Go语言goroutine和channel使用
TODO:Go语言goroutine和channel使用 goroutine是Go语言中的轻量级线程实现,由Go语言运行时(runtime)管理.使用的时候在函数前面加"go"这个 ...
- 同步与异步 & 阻塞与非阻塞
在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 一.同步 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用 ...
- JavaScript权威设计--CSS(简要学习笔记十六)
1.Document的一些特殊属性 document.lastModified document.URL document.title document.referrer document.domai ...
- Android数据存储之SQLCipher数据库加密
前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...
- 【CSS进阶】试试酷炫的 3D 视角
写这篇文章的缘由是因为看到了这个页面: 戳我看看(移动端页面,使用模拟器观看) 运用 CSS3 完成的 3D 视角,虽然有一些晕3D,但是使人置身于其中的交互体验感觉非常棒,运用在移动端制作一些 H5 ...
- cmder git bash 使用
cmder 是一款 windows 下的命令集合软件,它可以集合各种系统下的命令,并且操作非常快速方便. 安装有两个版本,一个是简化版(4.27M),一个是完全版(75.7M),它们的唯一区别:完全版 ...
- Effective前端4:尽可能地使用伪元素
伪元素是一个好东西,但是很多人都没怎么用,因为他们觉得伪元素太诡异了.其实使用伪元素有很多好处,最大的好处是它可以简化页面的html标签,同时用起来也很方便,善于使用伪元素可以让你的页面更加地简洁优雅 ...
- SQL Server Management Studio 无法修改表,超时时间已到 在操作完成之前超时时
在修改表时,保存的时候显示:无法修改表,超时时间已到 在操作完成之前超时时间已过或服务器未响应 这是执行时间设置过短的原因,可以修改一下设置便能把执行时间加长,以便有足够的时间执行完修改动作. 在 S ...