<!-- 各 .it に一意の id を付ける。#id 付きURLで開くと自動で開いてスクロール -->
<div class="bnto-acc12" data-bnto-hashaccordion>
<!-- デモ用の疑似リンク(本番では <a href="#faq-post"> でOK) -->
<div class="lk">
<button type="button" class="jp" data-target="faq-post">#faq-post を開く</button>
<button type="button" class="jp" data-target="faq-return">#faq-return を開く</button>
</div>
<div class="it" id="faq-post">
<button type="button" class="hd">配送について
<svg class="ar" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>
</button>
<div class="bd"><div><div class="in">回答1</div></div></div>
</div>
<div class="it" id="faq-return">
<button type="button" class="hd">返品について
<svg class="ar" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>
</button>
<div class="bd"><div><div class="in">回答2</div></div></div>
</div>
</div>
.bnto-acc12 {
--c1: hsl(160 55% 40%); /* アクセント色 */
--dur: .3s; /* 開閉スピード */
display: flex; flex-direction: column; gap: 8px;
}
/* デモ用の疑似リンク(本番の a タグでは不要) */
.bnto-acc12 .lk { display: flex; flex-wrap: wrap; gap: 6px; }
.bnto-acc12 .jp {
padding: 6px 12px; border: 2px solid #DFE9F1; border-radius: 999px;
background: #fff; color: var(--c1);
font: inherit; font-weight: 800; cursor: pointer;
}
.bnto-acc12 .jp:hover { border-color: var(--c1); }
.bnto-acc12 .it {
background: #fff; border: 2px solid #DFE9F1;
border-radius: 12px; overflow: hidden;
}
.bnto-acc12 .it.is-open { border-color: var(--c1); }
.bnto-acc12 .hd {
display: flex; align-items: center;
justify-content: space-between; gap: 10px;
width: 100%; padding: 11px 14px;
background: none; border: none; text-align: left;
font: inherit; font-weight: 800; cursor: pointer;
}
.bnto-acc12 .ar { color: var(--c1); transition: transform var(--dur); }
.bnto-acc12 .it.is-open .ar { transform: rotate(180deg); }
.bnto-acc12 .bd {
display: grid; grid-template-rows: 0fr;
transition: grid-template-rows var(--dur) ease;
}
.bnto-acc12 .bd > div { overflow: hidden; }
.bnto-acc12 .it.is-open .bd { grid-template-rows: 1fr; }
.bnto-acc12 .in { padding: 0 14px 12px; line-height: 1.75; }
// ハッシュ連動アコーディオン:#id で自動オープン+クリックでURL更新
(function () {
document.querySelectorAll('[data-bnto-hashaccordion]').forEach(function (group) {
if (group.dataset.bntoInit) return;
group.dataset.bntoInit = '1';
var items = Array.prototype.slice.call(group.querySelectorAll('.it'));
function setItem(item, open) {
item.classList.toggle('is-open', open);
item.querySelector('.hd').setAttribute('aria-expanded', open);
}
function openById(id, scroll) {
items.forEach(function (item) {
if (item.id === id) {
setItem(item, true);
if (scroll) item.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
});
}
items.forEach(function (item) {
var head = item.querySelector('.hd');
head.setAttribute('aria-expanded', item.classList.contains('is-open'));
head.addEventListener('click', function () {
var open = !item.classList.contains('is-open');
setItem(item, open);
// 開いた項目の #id をURLへ反映(履歴は増やさない)
if (open && item.id) history.replaceState(null, '', '#' + item.id);
});
});
// #id 付きURLで開かれたら、該当項目を開いてスクロール
if (location.hash) openById(location.hash.slice(1), true);
window.addEventListener('hashchange', function () {
openById(location.hash.slice(1), true);
});
// デモ用の疑似リンク(本番では <a href="#id"> だけでOK)
group.querySelectorAll('.jp').forEach(function (btn) {
btn.addEventListener('click', function () {
openById(btn.dataset.target, true);
history.replaceState(null, '', '#' + btn.dataset.target);
});
});
});
})();