Skip to content

Instantly share code, notes, and snippets.

@shiraco
Created September 13, 2020 16:04
Show Gist options
  • Save shiraco/e16c66eac2f579152463b365bac15e7a to your computer and use it in GitHub Desktop.
Save shiraco/e16c66eac2f579152463b365bac15e7a to your computer and use it in GitHub Desktop.
tab-sample
<div class="wrap-tab">
<ul id="js-tab" class="list-tab">
<li class="active">全て</li>
<li>報知</li>
<li>通達</li>
</ul>
<div class="wrap-tab-content">
<div class="tab-content active">
<p>通達1</p>
<p>報知1</p>
<p>通達2</p>
<p>報知2</p>
</div>
<div class="tab-content">
<p>通達1</p>
<p>通達2</p>
</div>
<div class="tab-content">
<p>報知1</p>
<p>報知2</p>
</div>
</div>
</div>
document.addEventListener('DOMContentLoaded', function(){
tabs = document.querySelectorAll('#js-tab li');
for(i = 0; i < tabs.length; i++) {
tabs[i].addEventListener('click', tabSwitch, false);
}
function tabSwitch(){
tabs = document.querySelectorAll('#js-tab li');
var node = Array.prototype.slice.call(tabs, 0);
node.forEach(function (element) {
element.classList.remove('active');
});
this.classList.add('active');
content = document.querySelectorAll('.tab-content');
var node = Array.prototype.slice.call(content, 0);
node.forEach(function (element) {
element.classList.remove('active');
});
const arrayTabs = Array.prototype.slice.call(tabs);
const index = arrayTabs.indexOf(this);
document.querySelectorAll('.tab-content')[index].classList.add('active');
};
});
ul{
margin: 0;
padding: 0;
list-style-type: none;
}
p{
margin: 0;
}
.wrap-tab{
overflow: hidden;
}
.list-tab{
display: flex;
margin: 0 -2px;
border-bottom: 3px solid #5bbee5;
}
.list-tab > li{
display: block;
padding: 0.5em 1em;
margin: 0 2px;
width: 100%;
color: #fff;
text-align: center;
background: #ccc;
box-sizing: border-box;
cursor: pointer;
}
.list-tab .active{
background: #5bbee5;
}
.tab-content{
display: none;
padding: 1em;
border-bottom: 1px solid #5bbee5;
border-left: 1px solid #5bbee5;
border-right: 1px solid #5bbee5;
}
.tab-content.active{
display: block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment