Blame view

zhongke_vision/js/tab.js 4.5 KB
5cd946c8   Yang Xiaoxiao   .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  ;(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);