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

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

不可顯的效果圖如下:

二者的區(qū)別是:不可現(xiàn)狀態(tài)數(shù)據(jù)不會(huì)回傳,懸浮上去不會(huì)有任何效果。
三),完整代碼以供學(xué)習(xí),本代碼已經(jīng)過(guò)測(cè)試,包括目錄結(jié)構(gòu)以及相關(guān)的文件。
?。?),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>
(2),以類(lèi)的方式來(lái)組織我們的插件邏輯。
(3),不斷重構(gòu)上面的實(shí)例,如何進(jìn)行合理的重構(gòu)那?不要設(shè)計(jì)過(guò)度,要游刃有余,推薦的方式是過(guò)程化設(shè)計(jì)與面向?qū)ο笏枷朐O(shè)計(jì)相結(jié)合。
(4),下篇文章中會(huì)擴(kuò)展相關(guān)功能,比如“mode”這個(gè)屬性,為"1"時(shí)支持checkbox多選模式,現(xiàn)在只是默認(rèn)下拉模式。
本文先到這里了,后續(xù)我們?cè)倮^續(xù)討論,希望小伙伴們能夠喜歡本系列文章。
- JavaScript插件化開(kāi)發(fā)教程(六)
- JavaScript插件化開(kāi)發(fā)教程 (四)
- JavaScript插件化開(kāi)發(fā)教程 (三)
- JavaScript插件化開(kāi)發(fā)教程 (二)
- JavaScript插件化開(kāi)發(fā)教程 (一)
- 一看就懂的Android APP開(kāi)發(fā)入門(mén)教程
- Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
- Android筆記之:App應(yīng)用之啟動(dòng)界面SplashActivity的使用
- Android 避免APP啟動(dòng)閃黑屏的解決辦法(Theme和Style)
- 親自動(dòng)手實(shí)現(xiàn)Android App插件化
相關(guān)文章
javaScript實(shí)現(xiàn)滾動(dòng)條事件詳解
這篇文章主要為大家詳細(xì)介紹了javaScript實(shí)現(xiàn)滾動(dòng)條事件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
js實(shí)現(xiàn)下拉列表選中某個(gè)值的方法(3種方法)
這篇文章主要介紹了js實(shí)現(xiàn)下拉列表選中某個(gè)值的方法,涉及JavaScript針對(duì)select下拉列表選擇操作的相關(guān)技巧,需要的朋友可以參考下2015-12-12
JavaScript函數(shù)式編程實(shí)現(xiàn)介紹
函數(shù)式編程是一種編程范式,將整個(gè)程序都由函數(shù)調(diào)用以及函數(shù)組合構(gòu)成。 可以看成一條流水線,數(shù)據(jù)可以不斷地從一個(gè)函數(shù)的輸出流入另一個(gè)函數(shù)的輸入,最后輸出結(jié)果2022-09-09
JS+CSS實(shí)現(xiàn)簡(jiǎn)單的二級(jí)下拉導(dǎo)航菜單效果
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)簡(jiǎn)單的二級(jí)下拉導(dǎo)航菜單效果,通過(guò)簡(jiǎn)單的JavaScript頁(yè)面元素遍歷及樣式操作實(shí)現(xiàn)下拉菜單效果,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09
頁(yè)面間固定參數(shù),通過(guò)cookie傳值的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇頁(yè)面間固定參數(shù),通過(guò)cookie傳值的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
javascript Array.prototype.slice的使用示例
javascript Array.prototype.slice除了常見(jiàn)的從某個(gè)數(shù)組中抽取出新的數(shù)組外,它還有一些其他的用法,下面就為大家講這些妙用2013-11-11

