JavaScript插件化開發(fā)教程(五)
一,開篇分析
Hi,大家好!前兩篇文章我們主要講述了以“jQuery的方式如何開發(fā)插件”,以及過程化設(shè)計與面向?qū)ο笏枷朐O(shè)計相結(jié)合的方式是如何設(shè)計一個插件的,兩種方式各有利弊取長補(bǔ)短,嘿嘿嘿,廢話少說,進(jìn)入正題。直接上實際效果圖:

大家看到了吧,這是一個下拉菜單插件,在我們?nèi)粘i_發(fā)中,系統(tǒng)提供的可能有時讓我們覺得不是很美觀并且功能有限,造成用戶
的體驗形式以及用戶的可交互性不是很好,所以今天模擬一個嘿嘿嘿。下面就具體分析一下吧。
?。ǘ瑢嵗治?/strong>
?。?),首先確定這個插件做什么事。下面看一下插件的調(diào)用方式,以及配置參數(shù)說明。如下代碼:
$(function(){
var itemSelector = new ItemSelector($("#item-selector"),{
currentText : "Please Choose Item" ,
items : [
{
text : "JavaScript" ,
value : "js" ,
disabled : "1"
} ,
{
text : "Css" ,
value : "css" ,
disabled : "0"
} ,
{
text : "Html" ,
value : "html" ,
disabled : "0"
}
] ,
mode : "0" , // 為"1"時支持checkbox多選模式
change : function(value){
// put your code here
}
}) ;
itemSelector.init() ;
setTimeout(function(){
console.log(itemSelector.getCurrentValue()) ; // 測試 獲取當(dāng)先選中項
},2000) ;
“var itemSelector = new ItemSelector()”里面包含兩個參數(shù),第一個是dom節(jié)點(diǎn)對象,第二個是插件參數(shù)選項,"currentText"代表“ItemSelector“插件中,選中文本顯示區(qū)域的文字描述。
”items“是一個數(shù)組,里面包含的是“ItemSelector”項目的屬性,包括文字描述,選項值,”disabled“代表列表條目的可顯度,0代表顯示,1代表不可顯示。
”change“代表選中時的操作回調(diào)函數(shù),選項數(shù)據(jù)會以參數(shù)的形式進(jìn)行回傳。
?。?),所涉的功能有哪些
可顯的效果圖如下:

不可顯的效果圖如下:

二者的區(qū)別是:不可現(xiàn)狀態(tài)數(shù)據(jù)不會回傳,懸浮上去不會有任何效果。
三),完整代碼以供學(xué)習(xí),本代碼已經(jīng)過測試,包括目錄結(jié)構(gòu)以及相關(guān)的文件。
(1),html
<body>
<div class="dxj-ui-hd">
大熊君{{bb}} - DXJ UI ------ ItemSelector
</div>
<div class="dxj-ui-bd">
<div id="item-selector">
<div class="title">
<div></div><span>↓</span>
</div>
<div class="content">
<div class="items">
</div>
</div>
</div>
</div>
</body>
(2),css
/* item-selector */
#item-selector {
margin : 0 auto;
width : 220px ;
overflow:hidden;
border:2px solid #ccc;
}
#item-selector .title {
border-bottom:1px solid #ccc;
overflow:hidden;
}
#item-selector .title div {
width:190px;
border:0px ;
color:#999;
font-family: arial ;
font-size: 14px;
height:28px;
line-height:28px;
float:left;
cursor:pointer;
}
#item-selector .title span {
display:block;
height:30px;
line-height:30px;
width:29px;
float:left;
text-align:center;
border-left:1px solid #ccc;
cursor:pointer;
}
#item-selector .content {
width : 220px ;
overflow:hidden;
}
#item-selector .content .items {
overflow:hidden;
}
#item-selector .content .items div {
padding-left:20px;
width : 200px ;
height:32px;
line-height:32px;
font-family: "微軟雅黑" ;
font-size: 14px;
font-weight:bold;
cursor:pointer;
}
.item-hover {
background:#3385ff;
color:#fff;
}
?。?),"ItemSelector.js"
function ItemSelector(elem,opts){
this.elem = elem ;
this.opts = opts ;
} ;
var ISProto = ItemSelector.prototype ;
ISProto.getElem = function(){
return this.elem ;
} ;
ISProto.getOpts = function(){
return this.opts ;
} ;
/* data manip*/
ISProto._setCurrent = function(current){
this.getOpts()["current"] = current ;
} ;
ISProto.getCurrentValue = function(current){
return this.getOpts()["current"] ;
} ;
/* data manip*/
ISProto.init = function(){
var that = this ;
this.getOpts()["current"] = null ; // 數(shù)據(jù)游標(biāo)
this._setItemValue(this.getOpts()["currentText"]) ;
var itemsElem = that.getElem().find(".content .items") ;
this.getElem().find(".title div").on("click",function(){
itemsElem.toggle() ;
}) ;
this.getElem().find(".title span").on("click",function(){
itemsElem.toggle() ;
}) ;
$.each(this.getOpts()["items"],function(i,item){
item["id"] = (new Date().getTime()).toString() ;
that._render(item) ;
}) ;
} ;
ISProto._setItemValue = function(value){
this.getElem().find(".title div").text(value)
} ;
ISProto._render = function(item){
var that = this ;
var itemElem = $("<div></div>")
.text(item["text"])
.attr("id",item["id"]) ;
if("0" == item["disabled"]){
itemElem.on("click",function(){
var onChange = that.getOpts()["change"] ;
that.getElem().find(".content .items").hide() ;
that._setItemValue(item["text"]) ;
that._setCurrent(item) ;
onChange && onChange(item) ;
})
.mouseover(function(){
$(this).addClass("item-hover") ;
})
.mouseout(function(){
$(this).removeClass("item-hover") ;
}) ;
}
else{
itemElem.css("color","#ccc").on("click",function(){
that.getElem().find(".content .items").hide() ;
that._setItemValue(item["text"]) ;
}) ;
}
itemElem.appendTo(this.getElem().find(".content .items")) ;
} ;
(四),最后總結(jié)
?。?),面向?qū)ο蟮乃伎挤绞胶侠矸治龉δ苄枨蟆?/p>
?。?),以類的方式來組織我們的插件邏輯。
?。?),不斷重構(gòu)上面的實例,如何進(jìn)行合理的重構(gòu)那?不要設(shè)計過度,要游刃有余,推薦的方式是過程化設(shè)計與面向?qū)ο笏枷朐O(shè)計相結(jié)合。
(4),下篇文章中會擴(kuò)展相關(guān)功能,比如“mode”這個屬性,為"1"時支持checkbox多選模式,現(xiàn)在只是默認(rèn)下拉模式。
本文先到這里了,后續(xù)我們再繼續(xù)討論,希望小伙伴們能夠喜歡本系列文章。
相關(guān)文章
JavaScript函數(shù)式編程實現(xiàn)介紹
函數(shù)式編程是一種編程范式,將整個程序都由函數(shù)調(diào)用以及函數(shù)組合構(gòu)成。 可以看成一條流水線,數(shù)據(jù)可以不斷地從一個函數(shù)的輸出流入另一個函數(shù)的輸入,最后輸出結(jié)果2022-09-09
JS+CSS實現(xiàn)簡單的二級下拉導(dǎo)航菜單效果
這篇文章主要介紹了JS+CSS實現(xiàn)簡單的二級下拉導(dǎo)航菜單效果,通過簡單的JavaScript頁面元素遍歷及樣式操作實現(xiàn)下拉菜單效果,非常簡單實用,需要的朋友可以參考下2015-09-09
頁面間固定參數(shù),通過cookie傳值的實現(xiàn)方法
下面小編就為大家?guī)硪黄撁骈g固定參數(shù),通過cookie傳值的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
javascript Array.prototype.slice的使用示例
javascript Array.prototype.slice除了常見的從某個數(shù)組中抽取出新的數(shù)組外,它還有一些其他的用法,下面就為大家講這些妙用2013-11-11

