JQ 封装 tab 切换

function tabSlide(obj, objHd, objBd, count, trigger, time){
// obj: Tab 切换最外层
// objHd: Tab 选项卡
// objBd: Tab 内容区
// count: Tab 切换有几个选项卡
// trigger: Tab 切换方式(点击/悬浮)
// time: Tab 自动切换时间
// now: Tab 当前展示区块的索引值,默认第一个开始
    var _this = this;
        _this.obj = obj || ".tabSlide";
        _this.objHd = objHd || ".hd li";
        _this.objBd = objBd || ".bd";
        _this.count = count || 2;
        _this.trigger = trigger || "mouseover";
        _this.time = time || null;
        _this.now = 0;
    var t;
    this.slide = function() {
        $(_this.obj + " " + _this.objHd).bind(_this.trigger, function(event) {
            $(event.target).addClass("on").siblings().removeClass("on");
            var index = $(_this.obj + " " + _this.objHd).index(this);
            $(_this.obj + " " + _this.objBd).eq(index).show().siblings().hide();
            _this.now = index;
            console.log(_this.now);
        })
    }
    this.addhover = function() {
        $(_this.obj).hover(function(){
            clearInterval(t);
        },function(){
            t = setInterval(_this.autoplay, _this.time);
        });
    }
    this.autoplay = function() {
        _this.now = _this.now >= (_this.count-1) ? 0 : ++_this.now;
        $(_this.obj + " " + _this.objHd).eq(_this.now).trigger(_this.trigger);
    }
    this.init = function() {
        this.slide();
        if (_this.time) {
            this.addhover();
            t = setInterval(this.autoplay, _this.time);
        }
    }
    this.init();
}
new tabSlide(".tab",".hd li",".bd .tabBox",2,"click",2000);
发表评论
* 昵称
* Email
* 网址
* 评论