;(function ($, window, document, undefined) { var plugin = function (elem, options) { this.$wrapper = elem; this.$tab_list = this.$wrapper.find('.tab-title').find('.item'); this.$tabcont_wrap = this.$wrapper.find('.tab-cont__wrap'); this.$tab_cont = this.$tabcont_wrap.find('.item'); this.timer = null; this.playtimer = null this.inow = 0; this.defaults = { curdisplay: 1, mouse: 'click', changemethod: 'default', autoplay: false }; this.opts = $.extend({}, this.defaults, options); }; plugin.prototype = { inital: function () { var self = this; this.setdata(); this.tabinital(); if (this.opts.mouse === 'click') { this.$tab_list.click(function () { self.changetab($(this).index()); self.inow = $(this).index(); }); } else if (this.opts.mouse === 'over') { this.$tab_list.hover(function () { var cur_obj = this; cleartimeout(self.timer); self.timer = settimeout(function () { self.changetab($(cur_obj).index()); }, 30); self.inow = $(this).index(); }, function () { cleartimeout(self.timer); }); } else { this.$tab_list.click(function () { self.changetab($(this).index()); self.inow = $(this).index(); }); } if (this.opts.autoplay) { clearinterval(this.playtimer); this.playtimer = setinterval(function () { self.autoplay(); }, 1000); this.$wrapper.hover(function () { clearinterval(self.playtimer); }, function () { self.playtimer = setinterval(function () { self.autoplay(); }, 1000); }); } }, setdata: function () { // 设置样式 var tabcont_w = this.$tab_cont.width(); var tabcont_h = this.$tab_cont.height(); var tabcont_len = this.$tab_cont.length; switch(this.opts.changemethod) { case 'default' : this.$tab_cont.css({display: 'none'}); break; case 'horizontal' : this.$tabcont_wrap.css({width: tabcont_w * tabcont_len}); this.$tab_cont.css({float: 'left'}); break; case 'vertical' : this.$tabcont_wrap.css({height: tabcont_h * tabcont_len}); break; case 'opacity' : this.$tab_cont.css({display: 'none'}); break; default : this.$tab_cont.css({display: 'none'}); break; } }, tabinital: function () { // 初始化当前显示 var curnum = this.opts.curdisplay - 1; this.$tab_list.removeclass('item-cur'); this.$tab_list.eq(curnum).addclass('item-cur'); if (this.opts.changemethod === 'default' || this.opts.changemethod === 'opacity') { this.$tab_cont.eq(curnum).css({display: 'block'}); } else if (this.opts.changemethod === 'horizontal') { this.$tabcont_wrap.css({left: -curnum * this.$tab_cont.width()}); } else if (this.opts.changemethod === 'vertical') { this.$tabcont_wrap.css({top: -curnum * this.$tab_cont.height()}); } else { this.$tab_cont.eq(curnum).css({display: 'block'}); } this.inow = this.opts.curdisplay - 1; }, changetab: function (index) { // 选项卡切换 this.$tab_list.removeclass('item-cur'); this.$tab_list.eq(index).addclass('item-cur'); switch(this.opts.changemethod) { case 'default' : this.$tab_cont.css({display: 'none'}); this.$tab_cont.eq(index).css({display: 'block'}); break; case 'horizontal' : this.$tabcont_wrap.stop().animate({left: this.$tab_cont.width() * -index}); break; case 'vertical' : this.$tabcont_wrap.stop().animate({top: this.$tab_cont.height() * -index}); break; case 'opacity' : this.$tab_cont.stop().fadeout(); this.$tab_cont.eq(index).stop().fadein() break; default : this.$tab_cont.css({display: 'none'}); this.$tab_cont.eq(index).css({display: 'block'}); break; } }, autoplay: function () { // 自动播放 if (this.inow === this.$tab_list.length - 1) { this.inow = 0; } else { this.inow ++; } this.changetab(this.inow); }, constructor: plugin }; $.fn.tab = function (options) { var plugin = new plugin(this, options); return plugin.inital(); }; })(window.jquery, window, document);