DIV CSS 佈局教程網

封裝一個可設定的 Tab 選項卡元件
編輯:JQuery特效代碼     

Tab 切換是每個後台都會用到的元件。與其每次重寫,不如封裝成一個 constructor,把「觸發方式、自動播放、間隔秒數、是否滑過暫停」都開放出來。以下是一個用原生物件原型搭配 jQuery 實作的版本,程式碼內附註解。

設定與狀態

function Tab(){
  this.config = {
    type          : 'mouseover',  // 觸發類型,預設滑鼠移入
    autoplay      : true,         // 是否自動播放
    triggerCls    : '.list',      // 頁籤選項
    panelCls      : '.tabContent',// 內容面板
    index         : 0,            // 當前索引
    switchTo      : 0,            // 預設切換到第幾項
    interval      : 3000,         // 自動播放間隔(毫秒)
    pauseOnHover  : true,         // 滑鼠移入是否暫停
    current       : 'current',    // 當前項的 class
    hidden        : 'hidden',     // 隱藏用的 class
    callback      : null          // 切換後的回呼
  };
  this.cache = { timer: undefined, flag: true };
}

初始化與事件綁定

Tab.prototype = {
  init: function(options){
    this.config = $.extend(this.config, options || {});
    this._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);

      // 切換完成後執行回呼
      _config.callback && $.isFunction(_config.callback) && _config.callback(index);
    });

    // 預設切換到第幾項
    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);
    }
  }
};

自動播放與滑過暫停

    // 自動播放
    if (_config.autoplay) {
      start();
      $(_config.triggerCls).hover(function(){
        if (_config.pauseOnHover) {
          _cache.timer && clearInterval(_cache.timer);
          _cache.timer = undefined;
        }
      }, function(){ start(); });

      function start(){
        _cache.timer = setInterval(autoRun, _config.interval);
      }

      function autoRun() {
        _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);
      }
    }

呼叫方式

$(function(){
  new Tab().init({
    switchTo: 1,
    callback: function(index){
      console.log(index);
    }
  });
});

設計上的取捨

這個版本有幾個聰明的地方:用 $.extend 把使用者傳入的 options 覆蓋預設值,做到「有傳就用、沒傳就 fallback」;用 class 的加減(current 與 hidden)而非直接操作 style 來控制顯示,樣式得以完全交給 CSS;unbind 再 bind 的組合則避免重複初始化時事件被綁上兩次。

要再精進的話,可以把 triggerCls 與 panelCls 改成傳入 DOM 元素而非選擇器字串,讓元件能同時存在多份實例而不互相干擾;另外 bind/unbind 在新版 jQuery 已更名為 on/off,遷移時記得替換。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved