easyResponsiveTabs.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Easy Responsive Tabs Plugin
  2. // Author: Samson.Onna <Email : samson3d@gmail.com>
  3. (function ($) {
  4. $.fn.extend({
  5. easyResponsiveTabs: function (options) {
  6. //Set the default values, use comma to separate the settings, example:
  7. var defaults = {
  8. type: 'default', //default, vertical, accordion;
  9. width: 'auto',
  10. fit: true
  11. }
  12. //Variables
  13. var options = $.extend(defaults, options);
  14. var opt = options, jtype = opt.type, jfit = opt.fit, jwidth = opt.width, vtabs = 'vertical', accord = 'accordion';
  15. //Main function
  16. this.each(function () {
  17. var $respTabs = $(this);
  18. $respTabs.find('ul.resp-tabs-list li').addClass('resp-tab-item');
  19. $respTabs.css({
  20. 'display': 'block',
  21. 'width': jwidth
  22. });
  23. $respTabs.find('.resp-tabs-container > div').addClass('resp-tab-content');
  24. jtab_options();
  25. //Properties Function
  26. function jtab_options() {
  27. if (jtype == vtabs) {
  28. $respTabs.addClass('resp-vtabs');
  29. }
  30. if (jfit == true) {
  31. $respTabs.css({ width: '100%', margin: '0px' });
  32. }
  33. if (jtype == accord) {
  34. $respTabs.addClass('resp-easy-accordion');
  35. $respTabs.find('.resp-tabs-list').css('display', 'none');
  36. }
  37. }
  38. //Assigning the h2 markup
  39. var $tabItemh2;
  40. $respTabs.find('.resp-tab-content').before("<h2 class='resp-accordion' role='tab'><span class='resp-arrow'></span></h2>");
  41. var itemCount = 0;
  42. $respTabs.find('.resp-accordion').each(function () {
  43. $tabItemh2 = $(this);
  44. var innertext = $respTabs.find('.resp-tab-item:eq(' + itemCount + ')').text();
  45. $respTabs.find('.resp-accordion:eq(' + itemCount + ')').append(innertext);
  46. $tabItemh2.attr('aria-controls', 'tab_item-' + (itemCount));
  47. itemCount++;
  48. });
  49. //Assigning the 'aria-controls' to Tab items
  50. var count = 0,
  51. $tabContent;
  52. $respTabs.find('.resp-tab-item').each(function () {
  53. $tabItem = $(this);
  54. $tabItem.attr('aria-controls', 'tab_item-' + (count));
  55. $tabItem.attr('role', 'tab');
  56. //First active tab
  57. $respTabs.find('.resp-tab-item').first().addClass('resp-tab-active');
  58. $respTabs.find('.resp-accordion').first().addClass('resp-tab-active');
  59. $respTabs.find('.resp-tab-content').first().addClass('resp-tab-content-active').attr('style', 'display:block');
  60. //Assigning the 'aria-labelledby' attr to tab-content
  61. var tabcount = 0;
  62. $respTabs.find('.resp-tab-content').each(function () {
  63. $tabContent = $(this);
  64. $tabContent.attr('aria-labelledby', 'tab_item-' + (tabcount));
  65. tabcount++;
  66. });
  67. count++;
  68. });
  69. //Tab Click action function
  70. $respTabs.find("[role=tab]").each(function () {
  71. var $currentTab = $(this);
  72. $currentTab.click(function () {
  73. var $tabAria = $currentTab.attr('aria-controls');
  74. if ($currentTab.hasClass('resp-accordion') && $currentTab.hasClass('resp-tab-active')) {
  75. $respTabs.find('.resp-tab-content-active').slideUp('', function () { $(this).addClass('resp-accordion-closed'); });
  76. $currentTab.removeClass('resp-tab-active');
  77. return false;
  78. }
  79. if (!$currentTab.hasClass('resp-tab-active') && $currentTab.hasClass('resp-accordion')) {
  80. $respTabs.find('.resp-tab-active').removeClass('resp-tab-active');
  81. $respTabs.find('.resp-tab-content-active').slideUp().removeClass('resp-tab-content-active resp-accordion-closed');
  82. $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active');
  83. $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + ']').slideDown().addClass('resp-tab-content-active');
  84. } else {
  85. $respTabs.find('.resp-tab-active').removeClass('resp-tab-active');
  86. $respTabs.find('.resp-tab-content-active').removeAttr('style').removeClass('resp-tab-content-active').removeClass('resp-accordion-closed');
  87. $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active');
  88. $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + ']').addClass('resp-tab-content-active').attr('style', 'display:block');
  89. }
  90. });
  91. //Window resize function
  92. $(window).resize(function () {
  93. $respTabs.find('.resp-accordion-closed').removeAttr('style');
  94. });
  95. });
  96. });
  97. }
  98. });
  99. })(jQuery);