tabs.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*!
  2. * PHPWind UI Library
  3. * Wind.tabs 选项卡组件
  4. * Author: chaoren1641@gmail.com
  5. * <ul class="tabs_nav">
  6. * <li><a href="ahah_1.html">Content 1</a></li>
  7. * <li><a href="ahah_2.html">Content 2</a></li>
  8. * <li><a href="ahah_3.html">Content 3</a></li>
  9. * </ul>
  10. * <div class="tabs_content">
  11. * <div>Content1</div>
  12. * <div>Content2</div>
  13. * <div>Content3</div>
  14. * </div>
  15. * $('.tabs_nav').tabs(.tabs_content > div);
  16. */
  17. ;(function ( $, window, document, undefined ) {
  18. var pluginName = 'tabs',
  19. defaults = {
  20. activeClass : 'current',//当前激活的选项卡样式
  21. event : 'click',//触发事件
  22. fx : 0,//显示时的动画,支持jQuery动画
  23. selected : 0, //默认选中项
  24. onShow : $.noop
  25. };
  26. function Plugin( element, selector, options ) {
  27. this.element = element;
  28. this.selector = selector;
  29. this.options = $.extend( {}, defaults, options) ;
  30. this.init();
  31. }
  32. Plugin.prototype.init = function () {
  33. var element = this.element,
  34. selector = this.selector,
  35. options = this.options,
  36. navList = element.children(),
  37. contentList = $(selector);
  38. //WAI-ARIA无障碍
  39. element.attr('role','tablist');
  40. navList.attr('role','tab');
  41. contentList.attr({'role':'tabpanel','aria-hidden':'true'});
  42. function show(index) {
  43. var selected_element = navList.eq(index);
  44. selected_element.addClass( options.activeClass ).siblings().removeClass( options.activeClass );
  45. var currentContent = contentList.eq(index);
  46. currentContent.show( options.fx ,options.onShow.call(currentContent)).attr('aria-hidden','false').siblings().hide( options.fx ).attr('aria-hidden','true');
  47. }
  48. show(options.selected);
  49. //add event
  50. navList.bind(options.event,function(e) {
  51. e.preventDefault();
  52. e.stopPropagation();
  53. var index = $(this).index();
  54. show(index);
  55. });
  56. navList.find(' > a').bind('focus',function(e) {
  57. e.stopPropagation();
  58. e.preventDefault();
  59. $(this).parent().trigger(options.event);
  60. });
  61. };
  62. $.fn[pluginName] = function ( selector, options ) {
  63. return this.each(function () {
  64. if (!$.data(this, 'plugin_' + pluginName)) {
  65. $.data(this, 'plugin_' + pluginName, new Plugin( $(this), selector ,options ));
  66. }
  67. });
  68. }
  69. })( jQuery, window );