Skip to content

Instantly share code, notes, and snippets.

@stamaniorec
Created June 16, 2016 08:29
Show Gist options
  • Save stamaniorec/c30a89476ef7ab6fd92cf9f682188e75 to your computer and use it in GitHub Desktop.
Save stamaniorec/c30a89476ef7ab6fd92cf9f682188e75 to your computer and use it in GitHub Desktop.
Implementing my own simple tabs
<!DOCTYPE html>
<html>
<head>
<title>Tabs</title>
<style type="text/css">
body { margin: 0; }
nav ul {
background-color: #222;
margin: 0;
padding: 0;
}
nav ul li {
list-style-type: none;
float: left;
height: 30px;
line-height: 30px;
min-width: 100px;
text-align: center;
background-color: #555;
color: white;
border-right: 1px solid #222;
cursor: pointer;
}
nav ul li.active {
background-color: #999;
cursor: auto;
}
nav ul:after {
display: block;
content: '';
clear: both;
}
body, html { height: 100%; }
.tabs-content {
background-color: #999;
color: white;
height: 100%;
}
.tab { display: none; }
.tab.open { display: block; }
</style>
</head>
<body>
<nav>
<ul>
<li class="active" data-tab="1">One</li>
<li data-tab="2">Two</li>
<li data-tab="3">Three</li>
</ul>
</nav>
<div class="tabs-content">
<div class="tab open" data-tab="1">
<span>First tab is here</span>
</div>
<div class="tab" data-tab="2">
<span>Second tab is here</span>
</div>
<div class="tab" data-tab="3">
<span>Third tab is here</span>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
(function() {
var navItems = $('nav ul li');
var tabs = $('div.tab');
navItems.on('click', function() {
navItems.filter('.active').removeClass('active');
$(this).addClass('active');
var selectedTabId = $(this).data('tab');
tabs.removeClass('open');
tabs.filter(function() {
return $(this).data('tab') == selectedTabId;
}).addClass('open');
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment