Tab switching component (Tab function) instance code _ jquery

Source: Internet
Author: User
This article mainly introduces a simple Tab switching component instance. You can refer to the following annotations in the Code:

The Code is as follows:


/**
* Simple Tab switching
* The following parameters can be configured:
*/
Function Tab (){
This. config = {
Type: 'mouseover'. // The default type is to move the cursor up.
Autoplay: true, // The default value is automatic playback.
TriggerCls: '. list', // menu item
PanelCls: '. tabcontent', // content item
Index: 0, // The current index 0
SwitchTo: 0, // which item to switch
Interval: 3000, // The default playback interval is 3 seconds.
PauseOnHover: true, // whether to pause when you place the cursor. The default value is true.
Current: 'current', // Add the current item to the class name
Hidden: 'den den ', // The default class name is hidden.
Callback: null // callback function
};

This. cache = {
Timer: undefined,
Flag: true
};
}

Tab. prototype = {

Init: function (options ){
This. config = $. extend (this. config, options || {});
Var self = this,
_ Config = self. config;
Self. _ handler ();
},
_ Handler: function (){
Var self = this,
_ Config = self. config,
_ Cache = self. cache,
Len = $ (_ config. triggerCls). length;
$ (_ Config. triggerCls). unbind (_ config. type );
$ (_ Config. triggerCls). bind (_ config. type, function (){
_ Cache. timer & clearInterval (_ cache. timer );
Var index = $ (_ config. triggerCls). index (this );
! $ (This). hasClass (_ config. current )&&
$ (This). addClass (_ config. current). siblings (). removeClass (_ config. current );
$ (_ Config. panelCls). eq (index). removeClass (_ config. hidden). siblings (). addClass (_ config. hidden );

// Add the callback function after switching
_ Config. callback & $. isFunction (_ config. callback) & _ config. callback (index );
});

// The number of items to be switched to by default
If (_ config. switchTo ){
$ (_ Config. triggerCls). eq (_ config. switchTo). addClass (_ config. current). siblings (). removeClass (_ config. current );
$ (_ Config. panelCls). eq (_ config. switchTo). removeClass (_ config. hidden). siblings (). addClass (_ config. hidden );
}

// Automatic playback
If (_ config. autoplay ){
Start ();
$ (_ Config. triggerCls). hover (function (){
If (_ config. pauseOnHover ){
_ Cache. timer & clearInterval (_ cache. timer );
_ Cache. timer = undefined;
} Else {
Return;
}
}, Function (){
Start ();
});
}
Function start (){
_ Cache. timer = setInterval (autoRun, _ config. interval );
}
Function autoRun (){
If (_ config. switchTo & (_ config. switchTo = len-1 )){
If (_ cache. flag ){
_ Config. index = _ config. switchTo;
_ Cache. flag = false;
}
}
_ Config. index ++;
If (_ config. index = len ){
_ Config. index = 0;
}
$ (_ Config. triggerCls). eq (_ config. index). addClass (_ config. current). siblings (). removeClass (_ config. current );
$ (_ Config. panelCls). eq (_ config. index). removeClass (_ config. hidden). siblings (). addClass (_ config. hidden );

}
}
};

The call method on the page is as follows:

The Code is as follows:


$ (Function (){
New Tab (). init ({
SwitchTo: 1,
Callback: function (index ){
Console. log (index );
}
});
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.