Skip to content

Instantly share code, notes, and snippets.

@Olein-jp
Created March 4, 2022 03:30
Show Gist options
  • Save Olein-jp/e54e2c90aae6da01b8ac53cc8b49ff66 to your computer and use it in GitHub Desktop.
Save Olein-jp/e54e2c90aae6da01b8ac53cc8b49ff66 to your computer and use it in GitHub Desktop.
タブを動かすスクリプト
document.addEventListener('DOMContentLoaded', function(){
// タブに対してクリックイベントを適用
const tabs = document.getElementsByClassName('tab-item');
for(let i = 0; i < tabs.length; i++) {
tabs[i].addEventListener('click', tabSwitch, false);
}
// タブをクリックすると実行する関数
function tabSwitch(){
// タブのclassの値を変更
document.getElementsByClassName('_active')[0].classList.remove('_active');
this.classList.add('_active');
// コンテンツのclassの値を変更
document.getElementsByClassName('_show')[0].classList.remove('_show');
const arrayTabs = Array.prototype.slice.call(tabs);
const index = arrayTabs.indexOf(this);
document.getElementsByClassName('content-item')[index].classList.add('_show');
};
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment