<article class="carousel">
<nav>
<ul><!-- you can put the links anywhere outside of this article; the carousel will work without a nav too -->
<li><a href="#itemone" class="active">item one </a> </li>
<li><a href="#another">another </a> </li>
<li><a href="#yetanother">yetanother </a> </li>
<li><a href="#lastthing">last thing</a> </li>
</ul>
</nav>
<section id="itemone" class="active"><h1>item one</h1> put whatever you want in these</section>
<section id="another">another</section>
<section id="yetanother">another <h3> more stuff injection</h3></section>
<section id="lastthing" class="hidden"> last thing</section>
</article>
<a href="#lastthing" class="btn">last item</a>
<a href="#itemone" class="btn">first item</a>
function copyClass($section, cls) {
var id = $section.attr('id');
if (!id) {
return;
}
var has_class = $section.hasClass(cls);
$('a[href="#' + id + '"]').toggleClass(cls, has_class);
$('.' + id).toggleClass(cls, has_class);
}
function ensureConsistency() {
// We specify which classes are okay to clone
// (classes that will never be used as an ID)
var classes = ['disabled', 'disabling', 'active', 'alert', 'online'];
var copyAllClasses = function() {
var $section = $(this);
jQuery.each(classes, function(ix, cls) {
copyClass($section, cls);
});
};
// We set which elements to listen to the ID/Status of
$('section').each(copyAllClasses);
// how can we do this without specifying all elements?
$('div').each(copyAllClasses);
$('a').each(copyAllClasses);
$('span').each(copyAllClasses);
$('td').each(copyAllClasses);
$('table').each(copyAllClasses);
}
ensureConsistency(); // now we pull the trigger
function activateFirstSections($root) {
var $sections = $root.children('section');
if ($sections.length === 0) {
return;
}
if ($sections.filter('.active').length > 0) {
return;
}
var $first_section = $sections.first().addClass('active');
activateFirstSections($first_section);
}
function activateElement($elem) {
$elem.addClass('active');
$elem.siblings().removeClass('active');
}
function activateTab(id) {
var $current_section = $('#content');
if (id) {
var $elem = $('#' + id);
activateElement($elem);
activateElement($elem.parents('section'));
if ($elem[0].tagName === 'SECTION' && $current_section.has($elem).length > 0) {
$current_section = $elem;
}
}
if ($current_section.find('section.active').length === 0) {
activateFirstSections($current_section.children('article:first'));
}
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.