tab.js 942 B

12345678910111213141516171819202122232425262728293031323334353637
  1. mbui.define([], function (exports) {
  2. var Tab = function () {
  3. this.config = {
  4. elem: null,
  5. trigger: 'click',
  6. ontab: null
  7. };
  8. };
  9. // 初始化Tab
  10. Tab.prototype.init = function (options) {
  11. var that = this;
  12. $.extend(true, that.config, options);
  13. var elem = $(that.config.elem);
  14. var tabs = elem.find('.mbui-tab-title li');
  15. var contents = elem.find('.mbui-tab-content .mbui-tab-item');
  16. tabs.on(that.config.trigger, function () {
  17. var index = $(this).index();
  18. tabs.removeClass('active');
  19. contents.removeClass('mbui-tab-show');
  20. $(this).addClass('active');
  21. contents.eq(index).addClass('mbui-tab-show');
  22. // 执行回调函数
  23. if (typeof that.config.ontab === 'function') {
  24. that.config.ontab(index, $(this));
  25. }
  26. });
  27. };
  28. // 导出Tab模块
  29. exports('tab', function (options) {
  30. var tab = new Tab();
  31. tab.init(options);
  32. });
  33. });