Last active
December 16, 2015 00:09
-
-
Save sagarjadhav/5345191 to your computer and use it in GitHub Desktop.
jQuery Tabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Utility : Object.create dosen't work all browsers. */ | |
if ( typeof Object.create !== 'function' ) { | |
Object.create = function( obj ) { | |
function F() {}; | |
F.prototype = obj; | |
return new F(); | |
}; | |
} | |
(function( $, window, document, undefined ) { | |
var Tab = { | |
init: function( options, elem ) { | |
var self = this; | |
self.elem = elem; | |
self.$elem = $( elem ); | |
/* Extend Options */ | |
self.options = $.extend( {}, $.fn.rtTab.options, options ); | |
self.rtTabs(); | |
}, | |
rtTabs: function() { | |
var self = this, | |
eachTab = self.$elem.find('li'); | |
/* First Tab Active */ | |
eachTab.first().addClass('active'); | |
self.rtTabContent( showFirst = 'yes' ); | |
self.rtClick(); | |
}, | |
rtClick: function() { | |
var self = this, | |
eachTab = self.$elem.find('li'), | |
tabLink = eachTab.find('a'); | |
tabLink.on('click', function(e){ | |
/* Prevent */ | |
e.preventDefault(); | |
/* Remove Active Class From All Tabs */ | |
eachTab.removeClass('active'); | |
/* Hide All Tab Contents */ | |
self.rtTabContent(); | |
/* Add Active Class to Current Tab */ | |
$(this).parent().addClass('active'); | |
/* Show Active Tab Content */ | |
var activeTab = $(this).attr('href'); | |
jQuery(activeTab).removeClass('hide'); | |
if ( typeof self.options.onComplete === 'function' ) { | |
self.options.onComplete.apply( self.elem, arguments ); | |
} | |
}); | |
}, | |
rtTabContent: function(showFirst) { | |
var self = this, | |
eachTab = self.$elem.find('li'), | |
tabLink = eachTab.find('a'); | |
tabLink.each( function() { | |
var link = $(this), | |
tabContent = link.attr('href'); | |
if( showFirst === 'yes' ) { | |
if( ! link.parent().hasClass('active') ) | |
$(tabContent).addClass('hide'); | |
} else { | |
$(tabContent).addClass('hide'); | |
} | |
}); | |
} | |
}; | |
$.fn.rtTab = function( options ) { | |
return this.each(function() { | |
var tab = Object.create( Tab ); | |
tab.init( options, this ); | |
/* Store Data */ | |
$.data( this, 'rtTab', tab ); | |
}); | |
}; | |
$.fn.rtTab.options = { | |
onComplete: null | |
}; | |
})( jQuery, window, document ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Markup | |
* | |
* <ul class="tabs"> | |
* <li class="tab"><a href="#example1"></a></li> | |
* <li class="tab"><a href="#example2"></a></li> | |
* </ul> | |
* | |
* <div id="example1" class="tab-content"></div> | |
* <div id="example2" class="tab-content"></div> | |
* | |
**/ | |
var tab = jQuery('.tab'), | |
tab_content = jQuery('.tab-content'); | |
/* Hide All Tab Contents */ | |
tab_content.hide(); | |
/* Show First Tab Content */ | |
tab_content.first().show(); | |
/* Add active Class to first Tab */ | |
tab.first().addClass('active'); | |
/* Click Event */ | |
tab.find('a').on('click', function(e) { | |
e.preventDefault(); | |
var elem = jQuery(this), | |
parent = elem.parent(); | |
if( !parent.hasClass('active') ) { | |
/* Remove Active Class From All Tabs */ | |
tab.removeClass('active'); | |
/* Hide All Tab Contents */ | |
tab_content.hide(); | |
/* Add Active Class to Current Tab */ | |
parent.addClass('active'); | |
/* Show Active Tab Content */ | |
var activeTab = elem.attr('href'); | |
jQuery(activeTab).fadeIn(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment