Skip to content

Instantly share code, notes, and snippets.

@PButcher
Created March 17, 2016 14:31
Show Gist options
  • Select an option

  • Save PButcher/140f7db487f704b098d2 to your computer and use it in GitHub Desktop.

Select an option

Save PButcher/140f7db487f704b098d2 to your computer and use it in GitHub Desktop.
Simple Tab View
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tabs</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div class="tab-view" id="tabView">
<div class="tabs-header">
<a href="#" class="tab">Tab 1</a>
<a href="#" class="tab">Tab 2</a>
<a href="#" class="tab">Tab 3</a>
<a href="#" class="tab">Tab 4</a>
</div>
<div class="tabs-content">
<div class="tab-content">Tab 1 Content</div>
<div class="tab-content">Tab 2 Content</div>
<div class="tab-content">Tab 3 Content</div>
<div class="tab-content">Tab 4 Content</div>
</div>
</div>
</body>
</html>
document.addEventListener('DOMContentLoaded', function() {
setupTabs();
loadTab(1);
});
var tabView, tabs, tabContents;
function setupTabs() {
tabView = document.getElementById('tabView');
tabs = tabView.getElementsByClassName('tab');
tabContents = tabView.getElementsByClassName('tab-content');
for(var i = 0; i < tabs.length; i++) {
tabs[i].attributes.data = i;
tabs[i].addEventListener('click', function() {
hideAllTabs();
tabContents[this.attributes.data].style.display = 'block';
});
}
}
function hideAllTabs() {
for(var i = 0; i < tabs.length; i++) {
tabContents[i].style.display = 'none';
}
}
function loadTab(tab) {
hideAllTabs();
tabContents[tab - 1].style.display = 'block';
}
html, body {
margin: 0;
}
.tab-view {
width: 900px;
height: 500px;
overflow: auto;
margin: 50px auto;
}
.tabs-header {
width: 100%;
height: 50px;
}
.tab {
width: 25%;
height: 50px;
line-height: 50px;
box-sizing: border-box;
float: left;
text-align: center;
text-decoration: none;
color: #333;
border-bottom: solid 4px #FFF;
}
.tab:hover {
color: #AAA;
border-bottom: solid 4px #DDD;
}
.tabs-content {
width: 100%;
height: 450px;
position: relative;
}
.tab-content {
width: 100%;
height: 450px;
position: absolute;
top: 0;
left: 0;
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment