There are comments in the direct-attached code:
Copy Code code as follows:
/**
* Simple tab switch
* Support configurable items with the following parameters
*/
function Tab () {
This.config = {
Type: ' MouseOver ',/type default for mouse to move up
Autoplay:true,//default to AutoPlay
Triggercls: '. List ',//menu items
Panelcls: '. Tabcontent ',//content items
index:0,//Current index 0
switchto:0,//switch to which item
interval:3000,//Auto playback interval defaults to 3 in S
Pauseonhover:true,//mouse put up for pause defaults to True
Current: ' present ',///The item is added to the class name
Hidden: ' Hidden ',//class name defaults to 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);
Switch the add callback function to the end
_config.callback && $.isfunction (_config.callback) && _config.callback (index);
});
Switch to the first few items 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);
}
Auto Play
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 page is called on the following way:
Copy Code code as follows:
$ (function () {
New Tab (). Init ({
Switchto:1,
Callback:function (index) {
Console.log (index);
}
});
});