JavaScript插件化開發(fā)講解

    時間:2024-05-26 05:15:37 JavaScript 我要投稿
    • 相關(guān)推薦

    JavaScript插件化開發(fā)講解

      (一),開篇分析

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

      大家看到了吧,這是一個下拉菜單插件,在我們?nèi)粘i_發(fā)中,系統(tǒng)提供的可能有時讓我們覺得不是很美觀并且功能有限,造成用戶

      的體驗形式以及用戶的可交互性不是很好,所以今天模擬一個嘿嘿嘿。下面就具體分析一下吧。

      (二),實例分析

      (1),首先確定這個插件做什么事。下面看一下插件的調(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()) ; // 測試 獲取當先選中項

      },2000) ;

      “var itemSelector = new ItemSelector()”里面包含兩個參數(shù),第一個是dom節(jié)點對象,第二個是插件參數(shù)選項,"currentText"代表“ItemSelector“插件中,選中文本顯示區(qū)域的文字描述。

      ”items“是一個數(shù)組,里面包含的是“ItemSelector”項目的屬性,包括文字描述,選項值,”disabled“代表列表條目的可顯度,0代表顯示,1代表不可顯示。

      ”change“代表選中時的操作回調(diào)函數(shù),選項數(shù)據(jù)會以參數(shù)的形式進行回傳。

      (2),所涉的功能有哪些

      可顯的效果圖如下:

      不可顯的效果圖如下:

      二者的區(qū)別是:不可現(xiàn)狀態(tài)數(shù)據(jù)不會回傳,懸浮上去不會有任何效果。

      (三),完整代碼以供學習,本代碼已經(jīng)過測試,包括目錄結(jié)構(gòu)以及相關(guān)的文件。

      (1),html

      復制代碼 代碼如下:

      大熊君{{bb}} - DXJ UI ------ ItemSelector

      ↓

      (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;

      }

      (3),"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ù)游標

      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 = $("

      ")

      .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é)

      (1),面向?qū)ο蟮乃伎挤绞胶侠矸治龉δ苄枨蟆?/p>

      (2),以類的方式來組織我們的插件邏輯。

      (3),不斷重構(gòu)上面的實例,如何進行合理的重構(gòu)那?不要設(shè)計過度,要游刃有余,推薦的方式是過程化設(shè)計與面向?qū)ο笏枷朐O(shè)計相結(jié)合。

      (4),下篇文章中會擴展相關(guān)功能,比如“mode”這個屬性,為"1"時支持checkbox多選模式,現(xiàn)在只是默認下拉模式。

      本文先到這里了,后續(xù)我們再繼續(xù)討論,希望小伙伴們能夠喜歡本系列文章。

    【JavaScript插件化開發(fā)講解】相關(guān)文章:

    JavaScript的課堂講解09-03

    JavaScript日期時間格式化函數(shù)08-29

    對javascript的理解08-08

    常用的JavaScript模式09-22

    Javascript的this用法簡述08-15

    JavaScript學習筆記08-24

    JavaScript 基礎(chǔ)教學09-29

    辦公自動化的應用與開發(fā)07-10

    AE影視常用插件FORM的應用技巧09-03

    JavaScript常用方法匯總10-25

    91久久大香伊蕉在人线_国产综合色产在线观看_欧美亚洲人成网站在线观看_亚洲第一无码精品立川理惠

      亚洲欧洲日产国码中文 | 五月天国产亚洲激情在线观看 | 色天使综合婷婷国产日韩AV | 伊人久久精品亚洲午夜 | 日韩欧美国产第一页 | 中文字幕在线观看不卡 |