| 1 |
/** |
| 2 |
* cbpFWTabs.js v1.0.0 |
| 3 |
* http://www.codrops.com |
| 4 |
* |
| 5 |
* Licensed under the MIT license. |
| 6 |
* http://www.opensource.org/licenses/mit-license.php |
| 7 |
* |
| 8 |
* Copyright 2014, Codrops |
| 9 |
* http://www.codrops.com |
| 10 |
*/ |
| 11 |
;( function( window ) { |
| 12 |
|
| 13 |
'use strict'; |
| 14 |
|
| 15 |
function extend( a, b ) { |
| 16 |
for( var key in b ) { |
| 17 |
if( b.hasOwnProperty( key ) ) { |
| 18 |
a[key] = b[key]; |
| 19 |
} |
| 20 |
} |
| 21 |
return a; |
| 22 |
} |
| 23 |
|
| 24 |
function CBPFWTabs( el, options ) { |
| 25 |
this.el = el; |
| 26 |
this.options = extend( {}, this.options ); |
| 27 |
extend( this.options, options ); |
| 28 |
this._init(); |
| 29 |
} |
| 30 |
|
| 31 |
CBPFWTabs.prototype.options = { |
| 32 |
start : 0 |
| 33 |
}; |
| 34 |
|
| 35 |
CBPFWTabs.prototype._init = function() { |
| 36 |
// tabs elems |
| 37 |
this.tabs = [].slice.call( this.el.querySelectorAll( 'nav > ul > li' ) ); |
| 38 |
// content items |
| 39 |
this.items = [].slice.call( this.el.querySelectorAll( '.content-wrap > section' ) ); |
| 40 |
// current index |
| 41 |
this.current = -1; |
| 42 |
// show current content item |
| 43 |
this._show(); |
| 44 |
// init events |
| 45 |
this._initEvents(); |
| 46 |
}; |
| 47 |
|
| 48 |
CBPFWTabs.prototype._initEvents = function() { |
| 49 |
var self = this; |
| 50 |
this.tabs.forEach( function( tab, idx ) { |
| 51 |
tab.addEventListener( 'click', function( ev ) { |
| 52 |
ev.preventDefault(); |
| 53 |
self._show( idx ); |
| 54 |
} ); |
| 55 |
} ); |
| 56 |
}; |
| 57 |
|
| 58 |
CBPFWTabs.prototype._show = function( idx ) { |
| 59 |
if( this.current >= 0 ) { |
| 60 |
this.tabs[ this.current ].className = this.items[ this.current ].className = ''; |
| 61 |
} |
| 62 |
// change current |
| 63 |
this.current = idx != undefined ? idx : this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : 0; |
| 64 |
this.tabs[ this.current ].className = 'tab-current'; |
| 65 |
this.items[ this.current ].className = 'content-current'; |
| 66 |
}; |
| 67 |
|
| 68 |
// add to global namespace |
| 69 |
window.CBPFWTabs = CBPFWTabs; |
| 70 |
|
| 71 |
})( window ); |