パーツ番号 04-14-07 ・ JavaScript使用
条件分岐で次の質問が変わるアンケート
選んだ回答によって続く質問文がふわっと切り替わる分岐型アンケートです。不動産や保険の事前ヒアリングフォームに応用できます。
プレビュー
ソースコード
HTML
<div class="d04-14-07-wrap">
<p class="d04-14-07-q">Q. 現在お住まいは持ち家ですか?</p>
<div class="d04-14-07-choice">
<button type="button" class="d04-14-07-opt" data-a="yes">はい</button>
<button type="button" class="d04-14-07-opt" data-a="no">いいえ</button>
</div>
<div class="d04-14-07-branch">
<p class="d04-14-07-follow"></p>
</div>
</div>CSS
.d04-14-07-wrap{width:min(280px,100%)}
.d04-14-07-q{margin:0 0 12px;font-size:13px;color:#333;font-weight:700}
.d04-14-07-choice{display:flex;gap:10px}
.d04-14-07-opt{
flex:1;padding:9px;font-size:13px;font-weight:700;border-radius:8px;cursor:pointer;
border:2px solid #d5d9e0;background:#fff;color:#444;transition:border-color .2s,background .2s,color .2s;
}
.d04-14-07-opt.is-active{border-color:#4a6cf7;background:#4a6cf7;color:#fff}
.d04-14-07-branch{
margin-top:6px;max-height:0;opacity:0;overflow:hidden;
transition:max-height .35s ease,opacity .3s,margin-top .35s;
}
.d04-14-07-branch.is-show{max-height:80px;opacity:1;margin-top:14px}
.d04-14-07-follow{
margin:0;padding:10px 12px;font-size:12.5px;color:#445;
background:#f6f7fb;border-radius:8px;
}JavaScript
(function(){
var wrap=document.querySelector('.d04-14-07-wrap');
if(!wrap)return;
var opts=wrap.querySelectorAll('.d04-14-07-opt');
var branch=wrap.querySelector('.d04-14-07-branch');
var follow=wrap.querySelector('.d04-14-07-follow');
var texts={yes:'築何年ですか? リフォームのご予定は?',no:'現在のお住まいの形態を教えてください。'};
opts.forEach(function(o){
o.addEventListener('click',function(){
opts.forEach(function(x){x.classList.remove('is-active');});
o.classList.add('is-active');
follow.textContent=texts[o.getAttribute('data-a')];
branch.classList.add('is-show');
});
});
})();クラス名 .d04-14-07 は本ライブラリ内での識別用です。複数のパーツを同じページで使う場合は、重複しないよう名前を変更してください。