/* ========================================================== top.js トップページ専用 ・スマホ用スライダーのドット(お悩み・取引実績) ・取引実績の種別タブ絞り込み ・よくある質問の開閉 ・スクロールに合わせて表示するアニメーション ========================================================== */ document.addEventListener('DOMContentLoaded', function () { /* ---------- スライダーのドット ---------- */ // list:横スクロールする ul / dots:ドットを入れる ul function setupDots(list, dots) { if (!list || !dots) return function () {}; function visibleItems() { return Array.prototype.filter.call(list.children, function (li) { return !li.hidden; }); } function build() { dots.innerHTML = ''; visibleItems().forEach(function (item, i) { var li = document.createElement('li'); var a = document.createElement('a'); a.href = '#'; a.setAttribute('aria-label', (i + 1) + '枚目を表示'); a.addEventListener('click', function (e) { e.preventDefault(); list.scrollTo({ left: item.offsetLeft - list.offsetLeft - 20, behavior: 'smooth' }); }); li.appendChild(a); dots.appendChild(li); }); update(); } function update() { var items = visibleItems(); var links = dots.querySelectorAll('a'); if (!items.length) return; var current = 0; var min = Infinity; items.forEach(function (item, i) { var dist = Math.abs(item.offsetLeft - list.offsetLeft - 20 - list.scrollLeft); if (dist < min) { min = dist; current = i; } }); // 最後までスクロールしたら最後のドットを点灯 if (list.scrollLeft + list.clientWidth >= list.scrollWidth - 2) current = items.length - 1; links.forEach(function (a, i) { a.setAttribute('aria-current', i === current ? 'true' : 'false'); }); } var ticking = false; list.addEventListener('scroll', function () { if (ticking) return; ticking = true; window.requestAnimationFrame(function () { update(); ticking = false; }); }); build(); return build; } setupDots(document.querySelector('.top_worry_list'), document.querySelector('.top_worry_dots')); var rebuildResultDots = setupDots(document.querySelector('.top_result_list'), document.querySelector('.top_result_dots')); /* ---------- 取引実績の種別タブ ---------- */ // タブの文字と、カード内タグ(p:first-child > span)の文字が一致するものだけ表示 var tabs = document.querySelectorAll('.top_result_tab a'); var cards = document.querySelectorAll('.top_result_list > li'); tabs.forEach(function (tab) { tab.addEventListener('click', function (e) { e.preventDefault(); var cat = tab.textContent.trim(); tabs.forEach(function (t) { t.setAttribute('aria-selected', t === tab ? 'true' : 'false'); }); cards.forEach(function (card) { var tag = card.querySelector('div p:first-child span'); var name = tag ? tag.textContent.trim() : ''; card.hidden = !(cat === 'すべて' || name === cat); }); var list = document.querySelector('.top_result_list'); if (list) list.scrollLeft = 0; rebuildResultDots(); }); }); /* ---------- スクロールで表示 ---------- */ // 対象に data-anim を付け、画面に入ったら data-anim="in" にする(見た目は CSS 側) // JSが動かない環境では data-anim が付かないので、最初から表示されたままになる var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches; if ('IntersectionObserver' in window && !reduce) { var isSp = window.matchMedia('(max-width: 767px)').matches; // スマホの横スクロールのスライダーは、カード単位ではなく一覧ごと表示する var listItems = isSp ? '.top_worry_list, .top_result_list, .top_consult_step > li, .top_news_list > li, .top_staff_list > li, .top_faq_list > li' : '.top_worry_list > li, .top_result_list > li, .top_consult_step > li, .top_news_list > li, .top_staff_list > li, .top_faq_list > li'; var targets = [ 'main section:not(.top_main_wrap) h2', '.top_worry_lead, .top_staff_lead, .top_faq_lead, .top_result_head > p', '.top_consult_txt > p, .top_consult_txt > ul, .top_consult_case, .top_cta_box', '.top_result_tab, .top_result_btn, .top_news_head > a', listItems ].join(', '); var observer = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (!entry.isIntersecting) return; entry.target.setAttribute('data-anim', 'in'); observer.unobserve(entry.target); }); }, { rootMargin: '0px 0px -10% 0px' }); document.querySelectorAll(targets).forEach(function (el) { // 一覧の項目は少しずつずらして表示(横に3つ並ぶ想定) if (el.tagName === 'LI') { var index = Array.prototype.indexOf.call(el.parentNode.children, el); el.style.transitionDelay = (index % 3) * 0.12 + 's'; } el.setAttribute('data-anim', ''); observer.observe(el); }); // 表示し終わったら遅延を外す(hoverの動きが遅れないように) document.addEventListener('transitionend', function (e) { if (e.target.getAttribute && e.target.getAttribute('data-anim') === 'in' && e.propertyName === 'opacity') { e.target.style.transitionDelay = ''; } }); } /* ---------- よくある質問の開閉 ---------- */ // 初期表示はHTML側で閉じた状態(p に hidden)にしている document.querySelectorAll('.top_faq_list h3 a').forEach(function (q) { var answer = q.parentNode.nextElementSibling; q.addEventListener('click', function (e) { e.preventDefault(); var open = q.getAttribute('aria-expanded') === 'true'; q.setAttribute('aria-expanded', open ? 'false' : 'true'); if (answer) answer.hidden = open; }); }); });