自己封裝js組件 - 中級

來源:互聯網
上載者:User

標籤:pen   remove   文本   div   dcl   val   query   addclass   ext   

書接上文,上次弄了個基本版本的alert組件(其實就是十分鐘前)但是很多功能都沒有實現 沒有關閉按鈕 沒有下面確定按鈕 沒有模態框 沒有這那的 這次終極篇就都給它完善好弄個中級版本也是基本可用版本!

 

define([‘jquery‘],function($){
function Window(){
this.cfg = {
width:400,
height:200,
content:‘我是預設常值內容‘,
handle:null,
title:‘系統訊息‘,
skinClassName:null,
hasCloseBtn:false,
hasMask:false
}
}
Window.prototype = {
alert:function(cfg){
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();
})
}

//定製樣式
if(CFG.skinClassName){
boundingBox.addClass(CFG.skinClassName);
}
}
}
return {
Window:Window
}
})

 

main.js調用

require([‘jquery‘,‘window‘],function($,w){
new w.Window().alert({
width:500,
height:300,
content:‘新年快樂‘,
title:‘我是正確標題‘,
hasCloseBtn:true,
hasMask:true
})
})

 

靠!靠!靠!直接上代碼  對就是這麼簡單暴力!

自己封裝js組件 - 中級

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.