手把手教你開發jquery外掛(三)
First, i want to add options to Tabs constructor like this:
var tabs = $("div.tabs").tabs({ "openEvent": "mouseover", "disabled": [1, 2], "current": 3 });
These options are borrowed from jQuery UI
Tabs:
openEvent:(String,"click") The type of event to be used for selecting a tab. disabled:(Array,[]) An array containing the position of the tabs (zero-based index) that should be disabled on initialization. current:(Number,0) Zero-based index of the tab to be selected on initialization. To set all tabs to unselected pass -1 as value.
The plugin code:
(function($) { function Tabs(tabs, panes, options) { var that = this; this.options = { "openEvent": "mouseover", "disabled": [], "current": 0 }; $.extend(this.options, options); this.tabs = tabs.removeClass("current"); this.panes = panes.hide(); this.current = this.options.current; this.openTab(this.current); this.tabs[this.options.openEvent](function() { that.openTab(that.tabs.index(this)); }); } Tabs.prototype = { openTab: function(index) { this.current = index; if (this.current !== -1 && $.inArray(this.current, this.options.disabled) === -1) { this.tabs.removeClass("current").eq(this.current).addClass("current"); this.panes.hide().eq(this.current).show(); } } }; $.fn.tabs = function(options) { var tabs = this.children("ul").find("li > a"); var panes = this.children("div"); return new Tabs(tabs, panes, options); }; });
Second, add some events to the plugin like this:
var tabs = $("div.tabs").tabs({ "openEvent": "mouseover", "disabled": [1, 2], "current": 3, "events": { "open": function(event, index) { console.log("[events-open]You click tab " + index); } } });
The plugin source code:
(function($) { function Tabs(tabs, panes, options) { var that = this; this.options = { "openEvent": "mouseover", "disabled": [], "current": 0, "events": {} }; $.extend(this.options, options); this.tabs = tabs.removeClass("current"); this.panes = panes.hide(); this.current = this.options.current; $.each(this.options.events, function(key, value) { $(that).bind(key, value); }); // Open current tab this.openTab(this.current); // Register open tab event this.tabs[this.options.openEvent](function() { that.openTab(that.tabs.index(this)); }); } Tabs.prototype = { openTab: function(index) { this.current = index; if (this.current !== -1 && $.inArray(this.current, this.options.disabled) === -1) { this.tabs.removeClass("current").eq(this.current).addClass("current"); this.panes.hide().eq(this.current).show(); $(this).trigger("open", [this.current]); } } }; $.fn.tabs = function(options) { var tabs = this.children("ul").find("li > a"); var panes = this.children("div"); return new Tabs(tabs, panes, options); }; });
The result:
[events-open]You click tab 3 [events-open]You click tab 4 [events-open]You click tab 0
Notice: In this section, we bind event to a JavaScript object not the jQuery object,
which i have mentioned in my last article.
Third, add some methods so that we can invoke like this:
tabs.bind("open", function(event, index) { console.log("[bind-open]You click tab " + index); });
Source code:
Tabs.prototype = { openTab: function(index) { // ... }, bind: function(name, fn) { $(this).bind(name, fn); } };
The result:
[events-open]You click tab 3 [events-open]You click tab 4 [bind-open]You click tab 4 [events-open]You click tab 3 [bind-open]You click tab 3 [events-open]You click tab 0 [bind-open]You click tab 0
Well, this series of tutorials has been finished. Pretty simple, isn’t it?
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2819/viewspace-2800837/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 手把手教你開發jquery外掛(二)jQuery
- 手把手教你開發 MyBatis 分頁外掛MyBatis
- 手把手教你開發微信小程式中的外掛微信小程式
- jQuery外掛開發模式jQuery模式
- jquery外掛開發方法jQuery
- 深入理解jQuery外掛開發總結(三)jQuery
- jquery外掛開發例項jQuery
- jQuery外掛開發全解析jQuery
- jQuery外掛開發模式詳解jQuery模式
- 「jQuery外掛開發日記」(二)高階外掛理念 – [翻譯]jQuery
- jQuery外掛開發流程簡單介紹jQuery
- 手把手教你寫一個 VSCode 外掛VSCode
- 深入理解jQuery外掛開發總結(一)jQuery
- Vite 實戰:手把手教你寫一個 Vite 外掛Vite
- jQuery外掛jQuery
- 掌握jQuery外掛開發,這篇文章就夠了jQuery
- jQuery外掛開發中$.extend和$.fn.extend辨析jQuery
- 移動開發必備!15款jQuery Mobile外掛移動開發jQuery
- 精美jQuery外掛及原始碼 前端開發福利啦!jQuery原始碼前端
- 手把手教你如何簡單使用laravel Modules寫外掛(1)Laravel
- jquery 外掛站jQuery
- HBuilder 第三方外掛開發UI
- 第8章 高效開發和使用外掛 (三)
- JQuery模板外掛-jquery.tmpljQuery
- 一篇文章,教你學會Vue CLI 外掛開發Vue
- Flutter外掛開發Flutter
- Mybatis外掛開發MyBatis
- Webstorm 外掛開發WebORM
- flutter 外掛開發Flutter
- 開發Rhino外掛
- chrome 外掛開發Chrome
- 手把手教你“開發GTA6”
- 手把手教你開發 Vue 元件 ( 一)Vue元件
- jQuery的外掛列表jQuery
- JQuery蜂巢圖外掛jQuery
- jQuery外掛推薦jQuery
- jquery分頁外掛jQuery
- jquery外掛寫法jQuery