亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

手把手教你開發jquery插件(三)

標簽:
Html/CSS 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?

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消