// 接下來寫一個 類似的layer彈框插件;
function Layer (data){
if(!data){
return;
}
// 創(chuàng)建HTML頁面代碼
// 創(chuàng)建最外層盒子 樣式寫在 common.css中
this.LayerBox = document.createElement('div');
this.LayerBox.className = 'layerBox';
// 創(chuàng)建一個title 盒子
this.LayerTitle = document.createElement('div');
this.LayerTitle.className = "layerTitle";
// 創(chuàng)建內(nèi)容盒子
this.LayerContent = document.createElement('div');
this.LayerContent.className = "layerContent";
// 創(chuàng)建一個 確定按鈕盒子
this.LayerConfirmBox = document.createElement('div');
this.LayerConfirmBox.className = "layerConfirmBox";
this.LayerConfirmBtn = document.createElement('a');
// 創(chuàng)建關(guān)閉彈框按鈕HTML代碼
this.LayerCloseBox = document.createElement('span');
this.LayerCloseBox.className = "layerCloseBox";
this.LayerCloseIcon = document.createElement('i');
// 創(chuàng)建朦朧層HTML代碼
this.LayerHazyBox = document.createElement('div');
this.LayerHazyBox.className = "layerHazyBox"
// 定義盒子內(nèi)容
this.title = data.title;
this.content = data.content;
this.btnText = data.btnText;
this.LayerTitle.innerHTML = this.content || "信息";
this.LayerConfirmBtn.innerHTML = this.btnText || "確定";
this.LayerContent.innerHTML = this.content || "默認(rèn)信息";
// 添加一個確定后的回調(diào)函數(shù)
this.success = data.success || function(){};
// 添加一個關(guān)閉彈框的回調(diào)函數(shù)
this.close = data.close || function(){};
}
Layer.prototype = {
init : function(){
this.LayerBox.appendChild(this.LayerTitle);
this.LayerBox.appendChild(this.LayerContent);
this.LayerBox.appendChild(this.LayerConfirmBox);
this.LayerBox.appendChild(this.LayerCloseBox);
this.LayerCloseBox.appendChild(this.LayerCloseIcon);
this.LayerConfirmBox.appendChild(this.LayerConfirmBtn);
document.body.appendChild(this.LayerBox);
document.body.appendChild(this.LayerHazyBox);
// 讓彈框出現(xiàn)
this.show();
// 確定跟關(guān)閉盒子綁定事件
this.bindEvent();
},
bindEvent : function(){
// 給 確定 跟 x 綁定事件
let _this = this;
this.LayerConfirmBtn.onclick = function(){
_this.success();
_this.hide();
}
this.LayerCloseIcon.onclick = function(){
_this.close();
_this.hide();
}
},
show : function(){
this.LayerBox.style.display = "block";
this.LayerHazyBox.style.display = "block";
},
hide : function(){
this.LayerBox.style.display = "none";
this.LayerHazyBox.style.display = "none";
// 讓HTML代碼刪除
document.body.removeChild(this.LayerBox);
document.body.removeChild(this.LayerHazyBox);
}
}
var btn = document.getElementById('btn');
btn.onclick = function(){
new Layer({
title:"",
content:"你好灸拍!",
success:function(){
alert('確定后執(zhí)行函數(shù)');
},
close:function(){
alert('點擊關(guān)閉執(zhí)行函數(shù)');
}
}).init();
}
// 使用子類繼承 父類 抽象 改變父類結(jié)構(gòu)
// 給確定按鈕 添加一個取消按鈕
function LayerInquiry (data){
// 改變this指向
Layer.call(this,data);
// 這里this只的是父級 Layer
// 創(chuàng)建一個取消按鈕
this.LayerCloseBtn = document.createElement('a');
this.btnCloseText = data.btnCloseText;
this.LayerCloseBtn.innerHTML = this.btnCloseText || "取消";
}
// 原型鏈繼承父級所有方法
LayerInquiry.prototype = new Layer();
LayerInquiry.prototype.init = function(){
// 改變初始化 取消按鈕添加到盒子
this.LayerConfirmBox.appendChild(this.LayerCloseBtn);
// 繼承父級原始的 init方法做祝;
Layer.prototype.init.call(this);
}
LayerInquiry.prototype.bindEvent = function(){
let _this = this;
// 為創(chuàng)建的取消按鈕 添加綁定事件
this.LayerCloseBtn.onclick = function(){
_this.hide();
}
// 繼承父級原始的 bindEvent方法;
Layer.prototype.bindEvent.call(this);
}
var btn2 = document.getElementById('btn2');
btn2.onclick = function(){
new LayerInquiry({
title:"添加需求的Title",
content:"添加取消了",
success:function(){
alert('確定后執(zhí)行函數(shù)');
},
close:function(){
alert('點擊關(guān)閉執(zhí)行函數(shù)');
}
}).init();
}
模版方法模式
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門卸察,熙熙樓的掌柜王于貴愁眉苦臉地迎上來脯厨,“玉大人,你說我怎么就攤上這事坑质『衔洌” “怎么了临梗?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長稼跳。 經(jīng)常有香客問我盟庞,道長,這世上最難降的妖魔是什么汤善? 我笑而不...
- 正文 為了忘掉前任茫经,我火速辦了婚禮,結(jié)果婚禮上萎津,老公的妹妹穿的比我還像新娘卸伞。我一直安慰自己,他們只是感情好锉屈,可當(dāng)我...
- 文/花漫 我一把揭開白布荤傲。 她就那樣靜靜地躺著,像睡著了一般颈渊。 火紅的嫁衣襯著肌膚如雪遂黍。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼敬飒,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了芬位?” 一聲冷哼從身側(cè)響起无拗,我...
- 正文 年R本政府宣布,位于F島的核電站撼泛,受9級特大地震影響挠说,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜愿题,卻給世界環(huán)境...
- 文/蒙蒙 一损俭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧潘酗,春花似錦杆兵、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缸兔,卻和暖如春日裙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背惰蜜。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 使用模版方法模式模擬不同計算機的啟動流程. 通過上面的例子可以看到,在startUp方法中有一些固定的步驟瓢宦,依次為...
- 本篇文章介紹一種設(shè)計模式——外觀模式。本篇文章內(nèi)容參考:《JAVA與模式》之模板方法模式灰羽,模板方法模式深度解析(三...