ssp.js
52 lines
| 1 | /** |
| 2 | * scripts |
| 3 | */ |
| 4 | addEventListener('DOMContentLoaded', function () { |
| 5 | |
| 6 | // 設定タブの切替処理 |
| 7 | (function () { |
| 8 | // ページ上部へ |
| 9 | window.scrollTo(0, 0); |
| 10 | |
| 11 | const tabNavs = document.querySelectorAll('.nav-tab'); |
| 12 | const tabContents = document.querySelectorAll('.tab-contents'); |
| 13 | |
| 14 | if (location.hash) { |
| 15 | const hashTarget = document.querySelector(location.hash); |
| 16 | const hashTab = document.querySelector( |
| 17 | '[href="' + location.hash + '"]' |
| 18 | ); |
| 19 | const actTabNav = document.querySelector('.nav-tab.act_'); |
| 20 | const actTabContent = document.querySelector('.tab-contents.act_'); |
| 21 | if (hashTarget && hashTab && actTabNav && actTabContent) { |
| 22 | actTabNav.classList.remove('act_'); |
| 23 | actTabContent.classList.remove('act_'); |
| 24 | hashTarget.classList.add('act_'); |
| 25 | hashTab.classList.add('act_'); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | for (let i = 0; i < tabNavs.length; i++) { |
| 30 | tabNavs[i].addEventListener('click', function (e) { |
| 31 | e.preventDefault(); |
| 32 | const targetHash = e.target.getAttribute('href'); |
| 33 | |
| 34 | // History APIでURLを書き換える( location.hash でやると 移動してしまう) |
| 35 | history.replaceState(null, null, targetHash); |
| 36 | |
| 37 | if (!tabNavs[i].classList.contains('act_')) { |
| 38 | document |
| 39 | .querySelector('.nav-tab.act_') |
| 40 | .classList.remove('act_'); |
| 41 | tabNavs[i].classList.add('act_'); |
| 42 | |
| 43 | document |
| 44 | .querySelector('.tab-contents.act_') |
| 45 | .classList.remove('act_'); |
| 46 | tabContents[i].classList.add('act_'); |
| 47 | } |
| 48 | }); |
| 49 | } |
| 50 | })(); |
| 51 | }); |
| 52 |