Created
March 4, 2022 03:30
-
-
Save Olein-jp/e54e2c90aae6da01b8ac53cc8b49ff66 to your computer and use it in GitHub Desktop.
タブを動かすスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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